Class: Dryer::Factories::BuildFromContract

Inherits:
Services::SimpleService
  • Object
show all
Defined in:
lib/dryer/factories/build_from_contract.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(contract) ⇒ BuildFromContract

Returns a new instance of BuildFromContract.



9
10
11
12
# File 'lib/dryer/factories/build_from_contract.rb', line 9

def initialize(contract)
  @contract = contract
  check_contract_type!
end

Instance Attribute Details

#contractObject (readonly)

Returns the value of attribute contract.



37
38
39
# File 'lib/dryer/factories/build_from_contract.rb', line 37

def contract
  @contract
end

Instance Method Details

#callObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/dryer/factories/build_from_contract.rb', line 22

def call
  gen_object = Object.new
  contract.schema.types.inject({}) do |fields, (field_name, field_type)|
    fields.merge(Fields::Build.call(
      name: field_name.to_sym,
      type: field_type
    ))
  end.then do |fields|
    GeneratedPayload.new(
      contract: contract,
      fields: fields
    )
  end
end

#check_contract_type!Object



14
15
16
17
18
19
20
# File 'lib/dryer/factories/build_from_contract.rb', line 14

def check_contract_type!
  raise(
    "#{self.class}.call: Argument must be an instance "+
    "or subclass of Dry::Validation::Contract"
  ) unless contract.respond_to?(:ancestors) &&
    contract.ancestors.include?(Dry::Validation::Contract)
end