Class: Tagcrumbs::ConfigStore

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

Overview

Use the ConfigStore to save configuration options to a yaml file

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, options = {}) ⇒ ConfigStore

Returns a new instance of ConfigStore.



6
7
8
9
# File 'lib/tagcrumbs/config_store.rb', line 6

def initialize(filename, options ={})      
  @filename = filename
  @options = options
end

Instance Attribute Details

#filenameObject

Returns the value of attribute filename.



4
5
6
# File 'lib/tagcrumbs/config_store.rb', line 4

def filename
  @filename
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/tagcrumbs/config_store.rb', line 4

def options
  @options
end

Class Method Details

.load(filename) ⇒ Object

Load a filename and read it with YAML



12
13
14
15
# File 'lib/tagcrumbs/config_store.rb', line 12

def self.load(filename)
  config = File.exist?(filename) ? YAML::load(open(filename)) : {}
  new(filename, config)
end

Instance Method Details

#save(file = filename) ⇒ Object

Save the current options into the file in YAML format



25
26
27
28
# File 'lib/tagcrumbs/config_store.rb', line 25

def save(file = filename)
  File.open(file, 'w') { |f| f.write(YAML.dump(@options.stringify_keys)) }
  self
end

#update(c = {}) ⇒ Object

Update the current options and save the file



18
19
20
21
22
# File 'lib/tagcrumbs/config_store.rb', line 18

def update(c={})
  @options.merge!(c)
  save
  self
end