Class: RFlow::Configuration::DataExtensionCollection

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

Overview

A collection class for data extensions that supports a naive prefix-based ‘inheritance’ on lookup. When looking up a key with [] all existing keys will be examined to determine if the existing key is a string prefix of the lookup key. All the results are consolidated into a single, flattened array.

Instance Method Summary collapse

Constructor Details

#initializeDataExtensionCollection

Returns a new instance of DataExtensionCollection.



19
20
21
22
# File 'lib/rflow/configuration.rb', line 19

def initialize
  # TODO: choose a different data structure ...
  @extensions_for = Hash.new {|hash, key| hash[key] = []}
end

Instance Method Details

#[](key) ⇒ Object

Return an array of all of the values that have keys that are prefixes of the lookup key.



26
27
28
29
30
# File 'lib/rflow/configuration.rb', line 26

def [](key)
  @extensions_for.
    find_all {|data_type, _| key.to_s.start_with?(data_type) }.
    flat_map {|_, extensions| extensions }
end

#add(data_type, extension) ⇒ Object

Adds a data extension for a given data type to the collection



33
34
35
# File 'lib/rflow/configuration.rb', line 33

def add(data_type, extension)
  @extensions_for[data_type.to_s] << extension
end

#clearObject

Remove all elements from the collection. Useful for testing, not much else



39
40
41
# File 'lib/rflow/configuration.rb', line 39

def clear
  @extensions_for.clear
end