Class: Lookbook::EntityCollection
- Inherits:
-
Object
- Object
- Lookbook::EntityCollection
show all
- Includes:
- Enumerable
- Defined in:
- lib/lookbook/entities/collections/entity_collection.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of EntityCollection.
9
10
11
12
13
|
# File 'lib/lookbook/entities/collections/entity_collection.rb', line 9
def initialize(entities = nil)
@_cache = {}
@entities = []
add(entities)
end
|
Instance Attribute Details
#entities ⇒ Object
7
8
9
|
# File 'lib/lookbook/entities/collections/entity_collection.rb', line 7
def entities
@entities
end
|
Instance Method Details
#add(to_add = nil) ⇒ Object
15
16
17
18
19
20
21
22
|
# File 'lib/lookbook/entities/collections/entity_collection.rb', line 15
def add(to_add = nil)
Array(to_add).each do |entity|
unless find_by_path(entity.lookup_path)
@entities.push(entity)
end
end
clear_cache
end
|
#clear_all ⇒ Object
55
56
57
58
|
# File 'lib/lookbook/entities/collections/entity_collection.rb', line 55
def clear_all
@entities = []
clear_cache
end
|
#each(&block) ⇒ Object
43
44
45
46
47
48
49
|
# File 'lib/lookbook/entities/collections/entity_collection.rb', line 43
def each(&block)
if block
entities.sort.each { |entity| yield entity }
else
to_enum(:each)
end
end
|
#find_by_id(id) ⇒ Object
24
25
26
27
|
# File 'lib/lookbook/entities/collections/entity_collection.rb', line 24
def find_by_id(id)
id = Utils.id(id)
@entities.find { |entity| entity.id == id }
end
|
#find_by_path(path) ⇒ Object
29
30
31
|
# File 'lib/lookbook/entities/collections/entity_collection.rb', line 29
def find_by_path(path)
@entities.find { |entity| entity.lookup_path.to_s == path.to_s }
end
|
#flat_map ⇒ Object
51
52
53
|
# File 'lib/lookbook/entities/collections/entity_collection.rb', line 51
def flat_map(...)
entities.map(...).map { |e| e.respond_to?(:to_a) ? e.to_a : e }.flatten
end
|
#next(entity) ⇒ Object
33
34
35
36
|
# File 'lib/lookbook/entities/collections/entity_collection.rb', line 33
def next(entity)
index = entities.find_index { |i| i.lookup_path == entity.lookup_path }
entities[index + 1] unless index.nil?
end
|
#previous(entity) ⇒ Object
38
39
40
41
|
# File 'lib/lookbook/entities/collections/entity_collection.rb', line 38
def previous(entity)
index = entities.find_index { |i| i.lookup_path == entity.lookup_path }
entities[index - 1] if !index.nil? && index > 0
end
|