Class: Lite::Containers::Helpers::Comparison::Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/lite/containers/helpers/comparison.rb

Direct Known Subclasses

Max, Min

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key_extractor) ⇒ Abstract

Returns a new instance of Abstract.



21
22
23
24
# File 'lib/lite/containers/helpers/comparison.rb', line 21

def initialize(key_extractor)
  @key_extractor = key_extractor
  freeze
end

Class Method Details

.for_item(item) ⇒ Object Also known as: for_key



50
51
52
# File 'lib/lite/containers/helpers/comparison.rb', line 50

def for_item(item)
  method(:compare).curry.call(item)
end

.to_key(element) ⇒ Object



46
47
48
# File 'lib/lite/containers/helpers/comparison.rb', line 46

def to_key(element)
  element
end

Instance Method Details

#compare(a, b) ⇒ Object



41
42
43
# File 'lib/lite/containers/helpers/comparison.rb', line 41

def compare(a, b)
  self.class.compare(to_key(a), to_key(b))
end

#for_item(item) ⇒ Object



30
31
32
# File 'lib/lite/containers/helpers/comparison.rb', line 30

def for_item(item)
  method(:compare).curry.call(item)
end

#for_key(key) ⇒ Object



34
35
36
37
38
39
# File 'lib/lite/containers/helpers/comparison.rb', line 34

def for_key(key)
  comparison = self.class
  proc do |b|
    comparison.compare(key, to_key(b))
  end
end

#to_key(element) ⇒ Object



26
27
28
# File 'lib/lite/containers/helpers/comparison.rb', line 26

def to_key(element)
  @key_extractor.call(element)
end