Class: Splash::Templates::Template
- Inherits:
-
Object
- Object
- Splash::Templates::Template
- Defined in:
- lib/splash/templates.rb
Overview
KISS template Engine
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#list_token ⇒ Object
readonly
Returns the value of attribute list_token.
-
#template_file ⇒ Object
readonly
Returns the value of attribute template_file.
Instance Method Summary collapse
-
#initialize(_options) ⇒ Template
constructor
constructor : generate the pseudo accessor for template Class from token list.
-
#map(_hash) ⇒ Object
map a hash against templates token_list.
-
#method_missing(_name, *_args) ⇒ Object
collector for pseudo accessor to prevent bad mapping.
-
#output ⇒ String
the templater;proceed to templating.
-
#token(_token, _value) ⇒ Object
generic accessor.
Constructor Details
#initialize(_options) ⇒ Template
constructor : generate the pseudo accessor for template Class from token list
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/splash/templates.rb', line 13 def initialize() @template_file = [:template_file] raise NoTemplateFile::new('No template file found') unless File::exist?(@template_file) begin @content = IO::readlines(@template_file).join.chomp rescue raise NoTemplateFile::new('No template file found') end token_from_template = @content.scan(/%%(\w+)%%/).flatten.uniq.map{ |item| item.downcase.to_sym} begin @list_token = [:list_token] @hash_token = Hash::new; @list_token.each{|_item| @hash_token[_item.to_s] = String::new('')} rescue raise InvalidTokenList::new("Token list malformation") end raise InvalidTokenList::new("Token list doesn't match the template") unless token_from_template.sort == @list_token.sort @list_token.each{|_token| eval("def #{_token}=(_value); raise ArgumentError::new('Not a String') unless _value.class == String; @hash_token['#{_token}'] = _value ;end")} @list_token.each{|_token| eval("def #{_token}; @hash_token['#{_token}'] ;end")} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(_name, *_args) ⇒ Object
collector for pseudo accessor to prevent bad mapping
58 59 60 |
# File 'lib/splash/templates.rb', line 58 def method_missing(_name,*_args) raise NotAToken end |
Instance Attribute Details
#content ⇒ Object (readonly)
Returns the value of attribute content.
10 11 12 |
# File 'lib/splash/templates.rb', line 10 def content @content end |
#list_token ⇒ Object (readonly)
Returns the value of attribute list_token.
8 9 10 |
# File 'lib/splash/templates.rb', line 8 def list_token @list_token end |
#template_file ⇒ Object (readonly)
Returns the value of attribute template_file.
9 10 11 |
# File 'lib/splash/templates.rb', line 9 def template_file @template_file end |
Instance Method Details
#map(_hash) ⇒ Object
map a hash against templates token_list
46 47 48 49 50 51 52 53 54 |
# File 'lib/splash/templates.rb', line 46 def map(_hash) _data = {} _hash.each { |item,val| raise ArgumentError::new("#{item} : Not a String") unless val.class == String _data[item.to_s.downcase] = val } raise InvalidTokenList::new("Token list malformation") unless _data.keys.sort == @list_token.map{|_token| _token.to_s }.sort @hash_token = _data end |
#output ⇒ String
the templater;proceed to templating
64 65 66 67 68 69 70 71 |
# File 'lib/splash/templates.rb', line 64 def output _my_res = String::new('') _my_res = @content @list_token.each{|_token| _my_res.gsub!(/%%#{_token.to_s.upcase}%%/,@hash_token[_token.to_s]) } return _my_res end |
#token(_token, _value) ⇒ Object
generic accessor
39 40 41 42 |
# File 'lib/splash/templates.rb', line 39 def token(_token,_value) raise ArgumentError::new('Not a String') unless _value.class == String @hash_token[_token.to_s] = _value end |