Class: RockRMS::Response::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/rock_rms/response/base.rb

Constant Summary collapse

BASE_MAPPING =
{
  id: 'Id',
  guid: 'Guid',
  created_date_time: 'CreatedDateTime',
  modified_date_time: 'ModifiedDateTime',
  created_by_person_alias_id: 'CreatedByPersonAliasId',
  attributes: 'Attributes',
  attribute_values: 'AttributeValues',
  foreign_key: 'ForeignKey'
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Base

Returns a new instance of Base.



21
22
23
# File 'lib/rock_rms/response/base.rb', line 21

def initialize(data)
  @data = data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



4
5
6
# File 'lib/rock_rms/response/base.rb', line 4

def data
  @data
end

Class Method Details

.format(data) ⇒ Object



17
18
19
# File 'lib/rock_rms/response/base.rb', line 17

def self.format(data)
  new(data).format
end

Instance Method Details

#formatObject



25
26
27
28
29
30
31
# File 'lib/rock_rms/response/base.rb', line 25

def format
  if data.is_a?(Array)
    data.map { |item| format_single(item) }
  else
    format_single(data)
  end
end

#format_attributes(res, klass) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/rock_rms/response/base.rb', line 48

def format_attributes(res, klass)
  return res if res.nil?

  res.each_with_object({}) do |(attr, val), object|
    object[attr] = klass.format(val)
  end
end

#to_h(dict, data) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rock_rms/response/base.rb', line 33

def to_h(dict, data)
  return {} if data.nil?

  dict
    .merge(BASE_MAPPING)
    .each_with_object({}) do |(l, r), object|
      if l == :attributes || l == :attribute_values
        format_klass = l == :attributes ? Attribute : AttributeValue
        object[l] = format_attributes(data[r], format_klass)
      else
        object[l] = data[r]
      end
    end
end