Sha256: 92750806cd1e412e7e8ebf3bb29da2e95a32b612fae90e5e5246c00e438b9e1b

Contents?: true

Size: 1.72 KB

Versions: 5

Compression:

Stored size: 1.72 KB

Contents

module CouchPotato
  module Persistence
    class SimpleProperty
      attr_accessor :name, :type
      
      def initialize(owner_clazz, name, options = {})
        self.name = name
        self.type = options[:type]
        owner_clazz.class_eval do
          attr_reader name, "#{name}_was"
          
          def initialize(attributes = {})
            super attributes
            attributes.each do |name, value|
              self.instance_variable_set("@#{name}_was", value)
            end if attributes
          end
          
          define_method "#{name}=" do |value|
            self.instance_variable_set("@#{name}", value)
          end
          
          define_method "#{name}?" do
            !self.send(name).nil? && !self.send(name).try(:blank?)
          end
          
          define_method "#{name}_changed?" do
            !self.instance_variable_get("@#{name}_not_changed") && self.send(name) != self.send("#{name}_was")
          end
          
          define_method "#{name}_not_changed" do
            self.instance_variable_set("@#{name}_not_changed", true)
          end
        end
      end
      
      def build(object, json)
        value = json[name.to_s] || json[name.to_sym]
        typecasted_value =  if type
                              type.json_create value
                            else
                              value
                            end
        object.send "#{name}=", typecasted_value
      end
      
      def dirty?(object)
        object.send("#{name}_changed?")
      end
      
      def save(object)
        
      end
      
      def destroy(object)
        
      end
      
      def serialize(json, object)
        json[name] = object.send name
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
langalex-couch_potato-0.2.0 lib/couch_potato/persistence/simple_property.rb
langalex-couch_potato-0.2.1 lib/couch_potato/persistence/simple_property.rb
langalex-couch_potato-0.2.2 lib/couch_potato/persistence/simple_property.rb
speedmax-couch_potato-0.2.0 lib/couch_potato/persistence/simple_property.rb
speedmax-couch_potato-0.2.2 lib/couch_potato/persistence/simple_property.rb