Class: SimplyCouch::Model::BelongsTo::Property

Inherits:
Object
  • Object
show all
Defined in:
lib/simply_couch/model/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.



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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/simply_couch/model/belongs_to.rb', line 63

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

  @options.assert_valid_keys(:class_name)

  owner_clazz.class_eval do
    property :"#{name}_id"
    alias_method :"#{name}_changed?", :"#{name}_id_changed?"

    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

      if send("#{name}_id").present?
        # Try to fetch the object. Does not have to be present. When relation dependency is ignore, the id remains.
        # This will result in a id to a non existent object. Therefore the rescue for object
        object = self.class.get_class_from_name(name).find(send("#{name}_id"), :with_deleted => with_deleted) rescue nil
        instance_variable_set("@#{name}",  object)
      else
        instance_variable_set("@#{name}",  nil)
      end
    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)

      if value
        # Has many object update
        value_has_many_name = klass.properties.find{|p| p.is_a?(SimplyCouch::Model::HasMany::Property) && p.options[:class_name] == self.class.name}.try(:name)
        value.send(value_has_many_name) << self unless !value_has_many_name || value.send(value_has_many_name).include?(self)

        # Has one object update
        value_has_one_name = klass.properties.find{|p| p.is_a?(SimplyCouch::Model::HasOne::Property) && p.options[:class_name] == self.class.name}.try(:name)
        value.instance_variable_set("@#{value_has_one_name}", self) unless !value_has_one_name || value.send(value_has_one_name) == self
      end

      # Mark changed if appropriate
      # send("#{name}_will_change!") if value != instance_variable_get("@#{name}") is not a persisted property, do not mark as changed

      instance_variable_set("@#{name}", value)
      if value.nil?
        send("#{name}_id=", nil)
      else
        send("#{name}_id=", value.id)
      end
    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}") && new_foreign_id != value.try(:id)
    end
  end
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



61
62
63
# File 'lib/simply_couch/model/belongs_to.rb', line 61

def name
  @name
end

#optionsObject

Returns the value of attribute options.



61
62
63
# File 'lib/simply_couch/model/belongs_to.rb', line 61

def options
  @options
end

Instance Method Details

#association?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/simply_couch/model/belongs_to.rb', line 134

def association?
  true
end

#build(object, json) ⇒ Object



125
126
127
# File 'lib/simply_couch/model/belongs_to.rb', line 125

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

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



129
130
131
# File 'lib/simply_couch/model/belongs_to.rb', line 129

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