Class: SimplyStored::Couch::HasAndBelongsToMany::Property

Inherits:
AssociationProperty show all
Defined in:
lib/simply_stored/couch/has_and_belongs_to_many.rb

Instance Attribute Summary

Attributes inherited from AssociationProperty

#name, #options

Instance Method Summary collapse

Methods inherited from AssociationProperty

#association?, #build, #dirty?, #serialize, #supports_dirty?

Constructor Details

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

Returns a new instance of Property.



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/simply_stored/couch/has_and_belongs_to_many.rb', line 166

def initialize(owner_clazz, name, options = {})
  options = {
    :storing_keys => false,
    :class_name => name.to_s.singularize.camelize,
    :foreign_key => nil,
  }.update(options)

  # there is only one pair of foreign_keys and it usualy the name of the class not storing the keys
  if options[:foreign_key].blank?
    if options[:storing_keys]
      options[:foreign_key] = options[:class_name].singularize.underscore.foreign_key.pluralize
    else
      options[:foreign_key] = owner_clazz.name.singularize.underscore.foreign_key.pluralize
    end
  end
  options[:class_storing_keys] = options[:storing_keys] ? owner_clazz.name : options[:class_name]
  @name, @options = name, options

  options.assert_valid_keys(:class_name, :foreign_key, :storing_keys, :class_storing_keys)

  owner_clazz.class_eval do
    _define_cache_accessors(name, options)
    define_has_and_belongs_to_many_property(options[:foreign_key]) if options[:storing_keys]
    define_has_and_belongs_to_many_views(name, options)
    define_has_and_belongs_to_many_getter(name, options)
    define_has_and_belongs_to_many_setter_add(name, options)
    define_has_and_belongs_to_many_setter_remove(name, options)
    define_has_and_belongs_to_many_setter_remove_all(name, options)
    define_has_and_belongs_to_many_count(name, options)
    define_has_and_belongs_to_many_after_destroy_cleanup(name, options)
  end
end