Class: Twterm::PersistableConfigurationProxy
- Inherits:
-
Object
- Object
- Twterm::PersistableConfigurationProxy
- Defined in:
- lib/twterm/persistable_configuration_proxy.rb
Class Method Summary collapse
- .getc ⇒ Object
-
.load_from_file!(klass, filepath) ⇒ Twterm::PersistableConfigurationProxy
Loads a configuration file from and returns a proxy.
Instance Method Summary collapse
- #[](*args) ⇒ Object
- #[]=(*args) ⇒ Object
-
#initialize(instance, filepath) ⇒ PersistableConfigurationProxy
constructor
A new instance of PersistableConfigurationProxy.
- #migrate! ⇒ self
Constructor Details
#initialize(instance, filepath) ⇒ PersistableConfigurationProxy
Returns a new instance of PersistableConfigurationProxy.
7 8 9 10 |
# File 'lib/twterm/persistable_configuration_proxy.rb', line 7 def initialize(instance, filepath) @instance = instance @filepath = filepath end |
Class Method Details
.getc ⇒ Object
21 22 23 24 25 26 |
# File 'lib/twterm/persistable_configuration_proxy.rb', line 21 def self.getc system('stty raw -echo') STDIN.getc ensure system('stty -raw echo') end |
.load_from_file!(klass, filepath) ⇒ Twterm::PersistableConfigurationProxy
Loads a configuration file from and returns a proxy. Falls back to the default value when the specified file does not exist.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/twterm/persistable_configuration_proxy.rb', line 34 def self.load_from_file!(klass, filepath) config = TomlRB.load_file(filepath, symbolize_keys: true) new(klass.new(config), filepath).migrate! rescue Errno::ENOENT new(klass.default, filepath) rescue TomlRB::ParseError, TomlRB::ValueOverwriteError => e msg = case e when TomlRB::ParseError "Your configuration file could not be parsed" when TomlRB::ValueOverwriteError "`#{e.key}` is declared more than once" end warn <<-EOS \e[1mCould not load the configuration file: #{filepath}\e[0m (#{msg}) Falling back to the default key assignments Check the syntax and edit the file manually, or remove it and launch twterm again to restore Press any key to continue EOS getc new(klass.default, filepath) end |
Instance Method Details
#[](*args) ⇒ Object
12 13 14 |
# File 'lib/twterm/persistable_configuration_proxy.rb', line 12 def [](*args) instance.[](*args) end |
#[]=(*args) ⇒ Object
16 17 18 19 |
# File 'lib/twterm/persistable_configuration_proxy.rb', line 16 def []=(*args) instance.[]=(*args) persist! end |
#migrate! ⇒ self
66 67 68 69 70 71 |
# File 'lib/twterm/persistable_configuration_proxy.rb', line 66 def migrate! instance.complete_missing_items! persist! self end |