Class: TypedParams::Parameter

Inherits:
Object
  • Object
show all
Defined in:
lib/typed_params/parameter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#keyObject

Returns the value of attribute key.



7
8
9
# File 'lib/typed_params/parameter.rb', line 7

def key
  @key
end

#parentObject (readonly)

Returns the value of attribute parent.



10
11
12
# File 'lib/typed_params/parameter.rb', line 10

def parent
  @parent
end

#schemaObject (readonly)

Returns the value of attribute schema.



10
11
12
# File 'lib/typed_params/parameter.rb', line 10

def schema
  @schema
end

#valueObject

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

Returns:

  • (Boolean)


20
# File 'lib/typed_params/parameter.rb', line 20

def array?  = Types.array?(value)

#deconstructObject



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: }

#deleteObject

Raises:

  • (NotImplementedError)


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

Returns:

  • (Boolean)


21
# File 'lib/typed_params/parameter.rb', line 21

def hash?   = Types.hash?(value)

#inspectObject



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?

Returns:

  • (Boolean)


31
# File 'lib/typed_params/parameter.rb', line 31

def key?(key) = keys.include?(key)

#keysObject



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?

Returns:

  • (Boolean)


34
# File 'lib/typed_params/parameter.rb', line 34

def keys?(*keys) = keys.all? { key?(_1) }

#parent?Boolean

Returns:

  • (Boolean)


23
# File 'lib/typed_params/parameter.rb', line 23

def parent? = parent.present?

#pathObject



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

Returns:

  • (Boolean)


97
# File 'lib/typed_params/parameter.rb', line 97

def respond_to_missing?(method_name, ...) = value.respond_to?(method_name, ...)

#scalar?Boolean

Returns:

  • (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