Class: Jimmy::Json::Array
- Inherits:
-
Object
- Object
- Jimmy::Json::Array
- Includes:
- Collection
- Defined in:
- lib/jimmy/json/array.rb
Overview
Represents an array in a JSON schema.
Constant Summary collapse
- KEY_PATTERN =
/\A(?:\d|[1-9]\d+)\z/.freeze
Instance Method Summary collapse
-
#[]=(index, value) ⇒ Object
Assign a member to the array at the given index.
-
#concat(array) ⇒ self
Append items in
array
to self. -
#dig(key, *rest) ⇒ Object
Dig into the array.
-
#each {|index, member| ... } ⇒ Enumerable, self
Iterate over items in the array.
-
#include?(obj) ⇒ true, false
Returns true if the array contains the given
obj
. -
#initialize(array = []) ⇒ Array
constructor
A new instance of Array.
-
#length ⇒ Integer
(also: #count, #size)
The length of the array.
-
#push(*members) ⇒ self
(also: #<<)
Add one or more items to self.
-
#to_a ⇒ Array
Get a regular array.
Methods included from Collection
#[], #as_json, #clear, #deep_dup, #dup, #empty?, #freeze, #inspect, #to_json
Constructor Details
#initialize(array = []) ⇒ Array
Returns a new instance of Array.
14 15 16 17 18 |
# File 'lib/jimmy/json/array.rb', line 14 def initialize(array = []) super() @members = [] concat array end |
Instance Method Details
#[]=(index, value) ⇒ Object
Assign a member to the array at the given index.
41 42 43 |
# File 'lib/jimmy/json/array.rb', line 41 def []=(index, value) @members[index] = cast_value(value) end |
#concat(array) ⇒ self
Append items in array
to self.
23 24 25 26 |
# File 'lib/jimmy/json/array.rb', line 23 def concat(array) array = array.to_a if array.is_a? Set push *array end |
#dig(key, *rest) ⇒ Object
Dig into the array.
72 73 74 75 |
# File 'lib/jimmy/json/array.rb', line 72 def dig(key, *rest) key = key.to_i if key.is_a?(String) && key.match?(KEY_PATTERN) super key, *rest end |
#each {|index, member| ... } ⇒ Enumerable, self
Iterate over items in the array. If a block with a single argument is given, only values will be yielded. Otherwise, indexes and values will be yielded.
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/jimmy/json/array.rb', line 52 def each(&block) return enum_for :each unless block if block.arity == 1 @members.each { |member| yield member } else @members.each.with_index { |member, i| yield i, member } end self end |
#include?(obj) ⇒ true, false
Returns true if the array contains the given obj
.
88 89 90 |
# File 'lib/jimmy/json/array.rb', line 88 def include?(obj) @members.include? obj end |
#length ⇒ Integer Also known as: count, size
Returns The length of the array.
64 65 66 |
# File 'lib/jimmy/json/array.rb', line 64 def length @members.length end |
#push(*members) ⇒ self Also known as: <<
Add one or more items to self.
31 32 33 34 |
# File 'lib/jimmy/json/array.rb', line 31 def push(*members) @members.concat members.map(&method(:cast_value)) self end |
#to_a ⇒ Array
Returns Get a regular array.
78 79 80 |
# File 'lib/jimmy/json/array.rb', line 78 def to_a @members.dup end |