Class: Otpui::Settings
- Inherits:
-
Object
- Object
- Otpui::Settings
- Defined in:
- lib/otpui/settings.rb
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, val) ⇒ Object
- #add_secret(issuer, secret) ⇒ Object
- #each(&block) ⇒ Object
-
#initialize(yaml = {}) ⇒ Settings
constructor
A new instance of Settings.
- #save ⇒ Object
- #secrets ⇒ Object
Constructor Details
#initialize(yaml = {}) ⇒ Settings
Returns a new instance of Settings.
5 6 7 |
# File 'lib/otpui/settings.rb', line 5 def initialize(yaml = {}) @settings = yaml end |
Class Method Details
.config_dir ⇒ Object
9 10 11 |
# File 'lib/otpui/settings.rb', line 9 def self.config_dir dir = "#{ENV['HOME']}/.config/otpui" end |
.load ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/otpui/settings.rb', line 46 def self.load return @@instance if defined? @@instance Log.debug "Loading settings from #{config_dir}/settings.yaml" file = "#{config_dir}/settings.yaml" if File.exist? file begin @@instance = Settings.new YAML.load_file(file) rescue TypeError @@instance = Settings.new rescue Exception => e Log.error "Error loading settings: #{e}\n#{e.backtrace}" end else @@instance = Settings.new @@instance.save end @@instance end |
Instance Method Details
#[](key) ⇒ Object
25 26 27 |
# File 'lib/otpui/settings.rb', line 25 def [](key) @settings[key] end |
#[]=(key, val) ⇒ Object
29 30 31 |
# File 'lib/otpui/settings.rb', line 29 def []=(key, val) @settings[key] = val end |
#add_secret(issuer, secret) ⇒ Object
41 42 43 44 |
# File 'lib/otpui/settings.rb', line 41 def add_secret(issuer, secret) secrets.merge! issuer => secret save end |
#each(&block) ⇒ Object
33 34 35 |
# File 'lib/otpui/settings.rb', line 33 def each(&block) @settings.each &block end |
#save ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/otpui/settings.rb', line 13 def save dir = Settings.config_dir unless File.directory?(dir) Log.debug "Creating settings directory: #{dir}" Dir.mkdir dir end Log.debug "Saving settings to #{File.join(dir,'settings.yaml')}" File.open(File.join(dir,"settings.yaml"), "w") do |f| f.puts @settings.to_yaml end end |
#secrets ⇒ Object
37 38 39 |
# File 'lib/otpui/settings.rb', line 37 def secrets @settings["secrets"] end |