Class: Light::Model

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming, ActiveModel::Translation, ActiveSupport::Concern
Includes:
ActiveModel::Conversion, ActiveModel::Serialization, ActiveModel::Serializers::JSON, ActiveModel::Validations
Defined in:
lib/light/model.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Model

Returns a new instance of Model.



37
38
39
40
41
42
# File 'lib/light/model.rb', line 37

def initialize(params = {})
  return unless params
  params.each do |attr, value|
    public_send("#{attr}=", value)
  end
end

Class Method Details

.attributes(*attrs) ⇒ Object



13
14
15
16
17
18
# File 'lib/light/model.rb', line 13

def attributes(*attrs)
  attrs.each do |attr|
    send(:attr_accessor, attr)
  end
  @__attributes = (@__attributes || []) + attrs
end

Instance Method Details

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



27
28
29
# File 'lib/light/model.rb', line 27

def ==(other)
  other.class == self.class && other.equality_state == equality_state
end

#equality_stateObject



21
22
23
24
25
# File 'lib/light/model.rb', line 21

def equality_state
  self.class.instance_variable_get(:@__attributes).map do |attr|
    public_send(attr.to_s)
  end
end

#hashObject



33
34
35
# File 'lib/light/model.rb', line 33

def hash
  equality_state.hash
end

#persisted?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/light/model.rb', line 48

def persisted?
  false
end

#to_h(opts = nil) ⇒ Object



44
45
46
# File 'lib/light/model.rb', line 44

def to_h(opts = nil)
  serializable_hash(opts)
end