class Couchbase::Model
Attributes
type[RW]
Public Class Methods
belongs_to(name, options = {})
click to toggle source
Current master (072262e) version of belongs to
support passing a class which is not supported in the current 0.5.3 but already in master
-
added suppport for assigning to
belongs_to
# File lib/orm_adapter-couchbase/ext/couchbase_model_patches.rb, line 13 def self.belongs_to(name, options = {}) ref = "#{name}_id" attribute(ref) assoc = (options[:class_name] || name).to_s.camelize.constantize define_method(name) do assoc.find(self.send(ref)) end define_method("#{name}=") do |other| raise TypeError unless other.is_a? assoc self.send("#{ref}=", other.id) end end
has_many(name, options = {})
click to toggle source
define a has_many
relationship, this is comprise of a view and and array with referential keys
# File lib/orm_adapter-couchbase/ext/couchbase_model_has_many.rb, line 8 def self.has_many(name, options = {}) assoc_name = name.to_s.singularize ref = "#{assoc_name}_ids" attribute(ref, :default => []) assoc = (options[:wrapper_class] || assoc_name).to_s.camelize.constantize define_method("#{name}=") do |others| raise TypeError unless others.all? { |o| o.is_a? assoc } self.send("#{ref}=", others.map(&:id)) end define_method(name) do assoc.find(self.send(ref)) end end
Public Instance Methods
==(other)
click to toggle source
define the equality as id equality this should be the default but it isn’t …
TODO file a bug about this!
# File lib/orm_adapter-couchbase/ext/couchbase_model_equality.rb, line 9 def == other id == other.id end
update_attributes(attrs)
click to toggle source
updating attributes should fail for invalid attributes, not simply ignore them
# File lib/orm_adapter-couchbase/ext/couchbase_model_patches.rb, line 30 def update_attributes(attrs) if id = attrs.delete(:id) @id = id end attrs.each do |key, value| setter = :"#{key}=" send(setter, value) end end