Class: Fried::Schema::AttributesToHash

Inherits:
Object
  • Object
show all
Includes:
Typings
Defined in:
lib/fried/schema/attributes_to_hash.rb

Overview

Converts all attributes into a Hash of name => value. It calls #to_h on each value that is a DataEntity

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAttributesToHash

Returns a new instance of AttributesToHash.



15
16
17
# File 'lib/fried/schema/attributes_to_hash.rb', line 15

def initialize
  self.get_attribute = GetAttribute.new
end

Instance Attribute Details

#get_attributeObject

Returns the value of attribute get_attribute.



13
14
15
# File 'lib/fried/schema/attributes_to_hash.rb', line 13

def get_attribute
  @get_attribute
end

Class Method Details

.buildObject



19
20
21
22
23
# File 'lib/fried/schema/attributes_to_hash.rb', line 19

def self.build
  new.tap do |instance|
    instance.get_attribute = GetAttribute.build
  end
end

.call(schema, obj) ⇒ Object



25
26
27
28
# File 'lib/fried/schema/attributes_to_hash.rb', line 25

def self.call(schema, obj)
  instance = build
  instance.(schema, obj)
end

Instance Method Details

#call(schema, obj) ⇒ Hash{Symbol => Object}

Returns hash of attributes key => values.

Parameters:

Returns:

  • (Hash{Symbol => Object})

    hash of attributes key => values



34
35
36
37
38
39
40
# File 'lib/fried/schema/attributes_to_hash.rb', line 34

def call(schema, obj)
  schema.each_attribute.inject({}) do |hash, attribute|
    value = get_attribute.(obj, attribute)
    hash[attribute.name] = value_to_h(value)
    hash
  end
end