Class: Olfactory::TemplateDefinition
- Inherits:
-
Object
- Object
- Olfactory::TemplateDefinition
- Defined in:
- lib/olfactory/template_definition.rb
Instance Attribute Summary collapse
-
#t_after ⇒ Object
Returns the value of attribute t_after.
-
#t_before ⇒ Object
Returns the value of attribute t_before.
-
#t_dictionaries ⇒ Object
Returns the value of attribute t_dictionaries.
-
#t_items ⇒ Object
Returns the value of attribute t_items.
-
#t_macros ⇒ Object
Returns the value of attribute t_macros.
-
#t_presets ⇒ Object
Returns the value of attribute t_presets.
-
#t_sequences ⇒ Object
Returns the value of attribute t_sequences.
-
#t_subtemplates ⇒ Object
Returns the value of attribute t_subtemplates.
Instance Method Summary collapse
- #after(options = {}, &block) ⇒ Object
-
#before(options = {}, &block) ⇒ Object
Defines defaults.
- #build(block, options = {}) ⇒ Object
- #build_preset(preset_name, quantity, options = {}) ⇒ Object
-
#dictionary(name, options = {}) ⇒ Object
Defines a dictionary.
- #embeds_many(name, options = {}, &block) ⇒ Object
-
#embeds_one(name, options = {}, &block) ⇒ Object
Defines a hash-holding field.
- #find_definition_in_list(name, definition_list) ⇒ Object
- #find_dictionary_definition(name) ⇒ Object
- #find_field_definition(name) ⇒ Object
- #find_item_definition(name) ⇒ Object
- #find_macro_definition(name) ⇒ Object
- #find_preset_definition(name) ⇒ Object
- #find_subtemplate_definition(name) ⇒ Object
- #has_many(name, options = {}, &block) ⇒ Object
-
#has_one(name, options = {}, &block) ⇒ Object
Defines a value holding field.
-
#initialize ⇒ TemplateDefinition
constructor
A new instance of TemplateDefinition.
-
#macro(name, options = {}, &block) ⇒ Object
Defines a macro.
-
#preset(name, options = {}, &block) ⇒ Object
Defines a preset of values.
- #reset_dictionaries(*names) ⇒ Object
- #reset_sequences(*names) ⇒ Object
-
#sequence(name, options = {}, &block) ⇒ Object
Defines a sequence.
Constructor Details
#initialize ⇒ TemplateDefinition
Returns a new instance of TemplateDefinition.
6 7 8 9 10 11 12 13 14 15 |
# 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_before = {} self.t_after = {} end |
Instance Attribute Details
#t_after ⇒ Object
Returns the value of attribute t_after.
4 5 6 |
# File 'lib/olfactory/template_definition.rb', line 4 def t_after @t_after end |
#t_before ⇒ Object
Returns the value of attribute t_before.
4 5 6 |
# File 'lib/olfactory/template_definition.rb', line 4 def t_before @t_before end |
#t_dictionaries ⇒ Object
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_items ⇒ Object
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_macros ⇒ Object
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_presets ⇒ Object
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_sequences ⇒ Object
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_subtemplates ⇒ Object
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(options = {}, &block) ⇒ Object
167 168 169 170 171 |
# File 'lib/olfactory/template_definition.rb', line 167 def after( = {}, &block) self.t_after = { :type => :default, :evaluator => block }.merge() self.t_after = self.t_after.merge(:preset => [:preset]) if [:preset] end |
#before(options = {}, &block) ⇒ Object
Defines defaults
161 162 163 164 165 166 |
# File 'lib/olfactory/template_definition.rb', line 161 def before( = {}, &block) self.t_before = { :type => :default, :evaluator => block }.merge() self.t_before = self.t_before.merge(:preset => [:preset]) if [:preset] end |
#build(block, options = {}) ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/olfactory/template_definition.rb', line 17 def build(block, = {}) if [:preset] || [:quantity] self.build_preset([:preset], ([:quantity] || 1), .reject { |k,v| [:preset, :quantity].include?(k) }) else new_template = Template.new(self, ) new_template.build(block, ) end new_template end |
#build_preset(preset_name, quantity, options = {}) ⇒ Object
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 |
# File 'lib/olfactory/template_definition.rb', line 26 def build_preset(preset_name, quantity, = {}) raise "Quantity must be an integer!" if !(quantity.class <= Integer) if quantity > 1 # Build multiple if preset_name Array.new(quantity) { self.build_preset(preset_name, 1, ) } else Array.new(quantity) { self.build(nil, ) } end elsif quantity == 1 if preset_definition = self.find_preset_definition(preset_name) # Build single new_template = Template.new(self, ) preset_block = preset_definition[:evaluator] if preset_definition[:regexp] new_template.build(preset_block, .merge(:value => preset_name)) else new_template.build(preset_block, ) end elsif preset_name.nil? self.build(nil, ) 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
139 140 141 |
# File 'lib/olfactory/template_definition.rb', line 139 def dictionary(name, = {}) self.t_dictionaries[name] = Olfactory::Dictionary.new(name, { :scope => :instance }.merge()) end |
#embeds_many(name, options = {}, &block) ⇒ Object
129 130 131 |
# File 'lib/olfactory/template_definition.rb', line 129 def (name, = {}, &block) self.(name, .merge(:collection => ([:named] ? Hash : Array)), &block) end |
#embeds_one(name, options = {}, &block) ⇒ Object
Defines a hash-holding field
123 124 125 126 127 128 |
# File 'lib/olfactory/template_definition.rb', line 123 def (name, = {}, &block) self.t_subtemplates[name] = { :type => :subtemplate, :name => name, :evaluator => block }.merge() end |
#find_definition_in_list(name, definition_list) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/olfactory/template_definition.rb', line 64 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
88 89 90 |
# File 'lib/olfactory/template_definition.rb', line 88 def find_dictionary_definition(name) self.t_dictionaries[name] end |
#find_field_definition(name) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/olfactory/template_definition.rb', line 56 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
82 83 84 85 86 |
# File 'lib/olfactory/template_definition.rb', line 82 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
72 73 74 |
# File 'lib/olfactory/template_definition.rb', line 72 def find_macro_definition(name) self.find_definition_in_list(name, self.t_macros) end |
#find_preset_definition(name) ⇒ Object
92 93 94 95 96 97 98 99 100 |
# File 'lib/olfactory/template_definition.rb', line 92 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
76 77 78 79 80 |
# File 'lib/olfactory/template_definition.rb', line 76 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
118 119 120 |
# File 'lib/olfactory/template_definition.rb', line 118 def has_many(name, = {}, &block) self.has_one(name, .merge(:collection => ([:named] ? Hash : Array)), &block) end |
#has_one(name, options = {}, &block) ⇒ Object
Defines a value holding field
112 113 114 115 116 117 |
# File 'lib/olfactory/template_definition.rb', line 112 def has_one(name, = {}, &block) self.t_items[name] = { :type => :item, :name => name, :evaluator => block }.merge() end |
#macro(name, options = {}, &block) ⇒ Object
Defines a macro
144 145 146 147 148 149 |
# File 'lib/olfactory/template_definition.rb', line 144 def macro(name, = {}, &block) self.t_macros[name] = { :type => :macro, :name => name, :evaluator => block }.merge() end |
#preset(name, options = {}, &block) ⇒ Object
Defines a preset of values
152 153 154 155 156 157 158 |
# File 'lib/olfactory/template_definition.rb', line 152 def preset(name, = {}, &block) self.t_presets[name] = { :type => :preset, :name => name, :evaluator => block }.merge() self.t_presets[name] = self.t_presets[name].merge(:regexp => name) if name.class <= Regexp end |
#reset_dictionaries(*names) ⇒ Object
106 107 108 109 |
# File 'lib/olfactory/template_definition.rb', line 106 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
102 103 104 105 |
# File 'lib/olfactory/template_definition.rb', line 102 def reset_sequences(*names) names = self.t_sequences.keys if names.empty? names.each { |name| self.t_sequences[name].reset } end |