Class: Definition::Types::Keys

Inherits:
Base
  • Object
show all
Includes:
Dsl
Defined in:
lib/definition/types/keys.rb

Defined Under Namespace

Modules: Dsl

Instance Attribute Summary collapse

Attributes inherited from Base

#context, #name

Instance Method Summary collapse

Methods included from Dsl

#include, #option, #optional, #required

Methods inherited from Base

#error_renderer, #explain

Constructor Details

#initialize(name, req: {}, opt: {}, defaults: {}, options: {}) ⇒ Keys

Returns a new instance of Keys.



58
59
60
61
62
63
64
# File 'lib/definition/types/keys.rb', line 58

def initialize(name, req: {}, opt: {}, defaults: {}, options: {})
  super(name)
  self.required_definitions = req
  self.optional_definitions = opt
  self.defaults = defaults
  self.ignore_extra_keys = options.fetch(:ignore_extra_keys, false)
end

Instance Attribute Details

#defaultsObject

Returns the value of attribute defaults.



56
57
58
# File 'lib/definition/types/keys.rb', line 56

def defaults
  @defaults
end

#ignore_extra_keysObject

Returns the value of attribute ignore_extra_keys.



56
57
58
# File 'lib/definition/types/keys.rb', line 56

def ignore_extra_keys
  @ignore_extra_keys
end

#optional_definitionsObject

Returns the value of attribute optional_definitions.



56
57
58
# File 'lib/definition/types/keys.rb', line 56

def optional_definitions
  @optional_definitions
end

#required_definitionsObject

Returns the value of attribute required_definitions.



56
57
58
# File 'lib/definition/types/keys.rb', line 56

def required_definitions
  @required_definitions
end

Instance Method Details

#conform(input_value) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/definition/types/keys.rb', line 66

def conform(input_value) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
  # input_value is duplicated because we don't want to modify the user object that is passed into this function.
  # The following logic will iterate over each definition and delete the key associated with the definition from
  # the input value.
  # In the end if there are still keys on the object and 'ignore_extra_keys' is false, an error will be raised.
  value = input_value.dup
  result_value = {}
  errors = []

  return wrong_type_result(value) unless value.is_a?(Hash)

  required_definitions.each do |key, definition|
    if value.key?(key)
      result = definition.conform(value.delete(key))
      result_value[key] = result.value
      next if result.passed?

      errors.push(key_error(definition, key, result))
    else
      errors << missing_key_error(key)
    end
  end

  optional_definitions.each do |key, definition|
    if value.key?(key)
      result = definition.conform(value.delete(key))
      result_value[key] = result.value
      next if result.passed?

      errors.push(key_error(definition, key, result))
    elsif defaults.key?(key)
      result_value[key] = defaults.fetch(key)
    end
  end

  if !ignore_extra_keys && !value.keys.empty?
    value.keys.each do |key|
      errors << extra_key_error(key)
    end
  end

  ConformResult.new(result_value, errors: errors)
end

#keysObject



110
111
112
# File 'lib/definition/types/keys.rb', line 110

def keys
  required_definitions.keys + optional_definitions.keys
end