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, name = nil) ⇒ Object
-
#initialize(field, data, options) ⇒ CastParameters
constructor
A new instance of CastParameters.
- #missing_and_optional?(hash, name, field) ⇒ Boolean
- #run ⇒ Object
- #typecast_array(field, array, name) ⇒ Object
- #typecast_hash(field, hash, name) ⇒ Object
- #typecast_value(type, value, name) ⇒ Object
Constructor Details
#initialize(field, data, options) ⇒ CastParameters
7 8 9 10 11 |
# File 'lib/paramore/cast_parameters.rb', line 7 def initialize(field, data, ) @root_field = field @data = data = 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 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, name = 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 45 46 47 48 |
# File 'lib/paramore/cast_parameters.rb', line 21 def cast(field, value, name = nil) if value.nil? if field.nullable? || field.default? return field.default else raise Paramore::NilParameter, name end elsif value == '' if field.allow_empty? return typecast_value(field.type, '', name) elsif field.default? return field.default elsif field.nullable? return else raise Paramore::NilParameter, name end end case field.type when Hash typecast_hash(field, value || {}, name) when Array typecast_array(field, value, name) else typecast_value(field.type, value, name) end end |
#missing_and_optional?(hash, name, field) ⇒ Boolean
91 92 93 |
# File 'lib/paramore/cast_parameters.rb', line 91 def missing_and_optional?(hash, name, field) !hash.key?(name) && !field.required? end |
#run ⇒ Object
17 18 19 |
# File 'lib/paramore/cast_parameters.rb', line 17 def run cast(root_field, data, 'data') end |
#typecast_array(field, array, name) ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'lib/paramore/cast_parameters.rb', line 75 def typecast_array(field, array, name) raise Paramore::ArrayExpected.new(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) } field.compact? ? result.compact : result end |
#typecast_hash(field, hash, name) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/paramore/cast_parameters.rb', line 50 def typecast_hash(field, hash, name) raise Paramore::HashExpected.new(name, hash) unless hash.is_a?(Hash) result = if field.wildly_keyed_hash? value_field = field.type.values.first key_type = field.type.keys.first hash.to_h do |name, value| [typecast_value(key_type, name, nil), cast(value_field, value, name)] end else if [:no_extra_keys] extra = hash.keys - field.type.keys raise "Found extra keys in `#{name}`: #{extra}!" if extra.size > 0 end field .type .reject { |name, value_field| missing_and_optional?(hash, name, value_field) } .to_h { |name, value_field| [name, cast(value_field, hash[name], name)] } end field.compact? ? result.compact : result end |
#typecast_value(type, value, name) ⇒ Object
85 86 87 88 89 |
# File 'lib/paramore/cast_parameters.rb', line 85 def typecast_value(type, value, name) type.send(Paramore.configuration.type_method_name, value) rescue StandardError => e raise "The field `#{name}` raised \"#{e}\"!" end |