Class: Olfactory::TemplateDefinition

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTemplateDefinition

Returns a new instance of TemplateDefinition.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/olfactory/template_definition.rb', line 6

def initialize
  self.t_items = {}
  self.t_subtemplates = {}
  self.t_sequences = {}
  self.t_dictionaries = {}
  self.t_macros = {}
  self.t_presets = {}
  self.t_befores = {}
  self.t_afters = {}
  self.t_instantiators = {}
end

Instance Attribute Details

#t_aftersObject

Returns the value of attribute t_afters.



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

def t_afters
  @t_afters
end

#t_beforesObject

Returns the value of attribute t_befores.



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

def t_befores
  @t_befores
end

#t_dictionariesObject

Returns the value of attribute t_dictionaries.



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

def t_dictionaries
  @t_dictionaries
end

#t_instantiatorsObject

Returns the value of attribute t_instantiators.



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

def t_instantiators
  @t_instantiators
end

#t_itemsObject

Returns the value of attribute t_items.



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

def t_items
  @t_items
end

#t_macrosObject

Returns the value of attribute t_macros.



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

def t_macros
  @t_macros
end

#t_presetsObject

Returns the value of attribute t_presets.



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

def t_presets
  @t_presets
end

#t_sequencesObject

Returns the value of attribute t_sequences.



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

def t_sequences
  @t_sequences
end

#t_subtemplatesObject

Returns the value of attribute t_subtemplates.



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

def t_subtemplates
  @t_subtemplates
end

Instance Method Details

#after(context = nil, options = {}, &block) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/olfactory/template_definition.rb', line 184

def after(context = nil, options = {}, &block)
  if context.class == Hash
    # Arguments need to be remapped...
    options = context
    context = nil
  end
  after_definition = { :type => :default,
                        :evaluator => block
                      }.merge(options)
  after_definition.merge!(:preset => options[:preset]) if options[:preset]
  context ||= :all
  self.t_afters[context] ||= []
  self.t_afters[context] << after_definition
end

#before(context = nil, options = {}, &block) ⇒ Object

Defines defaults



170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/olfactory/template_definition.rb', line 170

def before(context = nil, options = {}, &block)
  if context.class == Hash
    # Arguments need to be remapped...
    options = context
    context = nil
  end
  before_definition = { :type => :default,
                        :evaluator => block
                      }.merge(options)
  before_definition.merge!(:preset => options[:preset]) if options[:preset]
  context ||= :all
  self.t_befores[context] ||= []
  self.t_befores[context] << before_definition
end

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



18
19
20
21
22
23
24
25
26
# File 'lib/olfactory/template_definition.rb', line 18

def construct(block, options = {})
  if options[:preset] || options[:quantity]
    self.construct_preset(options[:preset], (options[:quantity] || 1), options.reject { |k,v| [:preset, :quantity].include?(k) })
  else
    new_template = Template.new(self, options)
    new_template.construct(block, options)
  end
  new_template
end

#construct_preset(preset_name, quantity, options = {}) ⇒ 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
# File 'lib/olfactory/template_definition.rb', line 27

def construct_preset(preset_name, quantity, options = {})
  raise "Quantity must be an integer!" if !(quantity.class <= Integer)

  if quantity > 1
    # Build multiple
    if preset_name
      Array.new(quantity) { self.construct_preset(preset_name, 1, options) }
    else
      Array.new(quantity) { self.construct(nil, options) }
    end
  elsif quantity == 1
    if preset_definition = self.find_preset_definition(preset_name)
      # Build single
      new_template = Template.new(self, options)
      preset_block = preset_definition[:evaluator]
      if preset_definition[:regexp]
        new_template.construct(preset_block, options.merge(:value => preset_name))
      else
        new_template.construct(preset_block, options)
      end
    elsif preset_name.nil?
      self.construct(nil, options)
    else
      raise "Missing preset matching '#{preset_name}' for template!"
    end
  else quantity <= 0
    raise "Can't build 0 or less items!"
  end
end

#dictionary(name, options = {}) ⇒ Object

Defines a dictionary



140
141
142
# File 'lib/olfactory/template_definition.rb', line 140

def dictionary(name, options = {})
  self.t_dictionaries[name] = Olfactory::Dictionary.new(name, { :scope => :instance }.merge(options))
end

#embeds_many(name, options = {}, &block) ⇒ Object



130
131
132
# File 'lib/olfactory/template_definition.rb', line 130

def embeds_many(name, options = {}, &block)
  self.embeds_one(name, options.merge(:collection => (options[:named] ? Hash : Array)), &block)
end

#embeds_one(name, options = {}, &block) ⇒ Object

Defines a hash-holding field



124
125
126
127
128
129
# File 'lib/olfactory/template_definition.rb', line 124

def embeds_one(name, options = {}, &block)
  self.t_subtemplates[name] = { :type => :subtemplate,
                                :name => name,
                                :evaluator => block 
                              }.merge(options)
