Sha256: 03e9a5eb58a307ec061e3a520daec9be87fceb6109cc65c227beb608310178e2

Contents?: true

Size: 1.37 KB

Versions: 11

Compression:

Stored size: 1.37 KB

Contents

module CouchPotato
  module Persistence
    class BelongsToProperty #:nodoc:
      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

11 entries across 11 versions & 3 rubygems

Version Path
andrewtimberlake-couch_potato-0.2.8.1 lib/couch_potato/persistence/belongs_to_property.rb
andrewtimberlake-couch_potato-0.2.8.2 lib/couch_potato/persistence/belongs_to_property.rb
andrewtimberlake-couch_potato-0.2.8.3 lib/couch_potato/persistence/belongs_to_property.rb
andrewtimberlake-couch_potato-0.2.8.4 lib/couch_potato/persistence/belongs_to_property.rb
langalex-couch_potato-0.2.3 lib/couch_potato/persistence/belongs_to_property.rb
langalex-couch_potato-0.2.4 lib/couch_potato/persistence/belongs_to_property.rb
langalex-couch_potato-0.2.5 lib/couch_potato/persistence/belongs_to_property.rb
langalex-couch_potato-0.2.6 lib/couch_potato/persistence/belongs_to_property.rb
langalex-couch_potato-0.2.7 lib/couch_potato/persistence/belongs_to_property.rb
langalex-couch_potato-0.2.8 lib/couch_potato/persistence/belongs_to_property.rb
thefool808-couch_potato-0.2.7 lib/couch_potato/persistence/belongs_to_property.rb