Class: Lookbook::ListResolver
- Defined in:
- lib/lookbook/services/list_resolver.rb
Instance Attribute Summary collapse
- #item_set ⇒ Object readonly
Instance Method Summary collapse
- #call(&resolver) ⇒ Object
-
#initialize(to_include = nil, item_set = nil) ⇒ ListResolver
constructor
A new instance of ListResolver.
- #to_include ⇒ Object
Methods inherited from Service
Constructor Details
#initialize(to_include = nil, item_set = nil) ⇒ ListResolver
Returns a new instance of ListResolver.
5 6 7 8 |
# File 'lib/lookbook/services/list_resolver.rb', line 5 def initialize(to_include = nil, item_set = nil) @to_include = to_include @item_set = Array(item_set).compact.uniq.map(&:to_s) end |
Instance Attribute Details
#item_set ⇒ Object (readonly)
3 4 5 |
# File 'lib/lookbook/services/list_resolver.rb', line 3 def item_set @item_set end |
Instance Method Details
#call(&resolver) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/lookbook/services/list_resolver.rb', line 10 def call(&resolver) included = to_include.inject([]) do |result, name| if name == "*" result += item_set.select { |item| !result.include?(item) } elsif item_set.include?(name) result << name end result end resolved = resolver ? included.map { |item| resolver.call(item) } : included resolved.compact end |
#to_include ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/lookbook/services/list_resolver.rb', line 24 def to_include case @to_include when true ["*"] when false [] else Array(@to_include).compact.uniq.map(&:to_s) end end |