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.



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

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::EnvironmentFilter,
                                                          Filters::BooleanConversionFilter,
                                                        ]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



203
204
205
206
207
# File 'lib/chamber/settings.rb', line 203

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.



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

def namespaces
  @namespaces
end

Instance Method Details

#==(other) ⇒ Object

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

Returns a Boolean



135
136
137
# File 'lib/chamber/settings.rb', line 135

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)


144
145
146
147
148
# File 'lib/chamber/settings.rb', line 144

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 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



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/chamber/settings.rb', line 104

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)


209
210
211
# File 'lib/chamber/settings.rb', line 209

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

#secureObject



156
157
158
159
160
161
# File 'lib/chamber/settings.rb', line 156

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

#securedObject



150
151
152
153
154
# File 'lib/chamber/settings.rb', line 150

def secured
  Settings.new( .merge(
                  settings:     raw_data,
                  pre_filters:  [Filters::SecureFilter]))
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



55
56
57
# File 'lib/chamber/settings.rb', line 55

def to_environment
  Hash[SystemEnvironment.extract_from(data).sort]
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



125
126
127
# File 'lib/chamber/settings.rb', line 125

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"'


71
72
73
74
75
76
77
78
79
80
81
# File 'lib/chamber/settings.rb', line 71

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