Class: DotCfg
- Inherits:
-
Object
- Object
- DotCfg
- Defined in:
- lib/dotcfg.rb
Overview
Constant Summary collapse
- PROCS =
{ json: { to: proc { |data| data.to_json }, from: proc { |json| JSON.parse json }, # YAML.load json }, pretty: proc { |data| JSON.pretty_generate data }, }, yaml: { to: proc { |data| data.to_yaml }, from: proc { |yaml| YAML.load yaml }, pretty: proc { |data| data.to_yaml }, }, }
Instance Attribute Summary collapse
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #delete(key) ⇒ Object
- #deserialize(junk) ⇒ Object
-
#initialize(filename, format = :yaml) ⇒ DotCfg
constructor
A new instance of DotCfg.
- #load ⇒ Object
- #pretty ⇒ Object
- #reset ⇒ Object
-
#save ⇒ Object
file operations.
-
#serialize ⇒ Object
serialization, using PROCS.
-
#to_h ⇒ Object
if you need to call this, you might be Doing It Wrong (tm).
- #try_load ⇒ Object
Constructor Details
#initialize(filename, format = :yaml) ⇒ DotCfg
Returns a new instance of DotCfg.
33 34 35 36 37 38 |
# File 'lib/dotcfg.rb', line 33 def initialize filename, format = :yaml @filename = File. filename @format = format @cfg = Hash.new File.exist?(@filename) ? self.try_load : self.reset end |
Instance Attribute Details
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
31 32 33 |
# File 'lib/dotcfg.rb', line 31 def filename @filename end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
31 32 33 |
# File 'lib/dotcfg.rb', line 31 def format @format end |
Instance Method Details
#[](key) ⇒ Object
44 45 46 |
# File 'lib/dotcfg.rb', line 44 def [] key @cfg[key] end |
#[]=(key, value) ⇒ Object
48 49 50 |
# File 'lib/dotcfg.rb', line 48 def []= key, value @cfg[key] = value end |
#delete(key) ⇒ Object
52 53 54 |
# File 'lib/dotcfg.rb', line 52 def delete key @cfg.delete key end |
#deserialize(junk) ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/dotcfg.rb', line 70 def deserialize junk data = self.class::PROCS.fetch(@format)[:from].call junk unless data.is_a? Hash raise ArgumentError, "invalid junk: #{junk} (#{junk.class})" end data.each { |k, v| self[k] = v } @cfg end |
#load ⇒ Object
91 92 93 |
# File 'lib/dotcfg.rb', line 91 def load File.open(@filename, 'r') { |f| self.deserialize f.read } end |
#pretty ⇒ Object
79 80 81 |
# File 'lib/dotcfg.rb', line 79 def pretty self.class::PROCS.fetch(@format)[:pretty].call @cfg end |
#reset ⇒ Object
112 113 114 115 |
# File 'lib/dotcfg.rb', line 112 def reset @cfg = Hash.new self.save end |
#save ⇒ Object
file operations
87 88 89 |
# File 'lib/dotcfg.rb', line 87 def save File.open(@filename, 'w') { |f| f.write self.serialize } end |
#serialize ⇒ Object
serialization, using PROCS
65 66 67 68 |
# File 'lib/dotcfg.rb', line 65 def serialize raise "invalid storage" unless @cfg.is_a? Hash self.class::PROCS.fetch(@format)[:to].call @cfg end |
#to_h ⇒ Object
if you need to call this, you might be Doing It Wrong (tm)
57 58 59 |
# File 'lib/dotcfg.rb', line 57 def to_h @cfg end |
#try_load ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/dotcfg.rb', line 95 def try_load rescues = 0 begin self.load rescue SystemCallError, ArgumentError, JSON::ParserError => e rescues += 1 puts "#{e} (#{e.class})" if rescues < 2 puts "Resetting #{@filename}" self.reset retry end puts "try_load failed!" raise e end end |