Module: Couchbase::ActiveModel

Included in:
Model
Defined in:
lib/couchbase/active_model.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/couchbase/active_model.rb', line 4

def self.included(base)
  base.class_eval do
    extend ::ActiveModel::Callbacks
    extend ::ActiveModel::Naming
    include ::ActiveModel::Conversion
    include ::ActiveModel::Validations
    include ::ActiveModel::Validations::Callbacks

    define_model_callbacks :create, :update, :delete, :save, :initialize
    [:save, :create, :update, :delete, :initialize].each do |meth|
      class_eval "        alias \#{meth}_without_callbacks \#{meth}\n        def \#{meth}(*args, &block)\n          run_callbacks(:\#{meth}) do\n            \#{meth}_without_callbacks(*args, &block)\n          end\n        end\n      EOC\n    end\n  end\nend\n"

Instance Method Details

#==(other) ⇒ Object

Public: Overrides == to compare via class and entity id.

other - Another object to compare to

Example

movie = Movie.find(1234)
movie.to_key
# => 'movie-1234'

Returns a string representing the unique key.



64
65
66
# File 'lib/couchbase/active_model.rb', line 64

def ==(other)
  hash == other.hash
end

#eql?(other) ⇒ Boolean

Public: Overrides eql? to use == in the comparison.

other - Another object to compare to

Returns a boolean.

Returns:

  • (Boolean)


49
50
51
# File 'lib/couchbase/active_model.rb', line 49

def eql?(other)
  self == other
end

#hashObject

Public: Hashes our unique key instead of the entire object. Ruby normally hashes an object to be used in comparisons. In our case we may have two techincally different objects referencing the same entity id, so we will hash just the class and id (via to_key) to compare so we get the expected result

Returns a string representing the unique key.



40
41
42
# File 'lib/couchbase/active_model.rb', line 40

def hash
  to_param.hash
end

#to_modelObject

Public: Allows for access to ActiveModel functionality.

Returns self.



29
30
31
# File 'lib/couchbase/active_model.rb', line 29

def to_model
  self
end