lib/chamber/settings.rb in chamber-2.13.1 vs lib/chamber/settings.rb in chamber-2.14.0

- old
+ new

@@ -14,33 +14,44 @@ ### # Internal: Represents the base settings storage needed for Chamber. # module Chamber class Settings - attr_accessor :pre_filters, - :post_filters, + attr_accessor :decryption_keys, :encryption_keys, - :decryption_keys + :post_filters, + :pre_filters, + :secure_key_prefix attr_reader :namespaces - # rubocop:disable Metrics/CyclomaticComplexity, Metrics/LineLength - def initialize(options = {}) - self.namespaces = options[:namespaces] || [] - self.raw_data = options[:settings] || {} - self.decryption_keys = options[:decryption_keys] || {} - self.encryption_keys = options[:encryption_keys] || {} - self.pre_filters = options[:pre_filters] || [ - Filters::NamespaceFilter, - ] - self.post_filters = options[:post_filters] || [ - Filters::DecryptionFilter, - Filters::EnvironmentFilter, - Filters::FailedDecryptionFilter, - Filters::TranslateSecureKeysFilter, - ] + # rubocop:disable Metrics/ParameterLists + def initialize( + decryption_keys: {}, + encryption_keys: {}, + namespaces: [], + pre_filters: [ + Filters::NamespaceFilter, + ], + post_filters: [ + Filters::DecryptionFilter, + Filters::EnvironmentFilter, + Filters::FailedDecryptionFilter, + Filters::TranslateSecureKeysFilter, + ], + secure_key_prefix: '_secure_', + settings: {}, + **_args + ) + self.decryption_keys = decryption_keys + self.encryption_keys = encryption_keys + self.namespaces = namespaces + self.post_filters = post_filters + self.pre_filters = pre_filters + self.raw_data = settings + self.secure_key_prefix = secure_key_prefix end - # rubocop:enable Metrics/CyclomaticComplexity, Metrics/LineLength + # rubocop:enable Metrics/ParameterLists ### # Internal: Converts a Settings object into a hash that is compatible as an # environment variable hash. # @@ -77,19 +88,15 @@ # my_key: 'my value', # my_other_key: 'my other value', # } ).to_s # # => 'MY_KEY="my value" MY_OTHER_KEY="my other value"' # - 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| + def to_s(hierarchical_separator: '_', + pair_separator: ' ', + value_surrounder: '"', + name_value_separator: '=') + pairs = to_concatenated_name_hash(hierarchical_separator).to_a.map do |key, value| "#{key.upcase}#{name_value_separator}#{value_surrounder}#{value}#{value_surrounder}" end pairs.join(pair_separator) end @@ -180,24 +187,25 @@ # } # # Returns a new Settings object # def merge(other) - other_settings = if other.is_a? Settings + other_settings = case other + when Settings other - elsif other.is_a? Hash + when Hash Settings.new(settings: other) end - # rubocop:disable Metrics/LineLength + # rubocop:disable Layout/LineLength Settings.new( encryption_keys: encryption_keys.any? ? encryption_keys : other_settings.encryption_keys, decryption_keys: decryption_keys.any? ? decryption_keys : other_settings.decryption_keys, namespaces: (namespaces + other_settings.namespaces), settings: raw_data.merge(other_settings.raw_data), ) - # rubocop:enable Metrics/LineLength + # rubocop:enable Layout/LineLength end ### # Internal: Determines whether a Settings is equal to another hash-like # object. @@ -218,26 +226,26 @@ data == other.data && namespaces == other.namespaces end def securable - Settings.new(metadata.merge( + Settings.new(**metadata.merge( settings: raw_data, pre_filters: [Filters::SecureFilter], )) end def secure - Settings.new(metadata.merge( + Settings.new(**metadata.merge( settings: raw_data, pre_filters: [Filters::EncryptionFilter], post_filters: [Filters::TranslateSecureKeysFilter], )) end def insecure - Settings.new(metadata.merge( + Settings.new(**metadata.merge( settings: raw_data, pre_filters: [Filters::InsecureFilter], post_filters: [Filters::TranslateSecureKeysFilter], )) end @@ -263,24 +271,18 @@ end # rubocop:disable Naming/MemoizedInstanceVariableName def raw_data @filtered_raw_data ||= pre_filters.inject(@raw_data) do |filtered_data, filter| - filter.execute({ data: filtered_data }. - merge(metadata)) + filter.execute(**{ data: filtered_data }.merge(metadata)) end end # rubocop:enable Naming/MemoizedInstanceVariableName def data @data ||= post_filters.inject(raw_data) do |filtered_data, filter| - filter.execute({ data: filtered_data }. - merge(metadata)) + filter.execute(**{ data: filtered_data }.merge(metadata)) end - end - - def secure_key_prefix - '_secure_' end def metadata { decryption_keys: decryption_keys,