Module: Id::Model

Defined in:
lib/id/model.rb

Instance Method Summary collapse

Instance Method Details

#dataObject Also known as: to_hash



29
30
31
# File 'lib/id/model.rb', line 29

def data
  @data ||= Id::Hashifier.enhash(_data)
end

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

Returns:



20
21
22
# File 'lib/id/model.rb', line 20

def eql? other
  other.is_a?(Id::Model) && other._data.eql?(self._data)
end

#hashObject



25
26
27
# File 'lib/id/model.rb', line 25

def hash
  _data.hash
end

#initialize(_data = {}) ⇒ Object



3
4
5
6
7
# File 'lib/id/model.rb', line 3

def initialize(_data = {})
  @_data = fields.reduce({}) do |acc, (_, field)|
    acc.merge(field.key => field.value(_data))
  end
end

#set(update) ⇒ Object



9
10
11
12
# File 'lib/id/model.rb', line 9

def set(update)
  updated = _data.merge(update.stringify_keys)
  self.class.new(updated)
end

#unset(*fields) ⇒ Object



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

def unset(*fields)
  fields  = fields.map(&:to_s)
  updated = _data.except(*fields)
  self.class.new(updated)
end