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
-
#[](i_) ⇒ Object
Returns the instance's subscript value at the given index i_.
-
#[]=(i, value) ⇒ Object
assigns the given index of the instance to the given value.
-
#each {|Object| ... } ⇒ self, Enumerator
yields each element.
-
#to_ary ⇒ Array
An array, the same size as the instance, in which the element at each index is the result of selfindex.
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.
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.
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.
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_ary ⇒ Array
Returns 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 |