Class: Lookbook::ListResolver

Inherits:
Service
  • Object
show all
Defined in:
lib/lookbook/services/list_resolver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Service

call

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_setObject (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_includeObject



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