Class: Octopusci::ConfigStore

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

Defined Under Namespace

Classes: MissingConfigField

Instance Method Summary collapse

Constructor Details

#initializeConfigStore

Returns a new instance of ConfigStore.



7
8
9
# File 'lib/octopusci/config.rb', line 7

def initialize
  reset()
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(key_name, *args) ⇒ Object

allow options to be read and set using method calls. This capability is primarily for allowing the configuration to be defined through a block passed to the configure() function from an initializer or similar file.



50
51
52
53
54
55
56
57
# File 'lib/octopusci/config.rb', line 50

def method_missing(key_name, *args)
  key_name_str = key_name.to_s()
  if key_name_str =~ /=$/ then
    self[key_name_str.chop()] = args[0]
  else
    return self[key_name_str]
  end
end

Instance Method Details

#[](key_name) ⇒ Object

allow options to be accessed as if this object is a Hash.



31
32
33
34
35
36
# File 'lib/octopusci/config.rb', line 31

def [](key_name)
  if !@options.has_key?(key_name.to_s())
    raise MissingConfigField, "'#{key_name}' is NOT defined as a config field."
  end
  return @options[key_name.to_s()]
end

#[]=(key_name, value) ⇒ Object

allow options to be set as if this object is a Hash.



39
40
41
# File 'lib/octopusci/config.rb', line 39

def []=(key_name, value)
  @options[key_name.to_s()] = value
end

#after_load(&block) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/octopusci/config.rb', line 59

def after_load(&block)
  if block
    @after_load = block
  elsif @after_load
    @after_load.call
  end
end

#has_key?(key_name) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/octopusci/config.rb', line 43

def has_key?(key_name)
  return @options.has_key?(key_name)
end

#load(yaml_file = nil) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



19
20
21
22
23
# File 'lib/octopusci/config.rb', line 19

def load(yaml_file = nil, &block)
  load_yaml(yaml_file) if !yaml_file.nil?
  yield self if block
  after_load()
end

#optionsObject



15
16
17
# File 'lib/octopusci/config.rb', line 15

def options
  @options
end

#reload(yaml_file = nil, &block) ⇒ Object



25
26
27
28
# File 'lib/octopusci/config.rb', line 25

def reload(yaml_file = nil, &block)
  reset()
  load(yaml_file, &block)
end

#resetObject



11
12
13
# File 'lib/octopusci/config.rb', line 11

def reset
  @options = {}
end