Class: HasEasyThing

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/has_easy_thing.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#definitionObject

Returns the value of attribute definition.



3
4
5
# File 'lib/has_easy_thing.rb', line 3

def definition
  @definition
end

#model_cacheObject

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_cacheObject

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_definitionObject



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_checkObject



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_validateObject



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{ |message| self.errors[:base] << message }
  end
end

#valueObject



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