Class: GeneralStore

Inherits:
Object
  • Object
show all
Defined in:
lib/general_store.rb,
lib/general_store/version.rb

Constant Summary collapse

VERSION =
"0.0.5"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_contents, dir) ⇒ GeneralStore

Returns a new instance of GeneralStore.



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/general_store.rb', line 25

def initialize config_contents, dir
  self.config = config_contents
  self.dir = dir
  config.each do |k,v|
    accessor = k.to_sym
    self.class.class_eval do
      attr_accessor accessor
    end
    send("#{accessor}=", v)
  end
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



6
7
8
# File 'lib/general_store.rb', line 6

def config
  @config
end

#dirObject

Returns the value of attribute dir.



6
7
8
# File 'lib/general_store.rb', line 6

def dir
  @dir
end

Class Method Details

.config_dir(dir) ⇒ Object



42
43
44
# File 'lib/general_store.rb', line 42

def self.config_dir dir
  File.expand_path dir
end

.config_file(dir_name) ⇒ Object



38
39
40
# File 'lib/general_store.rb', line 38

def self.config_file dir_name
  File.expand_path File.join dir_name, "config.yml"
end

.create(dir, ostruct = OpenStruct.new) {|ostruct| ... } ⇒ Object

Yields:

  • (ostruct)


14
15
16
17
18
# File 'lib/general_store.rb', line 14

def self.create dir, ostruct = OpenStruct.new
  create_config_file dir
  yield ostruct
  new(ostruct.marshal_dump, dir).set
end

.create_config_file(dir) ⇒ Object



46
47
48
49
# File 'lib/general_store.rb', line 46

def self.create_config_file dir
  ensure_dir_existence dir
  ensure_file_existence dir
end

.ensure_dir_existence(dir) ⇒ Object



57
58
59
# File 'lib/general_store.rb', line 57

def self.ensure_dir_existence dir
  FileUtils.mkdir_p config_dir dir
end

.ensure_file_existence(dir) ⇒ Object



61
62
63
64
65
66
# File 'lib/general_store.rb', line 61

def self.ensure_file_existence dir
  file = config_file dir
  unless File.exists? file
    write_file file, {}
  end
end

.read(dir_name) ⇒ Object



8
9
10
11
12
# File 'lib/general_store.rb', line 8

def self.read dir_name
  new YAML.load_file(config_file(dir_name)), dir_name
rescue Errno::ENOENT
  puts 'You need to setup your General Store first!'
end

.write_file(file, data) ⇒ Object



51
52
53
54
55
# File 'lib/general_store.rb', line 51

def self.write_file file, data
  File.open file, File::RDWR|File::TRUNC|File::CREAT, 0600 do |config|
    config.write YAML.dump data
  end
end

Instance Method Details

#setObject



20
21
22
23
# File 'lib/general_store.rb', line 20

def set
  klass = self.class
  klass.write_file klass.config_file(dir), config
end