Class: Chamber::File

Inherits:
Pathname show all
Defined in:
lib/chamber/file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Pathname

#write

Constructor Details

#initialize(options = {}) ⇒ File

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 object for consideration of which data will be parsed (see the Settings object’s documentation for details on how this works).

Examples:

###
# It can be created by passing namespaces
#
settings_file = Chamber::File.new path:       '/tmp/settings.yml',
                                  namespaces: {
                                    environment: ENV['RAILS_ENV'] }
# => <Chamber::File>

settings_file.to_settings
# => <Chamber::Settings>

###
# It can also be created without passing any namespaces
#
Chamber::File.new path: '/tmp/settings.yml'
# => <Chamber::File>


44
45
46
47
48
49
50
# File 'lib/chamber/file.rb', line 44

def initialize(options = {})
  self.namespaces      = options[:namespaces]      || {}
  self.decryption_keys = options[:decryption_keys] || {}
  self.encryption_keys = options[:encryption_keys] || {}

  super options.fetch(:path)
end

Instance Attribute Details

#decryption_keysObject

Returns the value of attribute decryption_keys.



13
14
15
# File 'lib/chamber/file.rb', line 13

def decryption_keys
  @decryption_keys
end

#encryption_keysObject

Returns the value of attribute encryption_keys.



13
14
15
# File 'lib/chamber/file.rb', line 13

def encryption_keys
  @encryption_keys
end

#namespacesObject

Returns the value of attribute namespaces.



13
14
15
# File 'lib/chamber/file.rb', line 13

def namespaces
  @namespaces
end

Instance Method Details

#secureObject

rubocop:disable Metrics/LineLength



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/chamber/file.rb', line 79

def secure
  insecure_settings = to_settings.insecure.to_flattened_name_hash
  secure_settings   = to_settings.insecure.secure.to_flattened_name_hash
  file_contents     = read

  insecure_settings.each_pair do |name_pieces, value|
    secure_value  = secure_settings[name_pieces]

    escaped_name  = Regexp.escape(name_pieces.last)
    escaped_value = Regexp.escape(value)

    file_contents.
      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_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

#to_settingsObject

Internal: Extracts the data from the file on disk. First passing it through ERB to generate any dynamic properties and then passing the resulting data through YAML.

Therefore if a settings file contains something like:

“‘erb test:

my_dynamic_value: <%= 1 + 1 %>

“‘

then the resulting settings object would have:

“‘ruby settings[:my_dynamic_value] # => 2 “`



71
72
73
74
75
76
# File 'lib/chamber/file.rb', line 71

def to_settings
  @data ||= Settings.new(settings:        file_contents_hash,
                         namespaces:      namespaces,
                         decryption_keys: decryption_keys,
                         encryption_keys: encryption_keys)
end