lib/chamber/file.rb in chamber-2.10.2 vs lib/chamber/file.rb in chamber-2.11.0

- old
+ new

@@ -1,16 +1,21 @@ # frozen_string_literal: true + require 'pathname' require 'yaml' require 'erb' ### # Internal: Represents a single file containing settings information in a given # file set. # module Chamber class File < Pathname + attr_accessor :namespaces, + :decryption_keys, + :encryption_keys + ### # Internal: Creates a settings file representing a path to a file on the # filesystem. # # Optionally, namespaces may be passed in which will be passed to the Settings @@ -35,13 +40,13 @@ # # # Chamber::File.new path: '/tmp/settings.yml' # # => <Chamber::File> # def initialize(options = {}) - self.namespaces = options[:namespaces] || {} - self.decryption_key = options[:decryption_key] - self.encryption_key = options[:encryption_key] + self.namespaces = options[:namespaces] || {} + self.decryption_keys = options[:decryption_keys] || {} + self.encryption_keys = options[:encryption_keys] || {} super options.fetch(:path) end ### @@ -62,16 +67,17 @@ # settings[:test][:my_dynamic_value] # # => 2 # ``` # def to_settings - @data ||= Settings.new(settings: file_contents_hash, - namespaces: namespaces, - decryption_key: decryption_key, - encryption_key: encryption_key) + @data ||= Settings.new(settings: file_contents_hash, + namespaces: namespaces, + decryption_keys: decryption_keys, + encryption_keys: encryption_keys) end + # rubocop:disable Metrics/LineLength def secure insecure_settings = to_settings.insecure.to_flattened_name_hash secure_settings = to_settings.insecure.secure.to_flattened_name_hash file_contents = read @@ -80,31 +86,34 @@ escaped_name = Regexp.escape(name_pieces.last) escaped_value = Regexp.escape(value) file_contents. - sub!( - /^(\s*)_secure_#{escaped_name}(\s*):(\s*)['"]?#{escaped_value}['"]?$/, - "\\1_secure_#{name_pieces.last}\\2:\\3#{secure_value}", + sub!( + /^(\s*)#{secure_prefix_pattern}#{escaped_name}(\s*):(\s*)['"]?#{escaped_value}['"]?$/, + "\\1#{secure_prefix}#{name_pieces.last}\\2:\\3#{secure_value}", ) file_contents. - sub!( - /^(\s*)_secure_#{escaped_name}(\s*):(\s*)\|((?:\n\1\s{2}.*)+)/, - "\\1_secure_#{name_pieces.last}\\2:\\3#{secure_value}", + sub!( + /^(\s*)#{secure_prefix_pattern}#{escaped_name}(\s*):(\s*)\|((?:\n\1\s{2}.*)+)/, + "\\1#{secure_prefix}#{name_pieces.last}\\2:\\3#{secure_value}", ) end write(file_contents) end + # rubocop:enable Metrics/LineLength - protected + private - attr_accessor :namespaces, - :decryption_key, - :encryption_key + def secure_prefix + '_secure_' + end - private + def secure_prefix_pattern + @secure_prefix_pattern ||= Regexp.escape(secure_prefix) + end def file_contents_hash file_contents = read erb_result = ERB.new(file_contents).result