Module: Property::Attribute::ClassMethods

Defined in:
lib/property/attribute.rb

Instance Method Summary collapse

Instance Method Details

#store_properties_in(accessor) ⇒ Object



37
38
39
40
41
42
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/property/attribute.rb', line 37

def store_properties_in(accessor)
  if accessor.nil? || accessor == self
    accessor = ''
  else
    accessor = "#{accessor}."
  end
  load_and_dump_methods =<<-EOF
    private
      @@invalid_property_failover = nil
      
      def load_properties
        raw_data = #{accessor}read_attribute('properties')
        prop = raw_data ? decode_properties(raw_data) : Properties.new
        # We need to set the owner to access property definitions and enable
        # type casting on write.
        prop.owner = self
        prop
      rescue => err
        if @@invalid_property_failover
          Properties[@@invalid_property_failover]
        else
          raise Property::DecodingError.new err.message
        end
      end

      def dump_properties
        if @properties && @properties.changed?
          if [email protected]?
            #{accessor}write_attribute('properties', encode_properties(@properties))
          else
            #{accessor}write_attribute('properties', nil)
          end
          @properties.clear_changes!
        end
        true
      end
      
      def self.invalid_property_failover(failover)
        raise "Invalid failover property value type: should be a Hash." unless failover.kind_of?(Hash) || failover == nil
        @@invalid_property_failover = failover
      end
  EOF
  class_eval(load_and_dump_methods, __FILE__, __LINE__)
end