Class: Camille::Types::Object
- Defined in:
- lib/camille/types/object.rb
Defined Under Namespace
Classes: ArgumentError
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#optional_keys ⇒ Object
readonly
Returns the value of attribute optional_keys.
Attributes inherited from BasicType
Instance Method Summary collapse
- #check(value) ⇒ Object
- #check_params(value) ⇒ Object
-
#initialize(fields) ⇒ Object
constructor
A new instance of Object.
- #literal ⇒ Object
Methods inherited from BasicType
&, #&, #[], [], check_params, directly_instantiable?, inherited, instance, |, #|
Constructor Details
#initialize(fields) ⇒ Object
Returns a new instance of Object.
7 8 9 10 11 |
# File 'lib/camille/types/object.rb', line 7 def initialize fields @optional_keys = [] @fields = normalize_fields fields @fingerprint = generate_fingerprint end |
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
5 6 7 |
# File 'lib/camille/types/object.rb', line 5 def fields @fields end |
#optional_keys ⇒ Object (readonly)
Returns the value of attribute optional_keys.
5 6 7 |
# File 'lib/camille/types/object.rb', line 5 def optional_keys @optional_keys end |
Instance Method Details
#check(value) ⇒ Object
13 14 15 16 17 18 19 20 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/camille/types/object.rb', line 13 def check value if value.is_a? Hash keys = (@fields.keys + value.keys).uniq keys_to_check, keys_to_skip = keys.partition{|key| @fields[key]} results = keys_to_check.map do |key| type = @fields[key] if @optional_keys.include?(key) && value[key].nil? nil else [key, type.check(value[key])] end end.compact errors = results.map do |key, result| if result.type_error? [key.to_s, result] else nil end end.compact skipped_pairs = keys_to_skip.map do |key| [key, value[key]] end if errors.empty? object = Camille::ObjectHash[results.map{|key, checked| [key, checked.value]}.concat(skipped_pairs).to_h] Camille::Checked.new(fingerprint, object) else Camille::TypeError.new(**errors.to_h) end else Camille::TypeError.new("Expected hash, got #{value.inspect}.") end end |
#check_params(value) ⇒ 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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/camille/types/object.rb', line 50 def check_params value if value.is_a? Hash # Convert camelCase keys to snake_case converted_value = value.transform_keys do |key| Camille::KeyConverter.convert_params_key(key.to_s) end fields_with_string_keys = @fields.transform_keys(&:to_s) optional_keys_as_strings = @optional_keys.map(&:to_s) # Now check using the regular check method which expects snake_case keys = (fields_with_string_keys.keys + converted_value.keys).uniq keys_to_check, keys_to_skip = keys.partition{|key| fields_with_string_keys[key]} results = keys_to_check.map do |key| type = fields_with_string_keys[key] if optional_keys_as_strings.include?(key) && converted_value[key].nil? nil else [key, type.check_params(converted_value[key])] end end.compact errors = results.map do |key, result| if result.type_error? [key.to_s, result] else nil end end.compact skipped_pairs = keys_to_skip.map do |key| [key, converted_value[key]] end if errors.empty? object = Hash[results.map{|key, checked| [key, checked.value]}.concat(skipped_pairs).to_h] Camille::Checked.new(fingerprint, object) else Camille::TypeError.new(**errors.to_h) end else Camille::TypeError.new("Expected hash, got #{value.inspect}.") end end |
#literal ⇒ Object
96 97 98 |
# File 'lib/camille/types/object.rb', line 96 def literal "{#{@fields.map{|k,v| "#{literal_key k}: #{v.literal}"}.join(', ')}}" end |