Module: GraphQL::Define::AssignmentDictionary

Defined in:
lib/graphql/define/assignment_dictionary.rb

Overview

Create a hash of definitions out of provided arguments.

Examples:

Create definitions (some default, some custom)

hash = AssignmentDictionary.create(:name, :description, field: (value, field_name) -> { value.create_field(field) })

Class Method Summary collapse

Class Method Details

.create(*keys) ⇒ Hash

Turn ‘keys` into a hash suitable for InstanceDefinable

Parameters:

  • Any

    number of symbols for default assignment, followed by an (optional) hash of custom assignment procs.

Returns:

  • (Hash)

    keys are attributes which may be defined. values are procs which assign values to the target object.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/graphql/define/assignment_dictionary.rb', line 12

def self.create(*keys)
  initial = if keys.last.is_a?(Hash)
    keys.pop
  else
    {}
  end
  keys.inject(initial) do |memo, key|
    assign_key = "#{key}="
    memo[key] = -> (target, value) { target.public_send(assign_key, value) }
    memo
  end
end