Module: JSI::Base::ArrayNode

Includes:
Enumerable, Util::Arraylike
Defined in:
lib/jsi/base/node.rb

Overview

module extending a JSI::Base object when its instance (its #jsi_node_content) is an Array (or responds to #to_ary)

Constant Summary

Constants included from Util::Arraylike

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

Instance Method Summary collapse

Methods included from Util::Arraylike

#assoc, #inspect, #pretty_print, #rassoc

Methods included from Enumerable

#as_json, #to_a

Instance Method Details

#each(**kw) {|Object| ... } ⇒ self, Enumerator

yields each array element of this node.

each yielded element is the result of JSI::Base#[] for each index of the instance array.

returns an Enumerator if no block is given.

Parameters:

Yields:

  • (Object)

    each element of this array node

Returns:

  • (self, Enumerator)

    an Enumerator if invoked without a block; otherwise self



124
125
126
127
128
# File 'lib/jsi/base/node.rb', line 124

def each(**kw, &block)
  return to_enum(__method__, **kw) { jsi_node_content_ary_pubsend(:size) } unless block
  jsi_node_content_ary_pubsend(:each_index) { |i| yield(self[i, **kw]) }
  self
end

#jsi_node_content_ary_pubsend(method_name, *a, **kw, &b) ⇒ Object

invokes the method with the given name on the jsi_node_content (if defined) or its #to_ary

Parameters:

  • method_name (String, Symbol)
  • a

    positional arguments are passed to the invocation of method_name

  • kw

    keyword arguments are passed to the invocation of method_name

  • b

    block is passed to the invocation of method_name

Returns:

  • (Object)

    the result of calling method method_name on the jsi_node_content or its #to_ary



146
147
148
149
150
151
152
# File 'lib/jsi/base/node.rb', line 146

def jsi_node_content_ary_pubsend(method_name, *a, &b)
  if jsi_node_content.respond_to?(method_name)
    jsi_node_content.public_send(method_name, *a, &b)
  else
    jsi_node_content.to_ary.public_send(method_name, *a, &b)
  end
end

#to_ary(**kw) ⇒ Array

an array, the same size as the instance array, in which the element at each index is the result of JSI::Base#[].

Parameters:

Returns:

  • (Array)


134
135
136
# File 'lib/jsi/base/node.rb', line 134

def to_ary(**kw)
  to_a(**kw)
end