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



50
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
91
92
# File 'lib/simply_stored/couch/belongs_to.rb', line 50

def initialize(owner_clazz, name, options = {})
  @name = name
  @options = {
    :class_name => name.to_s.singularize.camelize
  }.update(options)

  @options.assert_valid_keys(:class_name)

  owner_clazz.class_eval do
    attr_accessor "#{name}_id_was"
    property "#{name}_id"
    
    define_method name do |*args|
      local_options = args.last.is_a?(Hash) ? args.last : {}
      local_options.assert_valid_keys(:force_reload, :with_deleted)
      forced_reload = local_options[:force_reload] || false
      with_deleted = local_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(self.class._find_property(name).options[:class_name]).find(send("#{name}_id"), :with_deleted => with_deleted) : nil)
    end
  
    define_method "#{name}=" do |value|
      klass = self.class.get_class_from_name(self.class._find_property(name).options[:class_name])
      raise ArgumentError, "expected #{klass} got #{value.class}" unless value.nil? || value.is_a?(klass)

      new_foreign_id = value.nil? ? nil : value.id

      send("#{name}_id=", new_foreign_id)
      instance_variable_set("@#{name}", value)
    end

    define_method "#{name}_id=" do |new_foreign_id|
      super(new_foreign_id)
      value = instance_variable_get("@#{name}")
      remove_instance_variable("@#{name}") if instance_variable_defined?("@#{name}")
    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.



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

def name
  @name
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#association?Boolean



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

def association?
  true
end

#build(object, json) ⇒ Object



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

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

#dirty?(object) ⇒ Boolean



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

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

#reset_dirty_attribute(object) ⇒ Object



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

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

#serialize(json, object) ⇒ Object Also known as: value



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

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