Class: Olfactory::Template
- Inherits:
-
Hash
- Object
- Hash
- Olfactory::Template
- Defined in:
- lib/olfactory/template.rb
Instance Attribute Summary collapse
-
#default_mode ⇒ Object
Returns the value of attribute default_mode.
-
#definition ⇒ Object
Returns the value of attribute definition.
-
#dictionaries ⇒ Object
Returns the value of attribute dictionaries.
-
#sequences ⇒ Object
Returns the value of attribute sequences.
-
#transients ⇒ Object
Returns the value of attribute transients.
Instance Method Summary collapse
- #add_defaults(mode) ⇒ Object
- #build(block, options = {}) ⇒ Object
- #build_macro(macro_definition, args, block) ⇒ Object
- #build_many_items(item_definition, quantity, arr, args, block) ⇒ Object
- #build_many_subtemplates(subtemplate_definition, quantity, preset_name, block) ⇒ Object
- #build_one_item(item_definition, obj, block) ⇒ Object
- #build_one_subtemplate(subtemplate_definition, preset_name, block) ⇒ Object
- #can_set_field?(meth) ⇒ Boolean
- #extract_variable_name(args) ⇒ Object
- #generate(name, options = {}, &block) ⇒ Object
-
#initialize(definition, options = {}) ⇒ Template
constructor
A new instance of Template.
- #method_missing(meth, *args, &block) ⇒ Object
- #populate_field(field_definition, meth, args, block) ⇒ Object
- #reset_dictionaries(*names) ⇒ Object
- #reset_sequences(*names) ⇒ Object
- #save! ⇒ Object
- #transient(name, value) ⇒ Object
Constructor Details
#initialize(definition, options = {}) ⇒ Template
Returns a new instance of Template.
6 7 8 9 10 11 |
# File 'lib/olfactory/template.rb', line 6 def initialize(definition, = {}) self.definition = definition self.transients = [:transients] ? [:transients].clone : {} self.sequences = [:sequences] ? [:sequences].clone : {} self.dictionaries = [:dictionaries] ? [:dictionaries].clone : {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/olfactory/template.rb', line 44 def method_missing(meth, *args, &block) # Explicit fields if field_definition = self.definition.find_field_definition(meth) populate_field(field_definition, meth, args, block) else super # Unknown method end end |
Instance Attribute Details
#default_mode ⇒ Object
Returns the value of attribute default_mode.
4 5 6 |
# File 'lib/olfactory/template.rb', line 4 def default_mode @default_mode end |
#definition ⇒ Object
Returns the value of attribute definition.
4 5 6 |
# File 'lib/olfactory/template.rb', line 4 def definition @definition end |
#dictionaries ⇒ Object
Returns the value of attribute dictionaries.
4 5 6 |
# File 'lib/olfactory/template.rb', line 4 def dictionaries @dictionaries end |
#sequences ⇒ Object
Returns the value of attribute sequences.
4 5 6 |
# File 'lib/olfactory/template.rb', line 4 def sequences @sequences end |
#transients ⇒ Object
Returns the value of attribute transients.
4 5 6 |
# File 'lib/olfactory/template.rb', line 4 def transients @transients end |
Instance Method Details
#add_defaults(mode) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/olfactory/template.rb', line 212 def add_defaults(mode) # Prevents overwrites of custom values by defaults self.default_mode = true # Hackish for sure, but its efficient... case mode when :before default_definition = definition.t_before when :after default_definition = definition.t_after end if default_definition[:evaluator] default_definition[:evaluator].call(self) elsif default_definition[:preset] preset_definition = definition.find_preset_definition(default_definition[:preset]) preset_definition[:evaluator].call(self) end self.default_mode = false end |
#build(block, options = {}) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/olfactory/template.rb', line 13 def build(block, = {}) self.add_defaults(:before) if [:defaults].nil? || [:defaults] if block # Block can be nil (when we want only defaults) if [:value] block.call(self, [:value]) else block.call(self) end end self.add_defaults(:after) if [:defaults].nil? || [:defaults] self end |
#build_macro(macro_definition, args, block) ⇒ Object
232 233 234 235 236 |
# File 'lib/olfactory/template.rb', line 232 def build_macro(macro_definition, args, block) if macro_definition[:evaluator] macro_definition[:evaluator].call(self, *args) end end |
#build_many_items(item_definition, quantity, arr, args, block) ⇒ Object
272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/olfactory/template.rb', line 272 def build_many_items(item_definition, quantity, arr, args, block) # Integer, Block if quantity && block Array.new(quantity) { block.call } # Array elsif arr arr # Object, Object... else args end end |
#build_many_subtemplates(subtemplate_definition, quantity, preset_name, block) ⇒ Object
249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/olfactory/template.rb', line 249 def build_many_subtemplates(subtemplate_definition, quantity, preset_name, block) # Integer, Block if quantity && block Array.new(quantity) { subtemplate_definition.build(block, :transients => self.transients) } # Integer, Preset Name elsif quantity && preset_name subtemplate_definition.build_preset(preset_name, quantity, :transients => self.transients) # Integer elsif quantity Array.new(quantity) { subtemplate_definition.build(nil, :transients => self.transients) } else nil end end |
#build_one_item(item_definition, obj, block) ⇒ Object
263 264 265 266 267 268 269 270 271 |
# File 'lib/olfactory/template.rb', line 263 def build_one_item(item_definition, obj, block) if block block.call elsif obj obj else nil end end |
#build_one_subtemplate(subtemplate_definition, preset_name, block) ⇒ Object
237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/olfactory/template.rb', line 237 def build_one_subtemplate(subtemplate_definition, preset_name, block) # Block if block subtemplate_definition.build(block, :transients => self.transients) # Preset Name elsif preset_name subtemplate_definition.build_preset(preset_name, 1, :transients => self.transients) # Default (nothing) else subtemplate_definition.build(nil, :transients => self.transients) end end |
#can_set_field?(meth) ⇒ Boolean
52 53 54 |
# File 'lib/olfactory/template.rb', line 52 def can_set_field?(meth) !(self.default_mode && self.has_key?(meth)) end |
#extract_variable_name(args) ⇒ Object
55 56 57 58 59 |
# File 'lib/olfactory/template.rb', line 55 def extract_variable_name(args) variable_name = args.first raise "Must provide a name when adding to a named field!" if variable_name.nil? variable_name end |
#generate(name, options = {}, &block) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/olfactory/template.rb', line 197 def generate(name, = {}, &block) sequence = self.definition.t_sequences[name] # Template scope if sequence && sequence[:scope] == :template value = sequence.generate(name, , block) # Instance scope elsif sequence && sequence[:scope] == :instance self.sequences[name] ||= { :current_seed => ([:seed] || sequence[:seed]) } value = sequence.generate(name, .merge(:seed => self.sequences[name][:current_seed]), block) self.sequences[name][:current_seed] += 1 if !.has_key?(:seed) else raise "Unknown sequence '#{name}'!" end value end |
#populate_field(field_definition, meth, args, block) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/olfactory/template.rb', line 60 def populate_field(field_definition, meth, args, block) if field_definition[:type] == :macro field_value = build_macro(field_definition, args, block) do_not_set_value = true elsif field_definition[:type] == :subtemplate && can_set_field?(meth) subtemplate_name = field_definition.has_key?(:template) ? field_definition[:template] : field_definition[:name] subtemplate_definition = Olfactory.templates[subtemplate_name] subtemplate_definition ||= Olfactory.templates[field_definition[:singular]] if subtemplate_definition if field_definition[:collection] && field_definition[:collection] <= Array # Embeds many if meth == field_definition[:singular] # Singular grammar = :singular preset_name = args.first field_value = build_one_subtemplate(subtemplate_definition, preset_name, block) else # Plural grammar = :plural quantity = args.detect { |value| value.class <= Integer } preset_name = args.detect { |value| value != quantity } field_value = build_many_subtemplates(subtemplate_definition, quantity, preset_name, block) do_not_set_value if field_value.nil? end elsif field_definition[:collection] && field_definition[:collection] <= Hash # Embeds many named if meth == field_definition[:singular] # Singular grammar = :singular variable_name = extract_variable_name(args) args = args[1..(args.size-1)] preset_name = args.first field_value = build_one_subtemplate(subtemplate_definition, preset_name, block) do_not_set_value if field_value.nil? # || field_value.empty? else # Plural grammar = :plural do_not_set_value = true # UNSUPPORTED end else # Embeds one preset_name = args.first field_value = build_one_subtemplate(subtemplate_definition, preset_name, block) do_not_set_value if field_value.nil? end else raise "Could not find a template matching '#{subtemplate_name}'!" end elsif field_definition[:type] == :item && can_set_field?(meth) if field_definition[:collection] && field_definition[:collection] <= Array # Has many if meth == field_definition[:singular] # Singular grammar = :singular obj = args.count == 1 ? args.first : args field_value = build_one_item(field_definition, obj, block) do_not_set_value = true if field_value.nil? else # Plural grammar = :plural quantity = args.first if block && args.first.class <= Integer arr = args.first if args.count == 1 && args.first.class <= Array field_value = build_many_items(field_definition, quantity, arr, args, block) do_not_set_value = true if field_value.empty? end elsif field_definition[:collection] && field_definition[:collection] <= Hash # Has many named if meth == field_definition[:singular] # Singular grammar = :singular variable_name = extract_variable_name(args) args = args[1..(args.size-1)] obj = args.first field_value = build_one_item(field_definition, obj, block) do_not_set_value = true if field_value.nil? else # Plural grammar = :plural hash = args.first if args.first.class <= Hash # Hash if hash field_value = hash end do_not_set_value = true if field_value.nil? || field_value.empty? end else # Has one obj = args.first field_value = build_one_item(field_definition, obj, block) end elsif field_definition.class == Olfactory::Dictionary if field_definition.scope == :template return_value = field_definition elsif field_definition.scope == :instance return_value = (self.dictionaries[meth] ||= {}) end do_not_set_value = true else do_not_set_value = true end # Add field value to template if !do_not_set_value if field_definition[:collection] self[field_definition[:name]] ||= field_definition[:collection].new if field_definition[:collection] <= Array if grammar == :plural return_value = self[field_definition[:name]].concat(field_value) elsif grammar == :singular return_value = self[field_definition[:name]] << field_value end elsif field_definition[:collection] <= Hash if grammar == :plural return_value = self[field_definition[:name]].merge!(field_value) elsif grammar == :singular return_value = self[field_definition[:name]][variable_name] = field_value end end else return_value = self[field_definition[:name]] = field_value end end return_value end |
#reset_dictionaries(*names) ⇒ Object
288 289 290 291 |
# File 'lib/olfactory/template.rb', line 288 def reset_dictionaries(*names) names = self.dictionaries.keys if names.empty? names.each { |name| self.dictionaries[name].reset } end |
#reset_sequences(*names) ⇒ Object
284 285 286 287 |
# File 'lib/olfactory/template.rb', line 284 def reset_sequences(*names) names = self.sequences.keys if names.empty? names.each { |name| self.sequences[name].reset } end |
#save! ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/olfactory/template.rb', line 27 def save! # Items, then subtemplates [self.definition.t_items, self.definition.t_subtemplates].each do |field_group_definitions| field_group_definitions.each do |field_name, field_definition| if field_value = self[field_name] if field_definition[:collection] && field_definition[:collection] <= Array field_value.each { |value| value.save! if value.respond_to?(:save!) } elsif field_definition[:collection] && field_definition[:collection] <= Hash field_value.values.each { |value| value.save! if value.respond_to?(:save!) } else field_value.save! if field_value.respond_to?(:save!) end end end end end |
#transient(name, value) ⇒ Object
194 195 196 |
# File 'lib/olfactory/template.rb', line 194 def transient(name, value) self.transients[name] = value if !(self.default_mode && self.transients.has_key?(name)) end |