Class: StrongJSON::Type::Array

Inherits:
Object
  • Object
show all
Includes:
Match, WithAlias
Defined in:
lib/strong_json/type.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from WithAlias

#alias, #with_alias

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

#typeObject (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_sObject



175
176
177
# File 'lib/strong_json/type.rb', line 175

def to_s
  self.alias&.to_s || "array(#{@type})"
end