Class: RangeClassifier

Inherits:
Object
  • Object
show all
Defined in:
lib/range_classifier.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(projection = RangeClassifier.id, ranges: []) ⇒ RangeClassifier

Returns a new instance of RangeClassifier.



4
5
6
7
# File 'lib/range_classifier.rb', line 4

def initialize(projection=RangeClassifier.id, ranges: [])
  @projection = projection
  @ranges = ranges
end

Instance Attribute Details

#projectionObject (readonly)

Returns the value of attribute projection.



2
3
4
# File 'lib/range_classifier.rb', line 2

def projection
  @projection
end

Instance Method Details

#append_lower_stream(stream, low: nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/range_classifier.rb', line 14

def append_lower_stream(stream, low: nil)
  stream.each do |high, result|
    @ranges << {
      lower: [low, high],
      result: result
    }
    low = high
  end
  self
end

#append_ranges(ranges) ⇒ Object



9
10
11
12
# File 'lib/range_classifier.rb', line 9

def append_ranges(ranges)
  @ranges.concat(ranges)
  self
end

#classify(element, *args) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/range_classifier.rb', line 25

def classify(element, *args)
  lkey, ukey = @projection.call(element, *args)
  selected = @ranges.find { |range|
    matches_section(range[:lower], lkey) && matches_section(range[:upper], ukey)
  }
  selected[:result]
end