Class: DotCfg

Inherits:
Object
  • Object
show all
Defined in:
lib/dotcfg.rb

Overview

uses JSON or YAML for serialization top-level structure is object/hash/dict

e.g. d = DotCfg.new(‘~/.dotcfg’) d = ‘world’ d # => “world” d # => nil d.save d = “bent” d.load d # => nil

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

Instance Method Summary collapse

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.expand_path filename
  @format = format
  @cfg = Hash.new
  File.exist?(@filename) ? self.try_load : self.reset
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



31
32
33
# File 'lib/dotcfg.rb', line 31

def filename
  @filename
end

#formatObject (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

#loadObject



91
92
93
# File 'lib/dotcfg.rb', line 91

def load
  File.open(@filename, 'r') { |f| self.deserialize f.read }
end

#prettyObject



79
80
81
# File 'lib/dotcfg.rb', line 79

def pretty
  self.class::PROCS.fetch(@format)[:pretty].call @cfg
end

#resetObject



112
113
114
115
# File 'lib/dotcfg.rb', line 112

def reset
  @cfg = Hash.new
  self.save
end

#saveObject

file operations



87
88
89
# File 'lib/dotcfg.rb', line 87

def save
  File.open(@filename, 'w') { |f| f.write self.serialize }
end

#serializeObject

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_hObject

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_loadObject



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