Class: SmartConfig::Walker

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

Overview

Loads the data from any configured source and walks through it to find values

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sources) ⇒ Walker

Returns a new instance of Walker.



24
25
26
# File 'lib/smart_config/walker.rb', line 24

def initialize(sources)
  @sources = sources
end

Class Attribute Details

.strategiesObject (readonly)

Returns the value of attribute strategies.



17
18
19
# File 'lib/smart_config/walker.rb', line 17

def strategies
  @strategies
end

Class Method Details

.register_strategy(name, &block) ⇒ Object



19
20
21
# File 'lib/smart_config/walker.rb', line 19

def register_strategy(name, &block)
  @strategies[name.to_sym] = block
end

Instance Method Details

#walk(path) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/smart_config/walker.rb', line 28

def walk(path)
  data.each_with_object([]) do |d, results|
    next if d[:data].nil?

    strategy = self.class.strategies.fetch(d[:strategy]) do
      raise ArgumentError, "Unknown SmartConfig walker strategy: #{d[:strategy].inspect}"
    end
    value = strategy.call(d[:data], path)
    results << value unless value.nil?
  end
end