Class: Definition::Types::Keys
- Includes:
- Dsl
- Defined in:
- lib/definition/types/keys.rb
Defined Under Namespace
Modules: Dsl
Instance Attribute Summary collapse
-
#defaults ⇒ Object
Returns the value of attribute defaults.
-
#ignore_extra_keys ⇒ Object
Returns the value of attribute ignore_extra_keys.
-
#optional_definitions ⇒ Object
Returns the value of attribute optional_definitions.
-
#required_definitions ⇒ Object
Returns the value of attribute required_definitions.
Attributes inherited from Base
Instance Method Summary collapse
-
#conform(input_value) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity.
-
#initialize(name, req: {}, opt: {}, defaults: {}, options: {}) ⇒ Keys
constructor
A new instance of Keys.
- #initialize_dup(_other) ⇒ Object
- #keys ⇒ Object
Methods included from Dsl
#include, #option, #optional, #required
Methods inherited from Base
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 = .fetch(:ignore_extra_keys, false) end |
Instance Attribute Details
#defaults ⇒ Object
Returns the value of attribute defaults.
56 57 58 |
# File 'lib/definition/types/keys.rb', line 56 def defaults @defaults end |
#ignore_extra_keys ⇒ Object
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_definitions ⇒ Object
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_definitions ⇒ Object
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
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 109 110 111 112 113 114 115 |
# File 'lib/definition/types/keys.rb', line 73 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 |
#initialize_dup(_other) ⇒ Object
66 67 68 69 70 71 |
# File 'lib/definition/types/keys.rb', line 66 def initialize_dup(_other) super self.required_definitions = required_definitions.dup self.optional_definitions = optional_definitions.dup self.defaults = defaults.dup end |
#keys ⇒ Object
117 118 119 |
# File 'lib/definition/types/keys.rb', line 117 def keys required_definitions.keys + optional_definitions.keys end |