Class: Vident::StimulusCollectionBase
- Inherits:
-
Object
- Object
- Vident::StimulusCollectionBase
show all
- Defined in:
- lib/vident/stimulus_collection_base.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of StimulusCollectionBase.
5
6
7
|
# File 'lib/vident/stimulus_collection_base.rb', line 5
def initialize(items = [])
@items = Array(items).flatten.compact
end
|
Class Method Details
.merge(*collections) ⇒ Object
46
47
48
49
50
51
52
53
|
# File 'lib/vident/stimulus_collection_base.rb', line 46
def self.merge(*collections)
return new if collections.empty?
first_collection = collections.first
return first_collection if collections.size == 1
first_collection.merge(*collections[1..-1])
end
|
Instance Method Details
#<<(item) ⇒ Object
9
10
11
12
|
# File 'lib/vident/stimulus_collection_base.rb', line 9
def <<(item)
@items << item
self
end
|
#any? ⇒ Boolean
30
31
32
|
# File 'lib/vident/stimulus_collection_base.rb', line 30
def any?
!empty?
end
|
#empty? ⇒ Boolean
26
27
28
|
# File 'lib/vident/stimulus_collection_base.rb', line 26
def empty?
@items.empty?
end
|
#merge(*other_collections) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/vident/stimulus_collection_base.rb', line 34
def merge(*other_collections)
merged = self.class.new
merged.instance_variable_set(:@items, @items.dup)
other_collections.each do |collection|
next unless collection.is_a?(self.class)
merged.instance_variable_get(:@items).concat(collection.instance_variable_get(:@items))
end
merged
end
|
#to_a ⇒ Object
18
19
20
|
# File 'lib/vident/stimulus_collection_base.rb', line 18
def to_a
@items.dup
end
|
#to_h ⇒ Object
14
15
16
|
# File 'lib/vident/stimulus_collection_base.rb', line 14
def to_h
raise NoMethodError, "Subclasses must implement to_h"
end
|
#to_hash ⇒ Object
22
23
24
|
# File 'lib/vident/stimulus_collection_base.rb', line 22
def to_hash
to_h
end
|