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