Module: Izzle::HasEasy::InstanceMethods
- Defined in:
- lib/has_easy.rb
Instance Method Summary collapse
- #get_has_easy_thing(context, name, do_postprocess = false) ⇒ Object
- #set_has_easy_thing(context, name, value, do_preprocess = false) ⇒ Object
Instance Method Details
#get_has_easy_thing(context, name, do_postprocess = false) ⇒ Object
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 |
# File 'lib/has_easy.rb', line 82 def get_has_easy_thing(context, name, do_postprocess = false) context = Helpers.normalize(context) name = Helpers.normalize(name) # check to make sure the context exists raise ArgumentError, "has_easy('#{context}') is not defined for class #{self.class}" \ unless self.class.has_easy_configurators.has_key?(context) configurator = self.class.has_easy_configurators[context] # check to make sure the name of the thing exists raise ArgumentError, "'#{name}' not defined for has_easy('#{context}') for class #{self.class}" \ unless configurator.definitions.has_key?(name) definition = configurator.definitions[name] # invoke the association things = send(context) # try to find what they are looking for thing = things.detect{ |thing| thing.name == name } # if the thing isn't found, try to fallback on a default if thing.blank? # TODO break all these nested if statements out into helper methods, i like prettier code # TODO raise an exception if we don't respond to default_through or the resulting object doesn't respond to the context if definition.has_default_through and respond_to?(definition.default_through) and (through = send(definition.default_through)).blank? == false value = through.send(context)[name] elsif definition.has_default_dynamic if definition.default_dynamic.instance_of?(Proc) value = definition.default_dynamic.call(self) else # TODO raise an exception if we don't respond to default_dynamic value = send(definition.default_dynamic) end elsif definition.has_default value = Marshal::load(Marshal.dump(definition.default)) # BUGFIX deep cloning default values else value = nil end else value = thing.value end value = definition.postprocess.call(value) if do_postprocess and definition.has_postprocess value end |
#set_has_easy_thing(context, name, value, do_preprocess = false) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/has_easy.rb', line 43 def set_has_easy_thing(context, name, value, do_preprocess = false) context = Helpers.normalize(context) name = Helpers.normalize(name) # TODO dry this shit out, it's a copy/paste job with get_has_easy_thing # check to make sure the context exists raise ArgumentError, "has_easy('#{context}') is not defined for class #{self.class}" \ unless self.class.has_easy_configurators.has_key?(context) configurator = self.class.has_easy_configurators[context] # check to make sure the name of the thing exists raise ArgumentError, "'#{name}' not defined for has_easy('#{context}') for class #{self.class}" \ unless configurator.definitions.has_key?(name) definition = configurator.definitions[name] # do preprocess here, type_check and validate can be done as AR validation in HasEasyThing value = definition.preprocess.call(value) if do_preprocess and definition.has_preprocess # invoke the assocation things = send(context) # if thing already exists, update it, otherwise add a new one thing = things.detect{ |thing| thing.name == name } if thing.blank? thing = HasEasyThing.new :context => context, :name => name, :value => value thing.model = self #thing.set_model_target(self) # for the bug regarding thing's validation trying to invoke the 'model' assocation when self is a new record send("#{context}").send("<<", thing) else thing.value = value end thing.value end |