Sha256: d59018d6bf05525b8345acc96b69c6dc8150ca9ed6e7f5f0c93f07c0e4132084

Contents?: true

Size: 1.36 KB

Versions: 5

Compression:

Stored size: 1.36 KB

Contents

module CouchPotato
  module Persistence
    class BelongsToProperty
      attr_accessor :name
      
      def initialize(owner_clazz, name, options = {})
        self.name = name
        accessors =  <<-ACCESSORS
          def #{name}
            return @#{name} if instance_variable_defined?(:@#{name})
            @#{name} = @#{name}_id ? #{item_class_name}.find(@#{name}_id) : nil
          end
          
          def #{name}=(value)
            @#{name} = value
            if value.nil?
              @#{name}_id = nil
            else
              @#{name}_id = value.id
            end
          end
          
          def #{name}_id=(id)
            remove_instance_variable(:@#{name}) if instance_variable_defined?(:@#{name})
            @#{name}_id = id
          end
        ACCESSORS
        owner_clazz.class_eval accessors
        owner_clazz.send :attr_reader, "#{name}_id"
      end
      
      def save(object)
        
      end
      
      def dirty?(object)
        false
      end
      
      def destroy(object)
        
      end
      
      def build(object, json)
        object.send "#{name}_id=", json["#{name}_id"]
      end
      
      def serialize(json, object)
        json["#{name}_id"] = object.send("#{name}_id") if object.send("#{name}_id")
      end
      
      def item_class_name
        @name.to_s.camelize
      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/belongs_to_property.rb
langalex-couch_potato-0.2.1 lib/couch_potato/persistence/belongs_to_property.rb
langalex-couch_potato-0.2.2 lib/couch_potato/persistence/belongs_to_property.rb
speedmax-couch_potato-0.2.0 lib/couch_potato/persistence/belongs_to_property.rb
speedmax-couch_potato-0.2.2 lib/couch_potato/persistence/belongs_to_property.rb