Class: SmartParams::Field
- Inherits:
-
Object
- Object
- SmartParams::Field
- Defined in:
- lib/smart_params/field.rb
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#keychain ⇒ Object
readonly
Returns the value of attribute keychain.
-
#nullable ⇒ Object
readonly
Returns the value of attribute nullable.
-
#subfields ⇒ Object
readonly
Returns the value of attribute subfields.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#allow_empty? ⇒ Boolean
Check if we should consider this value even when empty.
- #claim(raw) ⇒ Object
- #clean? ⇒ Boolean
- #deep? ⇒ Boolean
-
#dirty? ⇒ Boolean
For nullable hashes: Any keys not in the schema make the hash dirty.
- #empty? ⇒ Boolean
-
#initialize(keychain:, type:, key: nil, subschema: false, nullable: false, &nesting) ⇒ Field
constructor
A new instance of Field.
- #inspect ⇒ Object
- #nullable? ⇒ Boolean
-
#removable? ⇒ Boolean
Should this field be removed from resulting hash?.
- #root? ⇒ Boolean
- #specified? ⇒ Boolean
- #to_hash ⇒ Object
- #value ⇒ Object
- #weight ⇒ Object
Constructor Details
#initialize(keychain:, type:, key: nil, subschema: false, nullable: false, &nesting) ⇒ Field
Returns a new instance of Field.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/smart_params/field.rb', line 20 def initialize(keychain:, type:, key: nil, subschema: false, nullable: false, &nesting) @key = key @keychain = Array(keychain) @subfields = Set.new @type = type @nullable = nullable @subschema = subschema @specified = false @dirty = false instance_eval(&nesting) if nesting if subschema @type = @type.schema(subfields.reduce({}) do |mapping, field| mapping.merge("#{field.key}#{'?' if field.nullable}": field.type) end).with_key_transform(&:to_sym) end @type = @type.optional if nullable end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
9 10 11 |
# File 'lib/smart_params/field.rb', line 9 def key @key end |
#keychain ⇒ Object (readonly)
Returns the value of attribute keychain.
5 6 7 |
# File 'lib/smart_params/field.rb', line 5 def keychain @keychain end |
#nullable ⇒ Object (readonly)
Returns the value of attribute nullable.
8 9 10 |
# File 'lib/smart_params/field.rb', line 8 def nullable @nullable end |
#subfields ⇒ Object (readonly)
Returns the value of attribute subfields.
6 7 8 |
# File 'lib/smart_params/field.rb', line 6 def subfields @subfields end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
7 8 9 |
# File 'lib/smart_params/field.rb', line 7 def type @type end |
Instance Method Details
#allow_empty? ⇒ Boolean
Check if we should consider this value even when empty.
83 84 85 86 87 |
# File 'lib/smart_params/field.rb', line 83 def allow_empty? return true if specified? && nullable? subfields.any?(&:allow_empty?) end |
#claim(raw) ⇒ Object
89 90 91 92 93 94 95 96 97 |
# File 'lib/smart_params/field.rb', line 89 def claim(raw) return type[dug(raw)] if deep? @value = type[dug(raw)] rescue Dry::Types::ConstraintError => _constraint_exception raise SmartParams::Error::InvalidPropertyType.new(keychain:, wanted: type, raw: keychain.empty? ? raw : raw.dig(*keychain)) rescue Dry::Types::MissingKeyError => missing_key_exception raise SmartParams::Error::InvalidPropertyType.new(keychain:, wanted: type, raw: keychain.empty? ? raw : raw.dig(*keychain), missing_key: missing_key_exception.key) end |
#clean? ⇒ Boolean
75 76 77 78 79 80 |
# File 'lib/smart_params/field.rb', line 75 def clean? return false if dirty? return true if empty? || subfields.reject(&:empty?).any? false end |
#deep? ⇒ Boolean
40 41 42 43 44 45 46 |
# File 'lib/smart_params/field.rb', line 40 def deep? # We check @specified directly because we want to know if ANY # subfields have been passed, not just ones that match the schema. return false if nullable? && @specified subfields.present? end |
#dirty? ⇒ Boolean
For nullable hashes: Any keys not in the schema make the hash dirty. If a key is found that matches the schema, we can consider the hash clean.
71 72 73 |
# File 'lib/smart_params/field.rb', line 71 def dirty? !!@dirty end |
#empty? ⇒ Boolean
105 106 107 |
# File 'lib/smart_params/field.rb', line 105 def empty? value.nil? end |
#inspect ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/smart_params/field.rb', line 11 def inspect "#<#{self.class.name}:#{__id__} #{[ ('subschema' if @subschema), ("#/#{@keychain.join('/')}" if @keychain), ("-> #{type.name}" if @type), ("= #{@value.inspect}" if @value) ].compact.join(' ')}>" end |
#nullable? ⇒ Boolean
56 57 58 |
# File 'lib/smart_params/field.rb', line 56 def nullable? !!@nullable end |
#removable? ⇒ Boolean
Should this field be removed from resulting hash?
110 111 112 |
# File 'lib/smart_params/field.rb', line 110 def removable? empty? && !allow_empty? end |
#root? ⇒ Boolean
48 49 50 |
# File 'lib/smart_params/field.rb', line 48 def root? keychain.empty? end |
#specified? ⇒ Boolean
60 61 62 63 64 65 66 |
# File 'lib/smart_params/field.rb', line 60 def specified? if nullable? !!@specified && clean? else !!@specified end end |
#to_hash ⇒ Object
99 100 101 102 103 |
# File 'lib/smart_params/field.rb', line 99 def to_hash keychain.reverse.reduce(value) do |accumulation, key| { key => accumulation } end end |
#value ⇒ Object
52 53 54 |
# File 'lib/smart_params/field.rb', line 52 def value @value || ({} if root?) end |
#weight ⇒ Object
114 115 116 |
# File 'lib/smart_params/field.rb', line 114 def weight keychain.map(&:to_s) end |