Module: ActiveAdmin::GraphQL::KeyValuePairs

Defined in:
lib/active_admin/graphql/key_value_pair_input.rb

Class Method Summary collapse

Class Method Details

.extract_from_hash(entry) ⇒ Object

Raises:

  • (::GraphQL::ExecutionError)


48
49
50
51
52
53
# File 'lib/active_admin/graphql/key_value_pair_input.rb', line 48

def extract_from_hash(entry)
  key = entry["key"] || entry[:key]
  raise ::GraphQL::ExecutionError, "key/value pair missing key" if key.nil?

  [key, entry["value"] || entry[:value]]
end

.extract_from_struct(entry) ⇒ Object



44
45
46
# File 'lib/active_admin/graphql/key_value_pair_input.rb', line 44

def extract_from_struct(entry)
  [entry.key, entry.value]
end

.extract_pair(entry) ⇒ Object

Raises:

  • (::GraphQL::ExecutionError)


33
34
35
36
37
38
# File 'lib/active_admin/graphql/key_value_pair_input.rb', line 33

def extract_pair(entry)
  return extract_from_struct(entry) if key_value_struct?(entry)
  return extract_from_hash(entry) if entry.is_a?(Hash)

  raise ::GraphQL::ExecutionError, "invalid key/value pair entry: #{entry.class}"
end

.key_value_struct?(entry) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/active_admin/graphql/key_value_pair_input.rb', line 40

def key_value_struct?(entry)
  entry.respond_to?(:key) && entry.respond_to?(:value) && !entry.is_a?(Hash)
end

.to_hash(entries) ⇒ Hash<String, String>

Parameters:

  • entries (nil, Array<#key, #value>, Array<Hash>)

Returns:

  • (Hash<String, String>)


20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/active_admin/graphql/key_value_pair_input.rb', line 20

def to_hash(entries)
  return {} if entries.nil?

  unless entries.is_a?(Array)
    raise ArgumentError, "expected an array of key/value pairs, got #{entries.class}"
  end

  entries.each_with_object({}) do |e, h|
    key, val = extract_pair(e)
    h[key.to_s] = val.to_s
  end
end