Sha256: fecf63c2e2cab0a42b4204e67e2fe5e4e6a9fca8d7bdd433ea42599728252691

Contents?: true

Size: 995 Bytes

Versions: 16

Compression:

Stored size: 995 Bytes

Contents

module GraphQL
  module Define
    # Create a hash of definitions out of provided arguments.
    #
    # @example Create definitions (some default, some custom)
    #    hash = AssignmentDictionary.create(:name, :description, field: (value, field_name) -> { value.create_field(field) })
    #
    module AssignmentDictionary
      # Turn `keys` into a hash suitable for {GraphQL::Define::InstanceDefinable}
      # @param Any number of symbols for default assignment, followed by an (optional) hash of custom assignment procs.
      # @return [Hash] keys are attributes which may be defined. values are procs which assign values to the target object.
      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
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
graphql-0.18.0 lib/graphql/define/assignment_dictionary.rb
graphql-0.17.2 lib/graphql/define/assignment_dictionary.rb
graphql-0.17.1 lib/graphql/define/assignment_dictionary.rb
graphql-0.17.0 lib/graphql/define/assignment_dictionary.rb
graphql-0.16.1 lib/graphql/define/assignment_dictionary.rb
graphql-0.16.0 lib/graphql/define/assignment_dictionary.rb
graphql-0.15.3 lib/graphql/define/assignment_dictionary.rb
graphql-0.15.2 lib/graphql/define/assignment_dictionary.rb
graphql-0.14.2 lib/graphql/define/assignment_dictionary.rb
graphql-0.15.1 lib/graphql/define/assignment_dictionary.rb
graphql-0.15.0 lib/graphql/define/assignment_dictionary.rb
graphql-0.14.1 lib/graphql/define/assignment_dictionary.rb
graphql-0.14.0 lib/graphql/define/assignment_dictionary.rb
graphql-0.13.0 lib/graphql/define/assignment_dictionary.rb
graphql-0.12.1 lib/graphql/define/assignment_dictionary.rb
graphql-0.12.0 lib/graphql/define/assignment_dictionary.rb