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
163 164 165 |
# File 'lib/strong_json/type.rb', line 163 def initialize(type) @type = type end |
Instance Attribute Details
#type ⇒ Object (readonly)
Returns the value of attribute type.
161 162 163 |
# File 'lib/strong_json/type.rb', line 161 def type @type end |
Instance Method Details
#==(other) ⇒ Object
181 182 183 184 185 186 |
# File 'lib/strong_json/type.rb', line 181 def ==(other) if other.is_a?(Array) # @type var other: Array<any> other.type == type end end |
#coerce(value, path: ErrorPath.root(self)) ⇒ Object
167 168 169 170 171 172 173 174 175 |
# File 'lib/strong_json/type.rb', line 167 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
177 178 179 |
# File 'lib/strong_json/type.rb', line 177 def to_s self.alias&.to_s || "array(#{@type})" end |