Class: Infrastructure::Configuration
  
  
  
  
  
    - Inherits:
- 
      Object
      
        
          - Object
- Infrastructure::Configuration
 show all
    - Defined in:
- lib/infrastructure/configuration.rb
 
  Instance Attribute Summary collapse
  
  
    
      Instance Method Summary
      collapse
    
    
  
  Constructor Details
  
    
  
  
    #initialize(base, config = {})  ⇒ Configuration 
  
  
  
  
    
Returns a new instance of Configuration.
   
 
  
  
    | 
5
6
7
8
9 | # File 'lib/infrastructure/configuration.rb', line 5
def initialize(base, config = {})
  @base = base
  @attributes = Hash.new
  assign config
end | 
 
  
 
  Dynamic Method Handling
  
    This class handles dynamic methods through the method_missing method
    
  
  
    
  
  
    #method_missing(method, *params, &block)  ⇒ Object 
  
  
  
  
    | 
80
81
82
83
84
85
86
87
88
89
90 | # File 'lib/infrastructure/configuration.rb', line 80
def method_missing(method, *params, &block)
  if @attributes.keys.include? method
    fetch method
  elsif method.to_s.end_with? '='
    assign method.to_s[0..-2], params.first
  elsif @attributes.respond_to? method
    @attributes.send method, *params, &block
  else
    super
  end
end | 
 
  
 
  
    Instance Attribute Details
    
      
      
      
  
  
    #attributes  ⇒ Object  
  
  
  
  
    
Returns the value of attribute attributes.
   
 
  
  
    | 
3
4
5 | # File 'lib/infrastructure/configuration.rb', line 3
def attributes
  @attributes
end | 
 
    
   
  
    Instance Method Details
    
      
  
  
    #after_assignment(*params)  ⇒ Object 
  
  
  
  
    | 
65
66
67 | # File 'lib/infrastructure/configuration.rb', line 65
def after_assignment(*params)
  @base.after_assignment *params
end | 
 
    
      
  
  
    | 
57
58
59 | # File 'lib/infrastructure/configuration.rb', line 57
def after_configure(*params)
  @base.after_configure *params
end | 
 
    
      
  
  
    #assign(key, value = nil)  ⇒ Object 
  
  
  
  
    | 
69
70
71
72
73
74
75
76
77
78 | # File 'lib/infrastructure/configuration.rb', line 69
def assign(key, value = nil)
  if key.is_a?(Hash)
    key.each { |k,v| assign k, v }
  else
    key = key.to_sym
    before_assignment key, value
    @attributes[key] = value.is_a?(Hash) ? Configuration.new(self, value) : value
    after_assignment key, value
  end
end | 
 
    
      
  
  
    #before_assignment(*params)  ⇒ Object 
  
  
  
  
    | 
61
62
63 | # File 'lib/infrastructure/configuration.rb', line 61
def before_assignment(*params)
  @base.before_assignment *params
end | 
 
    
      
  
  
    | 
53
54
55 | # File 'lib/infrastructure/configuration.rb', line 53
def before_configure(*params)
  @base.before_configure *params
end | 
 
    
      
  
  
    #config  ⇒ Object 
  
  
  
  
    | 
18
19
20 | # File 'lib/infrastructure/configuration.rb', line 18
def config
  self
end | 
 
    
      
  
  
    | 
11
12
13
14
15
16 | # File 'lib/infrastructure/configuration.rb', line 11
def configure(&block)
  raise "configure expects a block" unless block
  before_configure
  instance_eval &block
  after_configure
end | 
 
    
      
  
  
    #fetch(key)  ⇒ Object 
  
  
  
  
    | 
49
50
51 | # File 'lib/infrastructure/configuration.rb', line 49
def fetch(key)
  @attributes[key.to_sym]
end | 
 
    
      
  
  
    #load_config(path_to_config_file, &block)  ⇒ Object 
  
  
  
  
    | 
28
29
30
31 | # File 'lib/infrastructure/configuration.rb', line 28
def load_config(path_to_config_file, &block)
  assign YAML.load(ERB.new(File.read(path_to_config_file)).result)
  block.call if block
end | 
 
    
      
  
  
    #load_config_if_exists(path_to_config_file, &block)  ⇒ Object 
  
  
  
  
    | 
22
23
24
25
26 | # File 'lib/infrastructure/configuration.rb', line 22
def load_config_if_exists(path_to_config_file, &block)
  if File.exists? path_to_config_file
   load_config path_to_config_file, &block
  end
end | 
 
    
      
  
  
    #save_config(path_to_config_file)  ⇒ Object 
  
  
  
  
    | 
33
34
35
36
37 | # File 'lib/infrastructure/configuration.rb', line 33
def save_config(path_to_config_file)
  File.open(path_to_config_file, 'w') do |file| 
    file.write to_h(stringify_keys: true).to_yaml
  end
end | 
 
    
      
  
  
    #to_h(params = {})  ⇒ Object 
  
  
  
  
    | 
39
40
41
42
43
44
45
46
47 | # File 'lib/infrastructure/configuration.rb', line 39
def to_h(params = {})
  attributes = @attributes.dup
  if params[:stringify_keys]
    attributes.keys.each { |key| attributes[key.to_s] = attributes.delete(key) unless key.is_a?(String) }
    attributes.each { |key, value| attributes[key] = value.to_h(params) if value.is_a? Configuration }
  else
    attributes.each { |key, value| attributes[key] = value.to_h(params) if value.is_a? Configuration }
  end
end |