Class: Vident::Stimulus::Collection
- Inherits:
-
Object
- Object
- Vident::Stimulus::Collection
- Includes:
- Enumerable
- Defined in:
- lib/vident/stimulus/collection.rb
Instance Attribute Summary collapse
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#kind ⇒ Object
readonly
Returns the value of attribute kind.
Instance Method Summary collapse
- #any? ⇒ Boolean
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(kind:, items:) ⇒ Collection
constructor
A new instance of Collection.
- #length ⇒ Object
- #merge(other) ⇒ Object
- #size ⇒ Object
- #to_a ⇒ Object
- #to_h ⇒ Object (also: #to_hash)
Constructor Details
#initialize(kind:, items:) ⇒ Collection
Returns a new instance of Collection.
10 11 12 13 |
# File 'lib/vident/stimulus/collection.rb', line 10 def initialize(kind:, items:) @kind = kind @items = items.freeze end |
Instance Attribute Details
#items ⇒ Object (readonly)
Returns the value of attribute items.
8 9 10 |
# File 'lib/vident/stimulus/collection.rb', line 8 def items @items end |
#kind ⇒ Object (readonly)
Returns the value of attribute kind.
8 9 10 |
# File 'lib/vident/stimulus/collection.rb', line 8 def kind @kind end |
Instance Method Details
#any? ⇒ Boolean
25 |
# File 'lib/vident/stimulus/collection.rb', line 25 def any? = @items.any? |
#each(&block) ⇒ Object
15 |
# File 'lib/vident/stimulus/collection.rb', line 15 def each(&block) = @items.each(&block) |
#empty? ⇒ Boolean
23 |
# File 'lib/vident/stimulus/collection.rb', line 23 def empty? = @items.empty? |
#length ⇒ Object
21 |
# File 'lib/vident/stimulus/collection.rb', line 21 def length = @items.size |
#merge(other) ⇒ Object
32 33 34 35 36 37 |
# File 'lib/vident/stimulus/collection.rb', line 32 def merge(other) unless other.is_a?(self.class) && other.kind == @kind raise ArgumentError, "Collection#merge: can only merge with same-kind Collection" end self.class.new(kind: @kind, items: @items + other.items) end |
#size ⇒ Object
19 |
# File 'lib/vident/stimulus/collection.rb', line 19 def size = @items.size |
#to_a ⇒ Object
17 |
# File 'lib/vident/stimulus/collection.rb', line 17 def to_a = @items.dup |
#to_h ⇒ Object Also known as: to_hash
27 28 29 |
# File 'lib/vident/stimulus/collection.rb', line 27 def to_h @kind.value_class.to_data_hash(@items) end |