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.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/chamber/settings.rb', line 20

def initialize(options = {})
  self.namespaces       = options[:namespaces]      ||  []
  self.raw_data         = options[:settings]        ||  {}
  self.decryption_key   = options[:decryption_key]
  self.encryption_key   = options[:encryption_key]
  self.pre_filters      = options[:pre_filters]     ||  [
                                                          Filters::NamespaceFilter,
                                                        ]
  self.post_filters     = options[:post_filters]    ||  [
                                                          Filters::DecryptionFilter,
                                                          Filters::TranslateSecureKeysFilter,
                                                          Filters::EnvironmentFilter,
                                                          Filters::BooleanConversionFilter,
                                                        ]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



271
272
273
274
275
# File 'lib/chamber/settings.rb', line 271

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.



18
19
20
# File 'lib/chamber/settings.rb', line 18

def namespaces
  @namespaces
end

Instance Method Details

#==(other) ⇒ Object

Internal: Determines whether a Settings is equal to another hash-like object.

Returns a Boolean



196
197
198
# File 'lib/chamber/settings.rb', line 196

def ==(other)
  self.to_hash == other.to_hash
end

#eql?(other) ⇒ Boolean

Internal: Determines whether a Settings is equal to another Settings.

Returns a Boolean

Returns:

  • (Boolean)


205
206
207
208
209
# File 'lib/chamber/settings.rb', line 205

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

#insecureObject



224
225
226
227
228
229
# File 'lib/chamber/settings.rb', line 224

def insecure
  Settings.new( .merge(
                  settings:     raw_data,
                  pre_filters:  [Filters::InsecureFilter],
                  post_filters: [Filters::TranslateSecureKeysFilter] ))
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 all other Settings data 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 new Settings object



176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/chamber/settings.rb', line 176

def merge(other)
  other_settings = if other.is_a? Settings
                     other
                   elsif other.is_a? Hash
                     Settings.new(settings: other)
                   end

  Settings.new(
    encryption_key: encryption_key || other_settings.encryption_key,
    decryption_key: decryption_key || other_settings.decryption_key,
    namespaces:     (namespaces + other_settings.namespaces),
    settings:       raw_data.merge(other_settings.raw_data))
end

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

Returns:

  • (Boolean)


277
278
279
# File 'lib/chamber/settings.rb', line 277

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

#securableObject



211
212
213
214
215
# File 'lib/chamber/settings.rb', line 211

def securable
  Settings.new( .merge(
                  settings:     raw_data,
                  pre_filters:  [Filters::SecureFilter]))
end

#secureObject



217
218
219
220
221
222
# File 'lib/chamber/settings.rb', line 217

def secure
  Settings.new( .merge(
                  settings:     raw_data,
                  pre_filters:  [Filters::EncryptionFilter],
                  post_filters: [Filters::TranslateSecureKeysFilter] ))
end

#to_concatenated_name_hash(hierarchical_separator = '_') ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
# File 'lib/chamber/settings.rb', line 143

def to_concatenated_name_hash(hierarchical_separator = '_')
  concatenated_name_hash = {}

  to_flattened_name_hash.each_pair do |flattened_name, value|
    concatenated_name = flattened_name.join(hierarchical_separator)

    concatenated_name_hash[concatenated_name] = value
  end

  concatenated_name_hash.sort
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



57
58
59
60
61
# File 'lib/chamber/settings.rb', line 57

def to_environment
  to_concatenated_name_hash('_').each_with_object({}) do |pair, env_hash|
    env_hash[pair[0].upcase] = pair[1].to_s
  end
end

#to_flattened_name_hash(hash = data, parent_keys = []) ⇒ Object

Internal: Returns a hash which contains the flattened name hierarchy of the setting as the keys and the values of each setting as the value.

Examples:

Settings.new(settings: {
               my_setting: 'value',
               there:      'was not that easy?',
               level_1:    {
                 level_2:    {
                   some_setting: 'hello',
                   another:      'goodbye',
                 },
                 body:       'gracias',
               },
             }).to_flattened_name_hash
# => {
  ['my_setting']                         => 'value',
  ['there']                              => 'was not that easy?',
  ['level_1', 'level_2', 'some_setting'] => 'hello',
  ['level_1', 'level_2', 'another']      => 'goodbye',
  ['level_1', 'body']                    => 'gracias',
}

Returns a Hash



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/chamber/settings.rb', line 127

def to_flattened_name_hash(hash = data, parent_keys = [])
  flattened_name_hash = {}

  hash.each_pair do |key, value|
    flattened_name_components = parent_keys.dup.push(key)

    if value.respond_to?(:each_pair)
      flattened_name_hash.merge! to_flattened_name_hash(value, flattened_name_components)
    else
      flattened_name_hash[flattened_name_components] = value
    end
  end

  flattened_name_hash
end

#to_hashObject

Internal: Returns the Settings data as a Hash for easy manipulation. Changes made to the hash will not be reflected in the original Settings object.

Returns a Hash



97
98
99
# File 'lib/chamber/settings.rb', line 97

def to_hash
  data.to_hash
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"'


75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/chamber/settings.rb', line 75

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

  concatenated_name_hash = to_concatenated_name_hash(hierarchical_separator)

  pairs = concatenated_name_hash.to_a.map do |key, value|
    %Q{#{key.upcase}#{name_value_separator}#{value_surrounder}#{value}#{value_surrounder}}
  end

  pairs.join(pair_separator)
end