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.



160
161
162
# File 'lib/strong_json/type.rb', line 160

def initialize(type)
  @type = type
end

Instance Attribute Details

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



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

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