Class: TkWrapper::Widgets::Base::ComparatorItemStore

Inherits:
Object
  • Object
show all
Defined in:
lib/widgets/base/comparator_item_store.rb

Constant Summary collapse

Matcher =
TkWrapper::Widgets::Base::Matcher

Instance Method Summary collapse

Constructor Details

#initializeComparatorItemStore

Returns a new instance of ComparatorItemStore.



8
9
10
11
# File 'lib/widgets/base/comparator_item_store.rb', line 8

def initialize
  @key_map = {}        # for fast lookup
  @comparator_map = {} # for lookup using comparisons by Matcher class
end

Instance Method Details

#[](key) ⇒ Object



32
33
34
35
# File 'lib/widgets/base/comparator_item_store.rb', line 32

def [](key)
  items = map_key?(key) ? @key_map[key] : @comparator_map[key]
  items&.length == 1 ? items.first : items
end

#items_and_matches_for_widget(widget) ⇒ Object

returns [[…], match: Match, […], …]



26
27
28
29
30
# File 'lib/widgets/base/comparator_item_store.rb', line 26

def items_and_matches_for_widget(widget)
  widget.ids.reduce([]) do |items, id|
    items + items_from_key_map(id) + items_from_comparator_map(id, widget)
  end
end

#map_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/widgets/base/comparator_item_store.rb', line 13

def map_key?(key)
  [String, Symbol].include?(key)
end

#push(key, *items) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/widgets/base/comparator_item_store.rb', line 17

def push(key, *items)
  if map_key?(key)
    (@key_map[key] ||= []).concat(items)
  else
    (@comparator_map[key] ||= []).concat(items)
  end
end