module Property::Attribute::ClassMethods

Public Instance Methods

store_properties_in(accessor) click to toggle source
# 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 !@properties.empty?
                  #{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