Class: MatchReduce::Index

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/match_reduce/index.rb

Overview

The Index holds all the aggregators, the reverse lookup data structure, and the ability to retrieve aggregators based on a pattern

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aggregators = [], any: ANY) ⇒ Index

Returns a new instance of Index.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/match_reduce/index.rb', line 22

def initialize(aggregators = [], any: ANY)
  @any = any
  @aggregators = Aggregator.array(aggregators).uniq(&:name)
  @lookup = {}

  all_keys = @aggregators.flat_map(&:keys)
  @record  = HashMath::Record.new(all_keys, any)

  @aggregators.map do |aggregator|
    aggregator.patterns.each do |pattern|
      normalized_pattern = record.make!(pattern)

      get(normalized_pattern) << aggregator
    end
  end

  freeze
end

Instance Attribute Details

#aggregatorsObject (readonly)

Returns the value of attribute aggregators.



16
17
18
# File 'lib/match_reduce/index.rb', line 16

def aggregators
  @aggregators
end

#anyObject (readonly)

Returns the value of attribute any.



16
17
18
# File 'lib/match_reduce/index.rb', line 16

def any
  @any
end

#lookupObject (readonly)

Returns the value of attribute lookup.



16
17
18
# File 'lib/match_reduce/index.rb', line 16

def lookup
  @lookup
end

Instance Method Details

#find(pattern) ⇒ Object



41
42
43
# File 'lib/match_reduce/index.rb', line 41

def find(pattern)
  lookup.fetch(pattern, [])
end