Class: Chamber::Settings

Inherits:
Object
  • Object
show all
Defined in:
lib/chamber/settings.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Settings

Returns a new instance of Settings.



13
14
15
16
# File 'lib/chamber/settings.rb', line 13

def initialize(options = {})
  self.namespaces = options.fetch(:namespaces,  NamespaceSet.new)
  self.data       = options.fetch(:settings,    Hashie::Mash.new)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



103
104
105
106
107
# File 'lib/chamber/settings.rb', line 103

def method_missing(name, *args)
  return data.public_send(name, *args) if data.respond_to?(name)

  super
end

Instance Attribute Details

#namespacesObject

Returns the value of attribute namespaces.



11
12
13
# File 'lib/chamber/settings.rb', line 11

def namespaces
  @namespaces
end

Instance Method Details

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
96
97
# File 'lib/chamber/settings.rb', line 93

def eql?(other)
  other.is_a?(        Chamber::Settings)  &&
  self.data        == other.data          &&
  self.namespaces  == other.namespaces
end

#merge!(other) ⇒ Object

Internal: Merges a Settings object with another Settings object or a hash-like object.

Also, if merging Settings, it will merge the namespaces as well.

Example:

settings        = Settings.new settings: { my_setting:        'my value' }
other_settings  = Settings.new settings: { my_other_setting:  'my other value' }

settings.merge! other_settings

settings
# => {
  'my_setting'        => 'my value',
  'my_other_setting'  => 'my other value',
}

Returns a Hash



88
89
90
91
# File 'lib/chamber/settings.rb', line 88

def merge!(other)
  self.data       = data.merge(other.to_hash)
  self.namespaces = (namespaces + other.namespaces) if other.respond_to? :namespaces
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/chamber/settings.rb', line 109

def respond_to_missing?(name, include_private = false)
  data.respond_to?(name, include_private)
end

#to_environmentObject

Internal: Converts a Settings object into a hash that is compatible as an environment variable hash.

Example:

settings = Settings.new settings: {
                          my_setting:     'my value',
                          my_sub_setting: {
                            my_sub_sub_setting_1: 'my sub value 1',
                            my_sub_sub_setting_2: 'my sub value 2',
                          }
settings.to_environment
# => {
  'MY_SETTING'                          => 'my value',
  'MY_SUB_SETTING_MY_SUB_SUB_SETTING_1' => 'my sub value 1',
  'MY_SUB_SETTING_MY_SUB_SUB_SETTING_2' => 'my sub value 2',
}

Returns a Hash sorted alphabetically by the names of the keys



39
40
41
# File 'lib/chamber/settings.rb', line 39

def to_environment
  Hash[SystemEnvironment.extract_from(data).sort]
end

#to_hashObject



99
100
101
# File 'lib/chamber/settings.rb', line 99

def to_hash
  data
end

#to_s(options = {}) ⇒ Object

Internal: Converts a Settings object into a String with a format that will work well when working with the shell.

Examples:

Settings.new( settings: {
                my_key:       'my value',
                my_other_key: 'my other value',
              } ).to_s
# => 'MY_KEY="my value" MY_OTHER_KEY="my other value"'


55
56
57
58
59
60
61
62
63
64
65
# File 'lib/chamber/settings.rb', line 55

def to_s(options = {})
  pair_separator       = options[:pair_separator]       || ' '
  value_surrounder     = options[:value_surrounder]     || '"'
  name_value_separator = options[:name_value_separator] || '='

  pairs = to_environment.to_a.map do |pair|
    %Q{#{pair[0]}#{name_value_separator}#{value_surrounder}#{pair[1]}#{value_surrounder}}
  end

  pairs.join(pair_separator)
end