Class: Couchbase::Model

Inherits:
Object
  • Object
show all
Extended by:
OrmAdapter::ToAdapter
Defined in:
lib/orm_adapter-couchbase/couchbase.rb,
lib/orm_adapter-couchbase/ext/couchbase_model_patches.rb,
lib/orm_adapter-couchbase/ext/couchbase_model_equality.rb,
lib/orm_adapter-couchbase/ext/couchbase_model_has_many.rb

Defined Under Namespace

Classes: OrmAdapter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#typeObject

Returns the value of attribute type.



4
5
6
# File 'lib/orm_adapter-couchbase/ext/couchbase_model_patches.rb', line 4

def type
  @type
end

Class Method Details

.belongs_to(name, options = {}) ⇒ Object

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



13
14
15
16
17
18
19
20
21
22
23
24
# 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 = {}) ⇒ Object

define a has_many relationship, this is comprise of a view and and array with referential keys



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 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

Instance Method Details

#==(other) ⇒ Object

define the equality as id equality this should be the default but it isn’t …

TODO file a bug about this!



9
10
11
# File 'lib/orm_adapter-couchbase/ext/couchbase_model_equality.rb', line 9

def == other
  id == other.id
end

#update_attributes(attrs) ⇒ Object

updating attributes should fail for invalid attributes, not simply ignore them



30
31
32
33
34
35
36
37
38
# 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