end

#find_definition_in_list(name, definition_list) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/olfactory/template_definition.rb', line 65

def find_definition_in_list(name, definition_list)
  definition = definition_list[name]
  definition ||= definition_list.values.detect do |v|
    v.has_key?(:alias) && (v[:alias] == name || (v.respond_to?("include") && v.include?(name)))
  end
  definition
end

#find_dictionary_definition(name) ⇒ Object



89
90
91
# File 'lib/olfactory/template_definition.rb', line 89

def find_dictionary_definition(name)
  self.t_dictionaries[name]
end

#find_field_definition(name) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/olfactory/template_definition.rb', line 57

def find_field_definition(name)
  definition = find_macro_definition(name)
  definition ||= find_subtemplate_definition(name)
  definition ||= find_item_definition(name)
  definition ||= find_dictionary_definition(name)
  definition
end

#find_item_definition(name) ⇒ Object



83
84
85
86
87
# File 'lib/olfactory/template_definition.rb', line 83

def find_item_definition(name)
  definition = self.find_definition_in_list(name, self.t_items)
  definition ||= self.t_items.values.detect { |v| v.has_key?(:singular) && v[:singular] == name }
  definition
end

#find_macro_definition(name) ⇒ Object



73
74
75
# File 'lib/olfactory/template_definition.rb', line 73

def find_macro_definition(name)
  self.find_definition_in_list(name, self.t_macros)
end

#find_preset_definition(name) ⇒ Object



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

def find_preset_definition(name)
  preset_definition = self.find_definition_in_list(name, self.t_presets)
  if preset_definition.nil?
    # Try to find a regexp named preset that matches
    name = self.t_presets.keys.detect { |p_name| p_name.class == Regexp && p_name.match(name.to_s) }
    preset_definition = self.t_presets[name] if name
  end
  preset_definition
end

#find_subtemplate_definition(name) ⇒ Object



77
78
79
80
81
# File 'lib/olfactory/template_definition.rb', line 77

def find_subtemplate_definition(name)
  definition = self.find_definition_in_list(name, self.t_subtemplates)
  definition ||= self.t_subtemplates.values.detect { |v| v.has_key?(:singular) && v[:singular] == name }
  definition
end

#has_many(name, options = {}, &block) ⇒ Object



119
120
121
# File 'lib/olfactory/template_definition.rb', line 119

def has_many(name, options = {}, &block)
  self.has_one(name, options.merge(:collection => (options[:named] ? Hash : Array)), &block)
end

#has_one(name, options = {}, &block) ⇒ Object

Defines a value holding field



113
114
115
116
117
118
# File 'lib/olfactory/template_definition.rb', line 113

def has_one(name, options = {}, &block)
  self.t_items[name] = {  :type => :item,
                          :name => name,
                          :evaluator => block
                        }.merge(options)
end

#instantiate(name, options = {}, &block) ⇒ Object

Defines an instantiator



162
163
164
165
166
167
# File 'lib/olfactory/template_definition.rb', line 162

def instantiate(name, options = {}, &block)
  self.t_instantiators[name] = {  :type => :instantiator,
                                  :name => name,
                                  :evaluator => block 
                                }.merge(options)
end

#macro(name, options = {}, &block) ⇒ Object

Defines a macro



145
146
147
148
149
150
# File 'lib/olfactory/template_definition.rb', line 145

def macro(name, options = {}, &block)
  self.t_macros[name] = { :type => :macro,
                          :name => name,
                          :evaluator => block 
                        }.merge(options)
end

#preset(name, options = {}, &block) ⇒ Object

Defines a preset of values



153
154
155
156
157
158
159
# File 'lib/olfactory/template_definition.rb', line 153

def preset(name, options = {}, &block)
  self.t_presets[name] = {  :type => :preset,
                            :name => name,
                            :evaluator => block
                          }.merge(options)
  self.t_presets[name] = self.t_presets[name].merge(:regexp => name) if name.class <= Regexp
end

#reset_dictionaries(*names) ⇒ Object



107
108
109
110
# File 'lib/olfactory/template_definition.rb', line 107

def reset_dictionaries(*names)
  names = self.t_dictionaries.keys if names.empty?
  names.each { |name| self.t_dictionaries[name].reset }
end

#reset_sequences(*names) ⇒ Object



103
104
105
106
# File 'lib/olfactory/template_definition.rb', line 103

def reset_sequences(*names)
  names = self.t_sequences.keys if names.empty?
  names.each { |name| self.t_sequences[name].reset }
end

#sequence(name, options = {}, &block) ⇒ Object

Defines a sequence



135
136
137
# File 'lib/olfactory/template_definition.rb', line 135

def sequence(name, options = {}, &block)
  self.t_sequences[name] = Olfactory::Sequence.new(name, { :scope => :instance }.merge(options), block)
end