Class: StrongJSON::Type::Array
- Inherits:
-
Object
- Object
- StrongJSON::Type::Array
- Defined in:
- lib/strong_json/type.rb
Instance Attribute Summary collapse
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #coerce(value, path: ErrorPath.root(self)) ⇒ Object
-
#initialize(type) ⇒ Array
constructor
A new instance of Array.
- #to_s ⇒ Object
Methods included from WithAlias
Methods included from Match
Constructor Details
#initialize(type) ⇒ Array
Returns a new instance of Array.
161 162 163 |
# File 'lib/strong_json/type.rb', line 161 def initialize(type) @type = type end |
Instance Attribute Details
#type ⇒ Object (readonly)
Returns the value of attribute type.
159 160 161 |
# File 'lib/strong_json/type.rb', line 159 def type @type end |
Instance Method Details
#==(other) ⇒ Object
179 180 181 182 183 184 |
# File 'lib/strong_json/type.rb', line 179 def ==(other) if other.is_a?(Array) # @type var other: Array<any> other.type == type end end |
#coerce(value, path: ErrorPath.root(self)) ⇒ Object
165 166 167 168 169 170 171 172 173 |
# File 'lib/strong_json/type.rb', line 165 def coerce(value, path: ErrorPath.root(self)) if value.is_a?(::Array) value.map.with_index do |v, i| @type.coerce(v, path: path.dig(key: i, type: @type)) end else raise TypeError.new(path: path, value: value) end end |
#to_s ⇒ Object
175 176 177 |
# File 'lib/strong_json/type.rb', line 175 def to_s self.alias&.to_s || "array(#{@type})" end |