Class: Paramore::CastParameters

Inherits:
Object
  • Object
show all
Defined in:
lib/paramore/cast_parameters.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, options)
  @root_field = field
  @data = data
  @options = options
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



5
6
7
# File 'lib/paramore/cast_parameters.rb', line 5

def data
  @data
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/paramore/cast_parameters.rb', line 5

def options
  @options
end

#root_fieldObject

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, options = {})
  new(field, data, options).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].select(&:present?).join('.')

  if value.nil?
    return field.default if field.nullable? || field.default?

    raise Paramore::NilParameter, full_name
  elsif value == ''
    return typecast_value(field.type, '', 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 || {}, full_name)
  when Array
    typecast_array(field, value, full_name)
  else
    typecast_value(field.type, value, full_name)
  end
end

#cast_regular_hash(field, hash, full_name) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/paramore/cast_parameters.rb', line 88

def cast_regular_hash(field, hash, full_name)
  if options[:no_extra_keys]
    extra = hash.keys - field.type.keys
    raise "Found extra keys in #{Paramore::ErrorFieldName(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

#cast_wild_hash(field, hash, full_name) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/paramore/cast_parameters.rb', line 76

def cast_wild_hash(field, hash, full_name)
  value_field = field.type.values.first
  key_type = field.type.keys.first

  hash.to_h do |inner_key, value|
    [
      typecast_value(key_type, inner_key, nil),
      cast(value_field, value, inner_key, full_name)
    ]
  end
end

#missing_and_optional?(hash, key, field) ⇒ Boolean

Returns:



102
103
104
# File 'lib/paramore/cast_parameters.rb', line 102

def missing_and_optional?(hash, key, field)
  !hash.key?(key) && !field.required?
end

#runObject



17
18
19
# File 'lib/paramore/cast_parameters.rb', line 17

def run
  cast(root_field, data, nil)
end

#typecast_array(field, array, full_name) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/paramore/cast_parameters.rb', line 59

def typecast_array(field, array, 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, full_name) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/paramore/cast_parameters.rb', line 46

def typecast_hash(field, hash, full_name)
  raise Paramore::HashExpected.new(full_name, hash) unless hash.is_a?(Hash)

  result =
    if field.wildly_keyed_hash?
      cast_wild_hash(field, hash, full_name)
    else
      cast_regular_hash(field, hash, full_name)
    end

  field.compact? ? result.compact : result
end

#typecast_value(type, value, full_name) ⇒ Object



70
71
72
73
74
# File 'lib/paramore/cast_parameters.rb', line 70

def typecast_value(type, value, full_name)
  type.send(Paramore.configuration.type_method_name, value)
rescue StandardError => e
  raise "Tried casting #{Paramore::ErrorFieldName(full_name)}, but \"#{e}\" was raised!"
end