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.



10
11
12
13
# File 'lib/configr/configuration.rb', line 10

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



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

def method_missing(method, *args, &block)
  name = method.to_s
  
  case
  when name.include?("=")
    raise ConfigurationLocked, "Blocked from configuring values after 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.



8
9
10
# File 'lib/configr/configuration.rb', line 8

def attributes
  @attributes
end

#baseObject

Returns the value of attribute base.



8
9
10
# File 'lib/configr/configuration.rb', line 8

def base
  @base
end

#yamlObject

Returns the value of attribute yaml.



8
9
10
# File 'lib/configr/configuration.rb', line 8

def yaml
  @yaml
end

Class Method Details

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

Yields:



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/configr/configuration.rb', line 15

def self.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



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

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

#merge_configurations!Object



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

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