Class: XKPassword::ConfigFile
- Inherits:
-
Object
- Object
- XKPassword::ConfigFile
- Defined in:
- lib/xkpassword/config_file.rb
Overview
Loads and initializes the CLI config file stored at ~/.xkpassword.
Constant Summary collapse
- Error =
Class.new(StandardError)
- ALLOWED_OPTIONS =
%i[ preset words min_length max_length separator case_transform ].freeze
- INTEGER_OPTIONS =
%i[words min_length max_length].freeze
- TEMPLATE =
<<~YAML # Global defaults for the xkpassword CLI. # Remove the leading "#" from any setting you want to enable. # # Supported keys: # preset: xkcd, web32, wifi, security, apple_id # words: integer # min_length: integer # max_length: integer # separator: string # case_transform: upcase, downcase, capitalize # # Example: # preset: wifi # words: 6 # min_length: 4 # max_length: 8 # separator: "-" # case_transform: capitalize YAML
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
- #init! ⇒ Object
-
#initialize(path = self.class.default_path) ⇒ ConfigFile
constructor
A new instance of ConfigFile.
- #load ⇒ Object
Constructor Details
#initialize(path = self.class.default_path) ⇒ ConfigFile
Returns a new instance of ConfigFile.
42 43 44 |
# File 'lib/xkpassword/config_file.rb', line 42 def initialize(path = self.class.default_path) @path = path end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
40 41 42 |
# File 'lib/xkpassword/config_file.rb', line 40 def path @path end |
Class Method Details
.default_path(env = ENV) ⇒ Object
46 47 48 49 |
# File 'lib/xkpassword/config_file.rb', line 46 def self.default_path(env = ENV) home = env['HOME'] || Dir.home File.('.xkpassword', home) end |
Instance Method Details
#init! ⇒ Object
63 64 65 66 67 68 |
# File 'lib/xkpassword/config_file.rb', line 63 def init! raise Error, "#{path} already exists" if File.exist?(path) File.write(path, TEMPLATE) path end |
#load ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/xkpassword/config_file.rb', line 51 def load return {} unless File.exist?(path) data = Psych.safe_load(File.read(path), aliases: false) return {} if data.nil? raise Error, "#{path} must contain a YAML mapping of options" unless data.is_a?(Hash) (data) rescue Psych::SyntaxError => e raise Error, "Could not parse #{path}: #{e.}" end |