class Fried::Schema::AttributesToHash

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

Attributes

get_attribute[RW]

Public Class Methods

build() click to toggle source
# 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) click to toggle source
# File lib/fried/schema/attributes_to_hash.rb, line 25
def self.call(schema, obj)
  instance = build
  instance.(schema, obj)
end
new() click to toggle source
# File lib/fried/schema/attributes_to_hash.rb, line 15
def initialize
  self.get_attribute = GetAttribute.new
end

Public Instance Methods

call(schema, obj) click to toggle source

@param schema [Definition] @param obj [::Fried::Schema::Struct] an instance of a class including

{::Fried::Schema::Struct}

@return [Hash{Symbol => Object}] hash of attributes key => values

# 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

Private Instance Methods

value_to_h(value) click to toggle source
# File lib/fried/schema/attributes_to_hash.rb, line 44
def value_to_h(value)
  return value unless Is[DataEntity].valid?(value)

  schema = GetDefinition.(value.class)
  call(schema, value)
end