Class: Configr::Configuration

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(yaml = nil) ⇒ Configuration

Returns a new instance of Configuration.



25
26
27
28
# File 'lib/configr/configuration.rb', line 25

def initialize(yaml=nil)
  self.base = ConfigurationBlock.new
  self.yaml = yaml
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/configr/configuration.rb', line 34

def method_missing(method, *args, &block)
  name = method.to_s
  
  case
  when name.include?("=")
    raise ConfigurationLocked, "Configuration is locked from configuring values after the configuration has been run."
  when name.include?("?")
    !self.attributes[name.gsub("?", "").to_sym].nil?
  else
    self.attributes[method.to_sym] or self.attributes[method.to_s]
  end
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



23
24
25
# File 'lib/configr/configuration.rb', line 23

def attributes
  @attributes
end

#baseObject

Returns the value of attribute base.



23
24
25
# File 'lib/configr/configuration.rb', line 23

def base
  @base
end

#yamlObject

Returns the value of attribute yaml.



23
24
25
# File 'lib/configr/configuration.rb', line 23

def yaml
  @yaml
end

Class Method Details

.configure(yaml = nil) {|instance.base| ... } ⇒ Object

Yields:



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/configr/configuration.rb', line 10

def configure(yaml=nil)
  instance = self.new(yaml)
  
  yield instance.base if block_given?
  
  instance.attributes = instance.base.attributes
  instance.merge_configurations!
  instance.attributes.recursive_normalize!
  
  instance
end

Instance Method Details

#[](value) ⇒ Object



30
31
32
# File 'lib/configr/configuration.rb', line 30

def [](value)
  self.attributes[value]
end

#merge_configurations!Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/configr/configuration.rb', line 47

def merge_configurations!
  return unless self.yaml
  
  self.yaml = self.yaml.to_s
  
  hash = if File.exists?(self.yaml)
    YAML.load_file(self.yaml)
  else
    YAML.load(self.yaml)
  end
  
  return unless hash
  
  hash = Hash.new(hash)
  hash.recursive_symbolize_keys!
  
  self.attributes.merge!(hash)
end