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.
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 |
# File 'lib/jsi/base.rb', line 434 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 !instance.each_index.to_a.include?(i) && 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) class_for_schema(index_schema).new(default, ancestor: @ancestor) else default end elsif index_schema && (instance[i].respond_to?(:to_hash) || instance[i].respond_to?(:to_ary)) class_for_schema(index_schema).new(instance[i], ancestor: @ancestor) 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.
461 462 463 |
# File 'lib/jsi/base.rb', line 461 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.
410 411 412 413 414 |
# File 'lib/jsi/base.rb', line 410 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.
418 419 420 |
# File 'lib/jsi/base.rb', line 418 def to_ary to_a end |