Class: CouchPotato::Persistence::BelongsToProperty

Inherits:
Object
  • Object
show all
Defined in:
lib/couch_potato/persistence/belongs_to_property.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner_clazz, name, options = {}) ⇒ BelongsToProperty

Returns a new instance of BelongsToProperty.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/couch_potato/persistence/belongs_to_property.rb', line 6

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

Instance Attribute Details

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/couch_potato/persistence/belongs_to_property.rb', line 4

def name
  @name
end

Instance Method Details

#build(object, json) ⇒ Object



44
45
46
# File 'lib/couch_potato/persistence/belongs_to_property.rb', line 44

def build(object, json)
  object.send "#{name}_id=", json["#{name}_id"]
end

#destroy(object) ⇒ Object



40
41
42
# File 'lib/couch_potato/persistence/belongs_to_property.rb', line 40

def destroy(object)
  
end

#dirty?(object) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/couch_potato/persistence/belongs_to_property.rb', line 36

def dirty?(object)
  false
end

#item_class_nameObject



52
53
54
# File 'lib/couch_potato/persistence/belongs_to_property.rb', line 52

def item_class_name
  @name.to_s.camelize
end

#save(object) ⇒ Object



32
33
34
# File 'lib/couch_potato/persistence/belongs_to_property.rb', line 32

def save(object)
  
end

#serialize(json, object) ⇒ Object



48
49
50
# File 'lib/couch_potato/persistence/belongs_to_property.rb', line 48

def serialize(json, object)
  json["#{name}_id"] = object.send("#{name}_id") if object.send("#{name}_id")
end