Class: KB::BaseModel

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Attributes, ActiveModel::Dirty, ActiveModel::Model, ActiveModel::Serializers::JSON, Inspectionable
Defined in:
lib/kb/models/base_model.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Inspectionable

#inspect

Constructor Details

#initialize(attributes = {}) {|_self| ... } ⇒ BaseModel

Returns a new instance of BaseModel.

Yields:

  • (_self)

Yield Parameters:

  • _self (KB::BaseModel)

    the object that the method was called on



35
36
37
38
39
# File 'lib/kb/models/base_model.rb', line 35

def initialize(attributes = {})
  super
  @persisted = false
  yield self if block_given?
end

Instance Attribute Details

#persistedObject

Returns the value of attribute persisted.



9
10
11
# File 'lib/kb/models/base_model.rb', line 9

def persisted
  @persisted
end

Class Method Details

.define_attribute_methods(*fields) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/kb/models/base_model.rb', line 17

def define_attribute_methods(*fields)
  super
  fields.each do |field|
    define_method :"#{field}=" do |value|
      super(value).tap do
        public_send "#{field}_will_change!" if public_send("#{field}_changed?")
      end
    end
  end
end

.define_attributes(attributes, cast_type = nil) ⇒ Object



28
29
30
31
32
# File 'lib/kb/models/base_model.rb', line 28

def define_attributes(attributes, cast_type = nil)
  attributes.each do |attribute|
    attribute attribute, cast_type&.to_sym
  end
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



52
53
54
55
56
57
# File 'lib/kb/models/base_model.rb', line 52

def ==(other)
  super ||
    (other.instance_of?(self.class) &&
    !key.nil? &&
    other.key == key)
end

#persist!Object



45
46
47
48
# File 'lib/kb/models/base_model.rb', line 45

def persist!
  changes_applied
  @persisted = true
end

#persisted?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/kb/models/base_model.rb', line 41

def persisted?
  @persisted
end