Class: TypedParams::Parameter
- Inherits:
-
Object
- Object
- TypedParams::Parameter
- Defined in:
- lib/typed_params/parameter.rb
Instance Attribute Summary collapse
-
#key ⇒ Object
Returns the value of attribute key.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
- #array? ⇒ Boolean
- #deconstruct ⇒ Object
- #deconstruct_keys(keys) ⇒ Object
- #delete ⇒ Object
- #hash? ⇒ Boolean
-
#initialize(key:, value:, schema:, parent: nil) ⇒ Parameter
constructor
A new instance of Parameter.
- #inspect ⇒ Object
- #key?(key) ⇒ Boolean (also: #has_key?)
- #keys ⇒ Object
- #keys?(*keys) ⇒ Boolean (also: #has_keys?)
- #method_missing(method_name) ⇒ Object
- #parent? ⇒ Boolean
- #path ⇒ Object
-
#respond_to_missing?(method_name) ⇒ Boolean
Delegate everything else to the value.
- #scalar? ⇒ Boolean
- #unwrap(formatter: schema.formatter, controller: nil) ⇒ Object
Constructor Details
#initialize(key:, value:, schema:, parent: nil) ⇒ Parameter
Returns a new instance of Parameter.
13 14 15 16 17 18 |
# File 'lib/typed_params/parameter.rb', line 13 def initialize(key:, value:, schema:, parent: nil) @key = key @value = value @schema = schema @parent = parent end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name) ⇒ Object
98 |
# File 'lib/typed_params/parameter.rb', line 98 def method_missing(method_name, ...) = value.send(method_name, ...) |
Instance Attribute Details
#key ⇒ Object
Returns the value of attribute key.
7 8 9 |
# File 'lib/typed_params/parameter.rb', line 7 def key @key end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
10 11 12 |
# File 'lib/typed_params/parameter.rb', line 10 def parent @parent end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
10 11 12 |
# File 'lib/typed_params/parameter.rb', line 10 def schema @schema end |
#value ⇒ Object
Returns the value of attribute value.
7 8 9 |
# File 'lib/typed_params/parameter.rb', line 7 def value @value end |
Instance Method Details
#array? ⇒ Boolean
20 |
# File 'lib/typed_params/parameter.rb', line 20 def array? = Types.array?(value) |
#deconstruct ⇒ Object
101 |
# File 'lib/typed_params/parameter.rb', line 101 def deconstruct = value |
#deconstruct_keys(keys) ⇒ Object
100 |
# File 'lib/typed_params/parameter.rb', line 100 def deconstruct_keys(keys) = { key:, value: } |
#delete ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/typed_params/parameter.rb', line 51 def delete raise NotImplementedError, "cannot delete param: #{key.inspect}" unless parent? case parent.value when Array parent.value.delete(self) when Hash parent.value.delete( parent.value.key(self), ) end end |
#hash? ⇒ Boolean
21 |
# File 'lib/typed_params/parameter.rb', line 21 def hash? = Types.hash?(value) |
#inspect ⇒ Object
103 104 105 106 107 |
# File 'lib/typed_params/parameter.rb', line 103 def inspect value = unwrap(formatter: nil) "#<#{self.class.name} key=#{key.inspect} value=#{value.inspect}>" end |
#key?(key) ⇒ Boolean Also known as: has_key?
31 |
# File 'lib/typed_params/parameter.rb', line 31 def key?(key) = keys.include?(key) |
#keys ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/typed_params/parameter.rb', line 37 def keys return [] if schema.children.blank? case value when Array (0...value.size).to_a when Hash value.keys else [] end end |
#keys?(*keys) ⇒ Boolean Also known as: has_keys?
34 |
# File 'lib/typed_params/parameter.rb', line 34 def keys?(*keys) = keys.all? { key?(_1) } |
#parent? ⇒ Boolean
23 |
# File 'lib/typed_params/parameter.rb', line 23 def parent? = parent.present? |
#path ⇒ Object
25 26 27 28 29 |
# File 'lib/typed_params/parameter.rb', line 25 def path key = @key == ROOT ? nil : @key @path ||= Path.new(*parent&.path&.keys, *key) end |
#respond_to_missing?(method_name) ⇒ Boolean
Delegate everything else to the value
97 |
# File 'lib/typed_params/parameter.rb', line 97 def respond_to_missing?(method_name, ...) = value.respond_to?(method_name, ...) |
#scalar? ⇒ Boolean
22 |
# File 'lib/typed_params/parameter.rb', line 22 def scalar? = Types.scalar?(value) |
#unwrap(formatter: schema.formatter, controller: nil) ⇒ Object
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/typed_params/parameter.rb', line 65 def unwrap(formatter: schema.formatter, controller: nil) v = case value when Hash value.transform_values { _1.respond_to?(:unwrap) ? _1.unwrap : _1 } when Array value.map { _1.respond_to?(:unwrap) ? _1.unwrap : _1 } else value.respond_to?(:unwrap) ? value.unwrap : value end if formatter.present? v = case formatter.arity when 2 case formatter.parameters in [[:req, *], [:keyreq | :key, :controller], [:keyreq | :key, :schema]] formatter.call(v, controller:, schema:) in [[:req, *], [:keyreq | :key, :schema], [:keyreq | :key, :controller]] formatter.call(v, schema:, controller:) in [[:req, *], [:keyreq | :key, :controller]] formatter.call(v, controller:) in [[:req, *], [:keyreq | :key, :schema]] formatter.call(v, schema:) end when 1 formatter.call(v) end end v end |