class Jimmy::Json::Array

Represents an array in a JSON schema.

Constants

KEY_PATTERN

Public Class Methods

new(array = []) click to toggle source

@param [Array, ::Array, Set] array Items to be included in the array.

Calls superclass method
# File lib/jimmy/json/array.rb, line 14
def initialize(array = [])
  super()
  @members = []
  concat array
end

Public Instance Methods

<<(*members)
Alias for: push
[]=(index, value) click to toggle source

Assign a member to the array at the given index. @param [Integer] index @param [Object] value

# File lib/jimmy/json/array.rb, line 41
def []=(index, value)
  @members[index] = cast_value(value)
end
concat(array) click to toggle source

Append items in array to self. @param [Array, ::Array, Set] array @return [self]

# File lib/jimmy/json/array.rb, line 23
def concat(array)
  array = array.to_a if array.is_a? Set
  push *array
end
count()
Alias for: length
dig(key, *rest) click to toggle source

Dig into the array. @param [Integer] key Index of the item to be dug or returned. @param [Array<String, Integer>] rest Keys or indexes to be passed to

resolved hashes/arrays.
Calls superclass method Jimmy::Json::Collection#dig
# File lib/jimmy/json/array.rb, line 72
def dig(key, *rest)
  key = key.to_i if key.is_a?(String) && key.match?(KEY_PATTERN)
  super key, *rest
end
each() { |member| ... } click to toggle source

Iterate over items in the array. If a block with a single argument is given, only values will be yielded. Otherwise, indexes and values will be yielded. @yieldparam [Integer] index The index of each item. @yieldparam [Object] member Each item. @return [Enumerable, self] If no block is given, an {::Enumerable} is

returned. Otherwise, +self+ is returned.
# File lib/jimmy/json/array.rb, line 52
def each(&block)
  return enum_for :each unless block

  if block.arity == 1
    @members.each { |member| yield member }
  else
    @members.each.with_index { |member, i| yield i, member }
  end
  self
end
include?(obj) click to toggle source

Returns true if the array contains the given obj. @param [Object] obj @return [true, false]

# File lib/jimmy/json/array.rb, line 88
def include?(obj)
  @members.include? obj
end
length() click to toggle source

@return [Integer] The length of the array.

# File lib/jimmy/json/array.rb, line 64
def length
  @members.length
end
Also aliased as: count, size
push(*members) click to toggle source

Add one or more items to self. @param [Array] members Things to add. @return [self]

# File lib/jimmy/json/array.rb, line 31
def push(*members)
  @members.concat members.map(&method(:cast_value))
  self
end
Also aliased as: <<
size()
Alias for: length
to_a() click to toggle source

@return [Array] Get a regular array.

# File lib/jimmy/json/array.rb, line 78
def to_a
  @members.dup
end

Protected Instance Methods

cast_key(key) click to toggle source
# File lib/jimmy/json/array.rb, line 98
def cast_key(key)
  unless key.is_a? Integer
    raise Error::WrongType, "Invalid array index of type #{key.class}"
  end

  key
end
export_pairs(pairs) click to toggle source
# File lib/jimmy/json/array.rb, line 94
def export_pairs(pairs)
  pairs.map &:last
end