Sha256: fc72da38f68650593d64d3bf6c8cb5f9c6c16310e587d4f65d42f9bbb35f1df5

Contents?: true

Size: 1.91 KB

Versions: 2

Compression:

Stored size: 1.91 KB

Contents

module CouchPotato
  module Persistence
    class SimpleProperty  #:nodoc:
      attr_accessor :name, :type
      
      def initialize(owner_clazz, name, options = {})
        self.name = name
        self.type = options[:type]
        @type_caster = TypeCaster.new
        
        define_accessors accessors_module_for(owner_clazz), name, options
      end
      
      def build(object, json)
        value = json[name.to_s].nil? ? json[name.to_sym] : json[name.to_s]
        object.send "#{name}=", value
      end
      
      def dirty?(object)
        object.send("#{name}_changed?")
      end
      
      def serialize(json, object)
        json[name] = object.send name
      end
      alias :value :serialize
      
      private
      
      def accessors_module_for(clazz)
        unless clazz.const_defined?('AccessorMethods')
          accessors_module = clazz.const_set('AccessorMethods', Module.new) 
          clazz.send(:include, accessors_module)
        end
        clazz.const_get('AccessorMethods')
      end
      
      def define_accessors(base, name, options)
        base.class_eval do
          define_method "#{name}" do
            value = self.instance_variable_get("@#{name}")
            if value.nil? && options[:default]
              default = clone_attribute(options[:default])
              self.instance_variable_set("@#{name}", default)
              default
            else
              value
            end
          end
          
          define_method "#{name}=" do |value|
            typecasted_value = type_caster.cast(value, options[:type])
            send("#{name}_will_change!") unless @skip_dirty_tracking || typecasted_value == send(name)
            self.instance_variable_set("@#{name}", typecasted_value)
          end
          
          define_method "#{name}?" do
            !self.send(name).nil? && !self.send(name).try(:blank?)
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
davber_couch_potato-0.4.0 lib/couch_potato/persistence/simple_property.rb
couch_potato-0.4.0 lib/couch_potato/persistence/simple_property.rb