Class: Thot::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/thot/template.rb

Overview

KISS template Engine

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_file: nil, list_token:, strict: true, template_content: nil) ⇒ Template

constructor : generate the pseudo accessor for template Class from token list



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/thot/template.rb', line 20

def initialize(template_file: nil, list_token: , strict: true, template_content: nil)
  @myput  = Carioca::Registry::init.get_service name: :output if defined?(Carioca::Injector)
  @result = ""
  if template_file
    @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('Template file read error')
    end
  elsif template_content
    @content =  template_content
  else
    raise NoTemplateFile::new('No template file found or template content')
  end
    
  @tokenizer = Tokenizer::new string: @content ; @tokenizer.detect
  token_from_template = @tokenizer.tokens
 

 
  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
  if strict
    raise InvalidTokenList::new("Token list doesn't match the template") unless token_from_template.sort == @list_token.sort
  end
  if @myput then
    if @myput.level == :debug then 
      @myput.debug "Template :"
        @content.split("\n").each  do |line|
          @myput.debug "  #{line}"
        end
    end
  end
  @list_token.each do |_token|
    self.instance_eval do
      define_singleton_method(:"#{_token}=") {|_value| raise ArgumentError::new('Not a String') unless _value.class == String; @hash_token[__callee__.to_s.chomp('=')] = _value }
      define_singleton_method(_token.to_sym) { return @hash_token[__callee__.to_s]  }
    end
  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

Raises:

  • (NotAToken)

    if caling an accessor not mapped in token list



90
91
92
# File 'lib/thot/template.rb', line 90

def method_missing(_name,*_args)
  raise NotAToken
end

Instance Attribute Details

#contentObject (readonly)

getter of the flat content of the template



14
15
16
# File 'lib/thot/template.rb', line 14

def content
  @content
end

#list_tokenObject (readonly)

getter of the list of token



10
11
12
# File 'lib/thot/template.rb', line 10

def list_token
  @list_token
end

#template_fileObject (readonly)

getter of the template file



12
13
14
# File 'lib/thot/template.rb', line 12

def template_file
  @template_file
end

Instance Method Details

#map(_hash) ⇒ Object

map a hash against templates token_list

Parameters:

  • _hash (Hash)

    a hash data to map



79
80
81
82
83
84
85
86
# File 'lib/thot/template.rb', line 79

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
  }
  @hash_token = _data
end

#outputString

the templater himself : proceed to templating

Returns:

  • (String)

    the template output



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/thot/template.rb', line 96

def output
  @result = @content

  @tokenizer.definitions.each do |item, rule|
    @result.gsub!(item, Thot::Processor::new(value:  @hash_token[rule[:key].downcase], rule: rule).result)
  end

  if @myput then
    if @myput.level == :debug then 
      @myput.debug "Output :"
        @result.split("\n").each  do |line|
          @myput.debug "  #{line}"
        end
    end
  end
  return @result
end

#token(_token, _value) ⇒ Object

generic accessor

Parameters:

  • _token (Symbol)

    in the token list

  • _value (String)

    a text value

Raises:

  • (ArgumentError)

    if _valu is not a String



72
73
74
75
# File 'lib/thot/template.rb', line 72

def token(_token,_value)
  raise ArgumentError::new('Not a String') unless _value.class == String
  @hash_token[_token.to_s] = _value
end