Module: JSI::Base::Enumerable

Includes:
Enumerable
Included in:
ArrayNode, HashNode
Defined in:
lib/jsi/base/node.rb

Instance Method Summary collapse

Instance Method Details

#as_json(*opt) ⇒ Object

a jsonifiable representation of the node content

Returns:

  • (Object)


26
27
28
29
30
31
# File 'lib/jsi/base/node.rb', line 26

def as_json(*opt)
  # include Enumerable (above) means, if ActiveSupport is loaded, its undesirable #as_json is included
  # https://github.com/rails/rails/blob/v7.0.0/activesupport/lib/active_support/core_ext/object/json.rb#L139-L143
  # although Base#as_json does clobber activesupport's, I want as_json defined correctly on the module too.
  Typelike.as_json(jsi_node_content, *opt)
end

#to_a(**kw) ⇒ Array Also known as: entries

an Array containing each item in this JSI.

Parameters:

  • kw

    keyword arguments are passed to JSI::Base#[] - see its keyword params

Returns:

  • (Array)


11
12
13
14
15
16
17
18
19
20
# File 'lib/jsi/base/node.rb', line 11

def to_a(**kw)
  # TODO remove eventually (keyword argument compatibility)
  # discard when all supported ruby versions Enumerable#to_a delegate keywords to #each (3.0.1 breaks; 2.7.x warns)
  # https://bugs.ruby-lang.org/issues/18289
  ary = []
  each(**kw) do |e|
    ary << e
  end
  ary
end