Class: Olfactory::Template

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition, options = {}) ⇒ Template

Returns a new instance of Template.



6
7
8
9
# File 'lib/olfactory/template.rb', line 6

def initialize(definition, options = {})
  self.definition = definition
  self.transients = options[:transients] ? options[:transients].clone : {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



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
# File 'lib/olfactory/template.rb', line 27

def method_missing(meth, *args, &block)
  # Explicit fields
  if (definition.t_macros.has_key?(meth) || definition.t_subtemplates.has_key?(meth) || definition.t_items.has_key?(meth))
    if definition.t_macros.has_key?(meth)
      field_type = :macro
      definition_of_field = definition.t_macros[meth]
      field_value = build_macro(definition_of_field, args, block)
    elsif definition.t_subtemplates.has_key?(meth) && !(self.default_mode && self.has_key?(meth))
      field_type = :subtemplate
      definition_of_field = definition.t_subtemplates[meth]
      subtemplate_name = definition_of_field.has_key?(:template) ? definition_of_field[:template] : meth
      field_value = build_subtemplate(Olfactory.templates[subtemplate_name], args, block)
    elsif definition.t_items.has_key?(meth) && !(self.default_mode && self.has_key?(meth))
      field_type = :item
      definition_of_field = definition.t_items[meth]
      field_value = build_item(definition_of_field, args, block)
    end
    # Add field value to template
    if field_type && field_type != :macro
      if definition_of_field[:collection]
        self[meth] = [] if !self.has_key?(meth)
        if field_type == :subtemplate && field_value.class <= Array
          self[meth].concat(field_value)
        else
          self[meth] << field_value
        end
      else
        self[meth] = field_value
      end
    end
  # No field definition
  else
    super # Unknown method
  end
end

Instance Attribute Details

#default_modeObject

Returns the value of attribute default_mode.



4
5
6
# File 'lib/olfactory/template.rb', line 4

def default_mode
  @default_mode
end

#definitionObject

Returns the value of attribute definition.



4
5
6
# File 'lib/olfactory/template.rb', line 4

def definition
  @definition
end

#transientsObject

Returns the value of attribute transients.



4
5
6
# File 'lib/olfactory/template.rb', line 4

def transients
  @transients
end

Instance Method Details

#add_defaultsObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/olfactory/template.rb', line 65

def add_defaults
  # Prevents overwrites of custom values by defaults
  self.default_mode = true # Hackish for sure, but its efficient...

  default_definition = definition.t_default
  if default_definition[:evaluator]
    default_definition[:evaluator].call(self)
  elsif default_definition[:preset_name]
    preset_definition = definition.find_preset_definition(default_definition[:preset_name])
    preset_definition[:evaluator].call(self)
  end

  self.default_mode = false
end

#build(block, options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/olfactory/template.rb', line 11

def build(block, options = {})
  if block # Block can be nil (when we want only defaults)
    if options[:value]
      block.call(self, options[:value])
    else
      block.call(self)
    end
  end
  if options[:defaults].nil? || options[:defaults]
    # Only set defaults if configuration wasn't specified
    self.add_defaults
  end

  self
end

#build_item(item_definition, args, block) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/olfactory/template.rb', line 91

def build_item(item_definition, args, block)
  if block
    block.call(*args)
  else
    if item_definition[:evaluator]
      item_definition[:evaluator].call(*args)
    else
      args.count == 1 ? args.first : argsnew
    end
  end
end

#build_macro(macro_definition, args, block) ⇒ Object



79
80
81
82
83
# File 'lib/olfactory/template.rb', line 79

def build_macro(macro_definition, args, block)
  if macro_definition[:evaluator]
    macro_definition[:evaluator].call(self, *args)
  end
end

#build_subtemplate(subtemplate_definition, args, block) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/olfactory/template.rb', line 84

def build_subtemplate(subtemplate_definition, args, block)
  if block
    subtemplate_definition.build(block, :transients => self.transients)
  else
    subtemplate_definition.build_preset((args.count == 1 ? args.first : args), :transients => self.transients)
  end
end

#transient(name, value) ⇒ Object



62
63
64
# File 'lib/olfactory/template.rb', line 62

def transient(name, value)
  self.transients[name] = value if !(self.default_mode && self.transients.has_key?(name))
end