Class: FReCon::Configuration

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

Overview

Public: A wrapper to allow the manipulation of configurations.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Configuration

Public: Initialize a Configuration.

data - a Hash representing the data.



18
19
20
21
22
# File 'lib/frecon/configuration.rb', line 18

def initialize(data)
  data.each do |key, value|
    self[key] = value
  end
end

Class Method Details

.construct!(default_configuration: ConfigurationFile.default.read, system_configuration: ConfigurationFile.system.read, user_configuration: ConfigurationFile.user.read, argument_configuration: nil) ⇒ Object

Public: Constructs a configuration.

options - A Hash containing various configurations.

:default_configuration  - The default configuration's values
:system_configuration   - The system's configuration's values
:user_configuration     - The user's configuration's values
:argument_configuration - The configuration values from command-line arguments

Returns a Configuration generated by merging all of the given configurations together.



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/frecon/configuration.rb', line 77

def self.construct!(default_configuration: ConfigurationFile.default.read,
                    system_configuration: ConfigurationFile.system.read,
                    user_configuration: ConfigurationFile.user.read,
                    argument_configuration: nil)
  configuration_hierarchy = [default_configuration, system_configuration, user_configuration, argument_configuration]

  configuration = Configuration.new({})
  configuration_hierarchy.each do |other_configuration|
    configuration.merge(other_configuration)
  end

  configuration
end

Instance Method Details

#merge(other) ⇒ Object

Public: Merge with another Configuration.

Sets all key-value pairs within Configuration to the same within self.

other - A Configuration or Hash to be merged with.



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

def merge(other)
  case other
  when Configuration, Hash
    other.each do |key, value|
      case value
      when Configuration, Hash
        me = Configuration.new(self[key] || {})
        me.merge(Configuration.new(value))
        self[key] = me
      else
        self[key] = value
      end
    end
  when nil
  end
end

#to_hObject

Public: Convert self to a Hash.

Recursively converts instances of Configuration within self to hashes by calling this method.

Returns a Hash representing self.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/frecon/configuration.rb', line 30

def to_h
  hash = {}

  self.each do |key, value|
    case value
    when Configuration
      hash[key] = value.to_h
    else
      hash[key] = value
    end
  end

  hash
end