Class: Chamber::Settings
- Inherits:
-
Object
- Object
- Chamber::Settings
- Defined in:
- lib/chamber/settings.rb
Instance Attribute Summary collapse
-
#namespaces ⇒ Object
readonly
Returns the value of attribute namespaces.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Internal: Determines whether a Settings is equal to another hash-like object.
-
#eql?(other) ⇒ Boolean
Internal: Determines whether a Settings is equal to another Settings.
-
#initialize(options = {}) ⇒ Settings
constructor
A new instance of Settings.
-
#merge(other) ⇒ Object
Internal: Merges a Settings object with another Settings object or a hash-like object.
- #method_missing(name, *args) ⇒ Object
- #respond_to_missing?(name, include_private = false) ⇒ Boolean
- #secure ⇒ Object
- #secured ⇒ Object
-
#to_environment ⇒ Object
Internal: Converts a Settings object into a hash that is compatible as an environment variable hash.
-
#to_hash ⇒ Object
Internal: Returns the Settings data as a Hash for easy manipulation.
-
#to_s(options = {}) ⇒ Object
Internal: Converts a Settings object into a String with a format that will work well when working with the shell.
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( = {}) self.namespaces = [:namespaces] || [] self.raw_data = [:settings] || {} self.decryption_key = [:decryption_key] self.encryption_key = [:encryption_key] self.pre_filters = [:pre_filters] || [ Filters::NamespaceFilter, ] self.post_filters = [: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
#namespaces ⇒ Object
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
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
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 |
#secure ⇒ Object
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 |
#secured ⇒ Object
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_environment ⇒ Object
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_hash ⇒ Object
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
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/chamber/settings.rb', line 71 def to_s( = {}) pair_separator = [:pair_separator] || ' ' value_surrounder = [:value_surrounder] || '"' name_value_separator = [: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 |