Class: SimplyStored::Couch::BelongsTo::Property

Inherits:
Object
  • Object
show all
Defined in:
lib/simply_stored/couch/belongs_to.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Property.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/simply_stored/couch/belongs_to.rb', line 51

def initialize(owner_clazz, name, options = {})
  @name, @options = name, options
  item_class_name = self.item_class_name
  owner_clazz.class_eval do
    attr_reader "#{name}_id"
    attr_accessor "#{name}_id_was"
    property "#{name}_id"
    
    define_method name do |*args|
      options = args.last.is_a?(Hash) ? args.last : {}
      options.assert_valid_keys(:force_reload, :with_deleted)
      forced_reload = options[:force_reload] || false
      with_deleted = options[:with_deleted] || false
      
      return instance_variable_get("@#{name}") unless instance_variable_get("@#{name}").nil? or forced_reload
      instance_variable_set("@#{name}", send("#{name}_id").present? ? Object.const_get(item_class_name).find(send("#{name}_id"), :with_deleted => with_deleted) : nil)
    end
  
    define_method "#{name}=" do |value|
      klass = self.class.get_class_from_name(name)
      raise ArgumentError, "expected #{klass} got #{value.class}" unless value.nil? || value.is_a?(klass)

      instance_variable_set("@#{name}", value)
      if value.nil?
        instance_variable_set("@#{name}_id", nil)
      else
        instance_variable_set("@#{name}_id", value.id)
      end
    end

    define_method "#{name}_id=" do |id|
      remove_instance_variable("@#{name}") if instance_variable_defined?("@#{name}")
      instance_variable_set("@#{name}_id", id)
    end
    
    define_method "#{name}_changed?" do
      self.send("#{name}_id") != self.send("#{name}_id_was")
    end
  end
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



49
50
51
# File 'lib/simply_stored/couch/belongs_to.rb', line 49

def name
  @name
end

#optionsObject

Returns the value of attribute options.



49
50
51
# File 'lib/simply_stored/couch/belongs_to.rb', line 49

def options
  @options
end

Instance Method Details

#association?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/simply_stored/couch/belongs_to.rb', line 120

def association?
  true
end

#build(object, json) ⇒ Object



107
108
109
110
# File 'lib/simply_stored/couch/belongs_to.rb', line 107

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

#destroy(object) ⇒ Object



103
104
105
# File 'lib/simply_stored/couch/belongs_to.rb', line 103

def destroy(object)

end

#dirty?(object) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/simply_stored/couch/belongs_to.rb', line 95

def dirty?(object)
  object.send("#{name}_id_was") != object.send("#{name}_id")
end

#item_class_nameObject



116
117
118
# File 'lib/simply_stored/couch/belongs_to.rb', line 116

def item_class_name
  @name.to_s.camelize
end

#reset_dirty_attribute(object) ⇒ Object



99
100
101
# File 'lib/simply_stored/couch/belongs_to.rb', line 99

def reset_dirty_attribute(object)
  object.send("#{name}_id_was=", object.send("#{name}_id"))
end

#save(object) ⇒ Object



92
93
# File 'lib/simply_stored/couch/belongs_to.rb', line 92

def save(object)
end

#serialize(json, object) ⇒ Object



112
113
114
# File 'lib/simply_stored/couch/belongs_to.rb', line 112

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