Class: HasEasyThing
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- HasEasyThing
- Defined in:
- lib/has_easy_thing.rb
Instance Attribute Summary collapse
-
#definition ⇒ Object
Returns the value of attribute definition.
-
#model_cache ⇒ Object
Returns the value of attribute model_cache.
-
#value_cache ⇒ Object
Returns the value of attribute value_cache.
Instance Method Summary collapse
- #get_definition ⇒ Object
- #validate_type_check ⇒ Object
- #validate_validate ⇒ Object
- #value ⇒ Object
- #value=(v) ⇒ Object
Instance Attribute Details
#definition ⇒ Object
Returns the value of attribute definition.
3 4 5 |
# File 'lib/has_easy_thing.rb', line 3 def definition @definition end |
#model_cache ⇒ Object
Returns the value of attribute model_cache.
3 4 5 |
# File 'lib/has_easy_thing.rb', line 3 def model_cache @model_cache end |
#value_cache ⇒ Object
Returns the value of attribute value_cache.
3 4 5 |
# File 'lib/has_easy_thing.rb', line 3 def value_cache @value_cache end |
Instance Method Details
#get_definition ⇒ Object
7 8 9 10 |
# File 'lib/has_easy_thing.rb', line 7 def get_definition self.model_cache = model if model_cache.blank? self.definition = model_cache.class.has_easy_configurators[self.context].definitions[self.name] if self.definition.blank? end |
#validate_type_check ⇒ Object
12 13 14 15 |
# File 'lib/has_easy_thing.rb', line 12 def validate_type_check return unless definition.has_type_check self.errors.add(:value, "has_easy type check failed for '#{self.name}'") unless definition.type_check.include?(value.class) end |
#validate_validate ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/has_easy_thing.rb', line 17 def validate_validate return unless definition.has_validate success = true if definition.validate.instance_of?(Array) and definition.validate.include?(value) == false success = false elsif definition.validate.instance_of?(Symbol) success = model_cache.send(definition.validate, value) elsif definition.validate.instance_of?(Proc) begin success = definition.validate.call(value) rescue HasEasy::ValidationError success = false end end if success == false self.errors[:base] << "has_easy validation failed for '#{self.name}'" elsif success.instance_of?(Array) success.each{ || self.errors[:base] << } end end |
#value ⇒ Object
45 46 47 |
# File 'lib/has_easy_thing.rb', line 45 def value self.value_cache ||= YAML.load(method_missing(:value)) end |
#value=(v) ⇒ Object
40 41 42 43 |
# File 'lib/has_easy_thing.rb', line 40 def value=(v) method_missing(:value=, v.to_yaml) self.value_cache = v end |