Class: Vmit::YamlRegistry

Inherits:
Registry show all
Defined in:
lib/vmit/registry.rb

Overview

Takes configuration options from a yml file.

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ YamlRegistry

Returns a new instance of YamlRegistry.



125
126
127
128
# File 'lib/vmit/registry.rb', line 125

def initialize(file_path)
  @file_path = file_path
  reload!
end

Instance Method Details

#[](key) ⇒ Object



138
139
140
141
142
143
144
145
146
# File 'lib/vmit/registry.rb', line 138

def [](key)
  # YAML uses strings for keys
  # we use symbols.
  if @data.has_key?(key)
    @data[key]
  else
    @data[key.to_s]
  end
end

#[]=(key, val) ⇒ Object



148
149
150
151
152
# File 'lib/vmit/registry.rb', line 148

def []=(key, val)
  @data[key.to_s] = val
  save!
  reload!
end

#each(&block) ⇒ Object



154
155
156
157
158
159
160
# File 'lib/vmit/registry.rb', line 154

def each(&block)
  Enumerator.new do |enum|
    @data.each do |key, val|
      enum.yield key.to_sym, val
    end
  end
end

#keysObject



162
163
164
# File 'lib/vmit/registry.rb', line 162

def keys
  each.to_a.map(&:first)
end

#reload!Object



130
131
132
# File 'lib/vmit/registry.rb', line 130

def reload!
  @data = YAML::load(File.read(@file_path))
end

#save!Object



134
135
136
# File 'lib/vmit/registry.rb', line 134

def save!
  File.write(@file_path, @data.to_yaml)
end