Module: Chassis::Persistence

Defined in:
lib/chassis/persistence.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/chassis/persistence.rb', line 20

def included(base)
  base.class_eval do
    include Initializable
    #include Equalizer.new(:id)

    # Compare the object with other object for equality
    #
    # @example
    #   object.eql?(other)  # => true or false
    #
    # @param [Object] other
    #   the other object to compare with
    #
    # @return [Boolean]
    #
    # @api public
    def eql?(other)
      instance_of?(other.class) && id.eql?(other.id)
    end

    # Compare the object with other object for equivalency
    #
    # @example
    #   object == other  # => true or false
    #
    # @param [Object] other
    #   the other object to compare with
    #
    # @return [Boolean]
    #
    # @api public
    def ==(other)
      other = coerce(other).first if respond_to?(:coerce, true)
      other.kind_of?(self.class) && id == other.id
    end

    def hash
      id.hash
    end

    attr_accessor :id
  end

  base.extend ClassMethods
end

Instance Method Details

#deleteObject



72
73
74
# File 'lib/chassis/persistence.rb', line 72

def delete
  repo.delete self
end

#new_record?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/chassis/persistence.rb', line 76

def new_record?
  id.nil?
end

#repoObject



80
81
82
# File 'lib/chassis/persistence.rb', line 80

def repo
  self.class.repo
end

#save {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



67
68
69
70
# File 'lib/chassis/persistence.rb', line 67

def save
  yield self if block_given?
  repo.save self
end