Module: JSI::BaseArray

Includes:
Arraylike
Defined in:
lib/jsi/base.rb

Overview

module extending a Base object when its schema instance is Array-like (responds to #to_ary)

Constant Summary

Constants included from Arraylike

Arraylike::DESTRUCTIVE_METHODS, Arraylike::SAFE_INDEX_ELEMENT_METHODS, Arraylike::SAFE_INDEX_ONLY_METHODS, Arraylike::SAFE_METHODS

Instance Method Summary collapse

Methods included from Arraylike

#inspect, #pretty_print, #to_s

Instance Method Details

#[](i_) ⇒ Object

Returns the instance's subscript value at the given index i_. if there is a subschema defined for that index on this JSI's schema, returns the instance's subscript as a JSI instiation of that subschema.

Parameters:

  • i_

    the array index to subscript

Returns:

  • (Object)

    returns the instance's subscript value at the given index i_. if there is a subschema defined for that index on this JSI's schema, returns the instance's subscript as a JSI instiation of that subschema.



423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/jsi/base.rb', line 423

def [](i_)
  memoize(:[], i_) do |i|
    begin
      index_schema = schema.subschema_for_index(i)
      index_schema = index_schema && index_schema.match_to_instance(instance[i])

      if index_schema && instance[i].is_a?(JSON::Node)
        JSI.class_for_schema(index_schema).new(instance[i], origin: @origin)
      else
        instance[i]
      end
    end
  end
end

#[]=(i, value) ⇒ Object

assigns the given index of the instance to the given value. if the value is a JSI, its instance is assigned.

Parameters:

  • i (Object)

    the array index to assign

  • value (Object)

    the value to be assigned to the given subscript i



442
443
444
# File 'lib/jsi/base.rb', line 442

def []=(i, value)
  subscript_assign(i, value)
end

#each {|Object| ... } ⇒ self, Enumerator

yields each element. each yielded element is the result of self[index] for each index of the instance (see #[]). returns an Enumerator if no block is given.

Yields:

  • (Object)

    each element of this JSI array

Returns:

  • (self, Enumerator)


399
400
401
402
403
# File 'lib/jsi/base.rb', line 399

def each
  return to_enum(__method__) { instance.size } unless block_given?
  instance.each_index { |i| yield(self[i]) }
  self
end

#to_aryArray

Returns an array, the same size as the instance, in which the element at each index is the result of selfindex.

Returns:

  • (Array)

    an array, the same size as the instance, in which the element at each index is the result of selfindex



407
408
409
# File 'lib/jsi/base.rb', line 407

def to_ary
  to_a
end