Module: JSI::BaseArray

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

Overview

module extending a Base object when its 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 PathedArrayNode

#each, #node_content_ary_pubsend, #to_ary

Methods included from Arraylike

#inspect, #pretty_print, #to_s

Instance Method Details

#[](i) ⇒ JSI::Base, Object

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 (Integer)

    the array index to subscript

Returns:

  • (JSI::Base, Object)

    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.



431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# File 'lib/jsi/base.rb', line 431

def [](i)
  memoize(:[], i, jsi_instance_sub(i), jsi_instance_ary_pubsend(:each_index).to_a.include?(i)) do |i_, instance_idx_value, i_in_range|
    begin
      index_schema = schema.subschema_for_index(i_)
      index_schema = index_schema && index_schema.match_to_instance(instance_idx_value)

      if !i_in_range && index_schema && index_schema.schema_object.key?('default')
        # use the default value
        default = index_schema.schema_object['default']
        if default.respond_to?(:to_hash) || default.respond_to?(:to_ary)
          # we are using #dup so that we get a modified copy of self, in which we set dup[i]=default.
          # this avoids duplication of code with #modified_copy and below in #[] to handle pathing and such.
          dup.tap { |o| o[i_] = default }[i_]
        else
          default
        end
      elsif index_schema && (instance_idx_value.respond_to?(:to_hash) || instance_idx_value.respond_to?(:to_ary))
        class_for_schema(index_schema).new(Base::NOINSTANCE, jsi_document: @jsi_document, jsi_ptr: @jsi_ptr[i_], ancestor_jsi: @ancestor_jsi || self)
      else
        instance_idx_value
      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



460
461
462
# File 'lib/jsi/base.rb', line 460

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