Class: Paramore::CastParameters
- Inherits:
-
Object
- Object
- Paramore::CastParameters
- Defined in:
- lib/paramore/cast_parameters.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#options ⇒ Object
Returns the value of attribute options.
-
#root_field ⇒ Object
Returns the value of attribute root_field.
Class Method Summary collapse
Instance Method Summary collapse
- #cast(field, value, key, namespace = nil) ⇒ Object
-
#initialize(field, data, options) ⇒ CastParameters
constructor
A new instance of CastParameters.
- #missing_and_optional?(hash, key, field) ⇒ Boolean
- #run ⇒ Object
- #typecast_array(field, array, key, full_name) ⇒ Object
- #typecast_hash(field, hash, key, full_name) ⇒ Object
- #typecast_value(type, value, key, full_name) ⇒ Object
Constructor Details
#initialize(field, data, options) ⇒ CastParameters
Returns a new instance of CastParameters.
7 8 9 10 11 |
# File 'lib/paramore/cast_parameters.rb', line 7 def initialize(field, data, ) @root_field = field @data = data @options = end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
5 6 7 |
# File 'lib/paramore/cast_parameters.rb', line 5 def data @data end |
#options ⇒ Object
Returns the value of attribute options.
5 6 7 |
# File 'lib/paramore/cast_parameters.rb', line 5 def @options end |
#root_field ⇒ Object
Returns the value of attribute root_field.
5 6 7 |
# File 'lib/paramore/cast_parameters.rb', line 5 def root_field @root_field end |
Class Method Details
.run(field, data, options = {}) ⇒ Object
13 14 15 |
# File 'lib/paramore/cast_parameters.rb', line 13 def self.run(field, data, = {}) new(field, data, ).run end |
Instance Method Details
#cast(field, value, key, namespace = nil) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/paramore/cast_parameters.rb', line 21 def cast(field, value, key, namespace = nil) full_name = [namespace, key].compact.join('.') if value.nil? return field.default if field.nullable? || field.default? raise Paramore::NilParameter, full_name elsif value == '' return typecast_value(field.type, '', key, full_name) if field.allow_empty? return field.default if field.default? return if field.nullable? raise Paramore::NilParameter, full_name end case field.type when Hash typecast_hash(field, value || {}, key, full_name) when Array typecast_array(field, value, key, full_name) else typecast_value(field.type, value, key, full_name) end end |
#missing_and_optional?(hash, key, field) ⇒ Boolean
95 96 97 |
# File 'lib/paramore/cast_parameters.rb', line 95 def missing_and_optional?(hash, key, field) !hash.key?(key) && !field.required? end |
#run ⇒ Object
17 18 19 |
# File 'lib/paramore/cast_parameters.rb', line 17 def run cast(root_field, data, 'root') end |
#typecast_array(field, array, key, full_name) ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'lib/paramore/cast_parameters.rb', line 79 def typecast_array(field, array, key, full_name) raise Paramore::ArrayExpected.new(full_name, array) unless array.is_a?(Array) result = array .reject { |unit| unit.to_s == '' && field.compact? } .map { |unit| cast(Paramore.field(field.type.first, null: true), unit, nil, full_name) } field.compact? ? result.compact : result end |
#typecast_hash(field, hash, key, full_name) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/paramore/cast_parameters.rb', line 46 def typecast_hash(field, hash, key, full_name) raise Paramore::HashExpected.new(full_name, hash) unless hash.is_a?(Hash) result = if field.wildly_keyed_hash? [field.type.values.first, field.type.keys.first].then do |value_field, key_type| hash.to_h do |inner_key, value| [ typecast_value(key_type, inner_key, nil, nil), cast(value_field, value, inner_key, full_name) ] end end else if [:no_extra_keys] extra = hash.keys - field.type.keys raise "Found extra keys in `#{full_name}`: #{extra}!" if extra.any? end field .type .reject { |inner_key, value_field| missing_and_optional?(hash, inner_key, value_field) } .to_h do |inner_key, value_field| [ inner_key, cast(value_field, hash[inner_key], inner_key, full_name) ] end end field.compact? ? result.compact : result end |
#typecast_value(type, value, key, full_name) ⇒ Object
89 90 91 92 93 |
# File 'lib/paramore/cast_parameters.rb', line 89 def typecast_value(type, value, key, full_name) type.send(Paramore.configuration.type_method_name, value) rescue StandardError => e raise "The field `#{full_name}` raised \"#{e}\"!" end |