Class: SproutPunk::ConfigFile

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

Direct Known Subclasses

Config

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ ConfigFile

Returns a new instance of ConfigFile.



18
19
20
# File 'lib/sproutpunk/config_file.rb', line 18

def initialize(hash = {})
  @table = hash
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sproutpunk/config_file.rb', line 55

def method_missing(meth, *args)
  name = meth.to_s
  if name =~ /(.*)\=$/
    @table[$1] = args.first
  elsif @table.has_key?(name)
    val = @table[name]
    val.is_a?(Hash) ? @table[name] = self.class.new(val) : val
  else
    super
  end
end

Class Method Details

.default_pathObject



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

def self.default_path
  nil
end

.load(path = self.default_path) ⇒ Object



14
15
16
# File 'lib/sproutpunk/config_file.rb', line 14

def self.load(path = self.default_path)
  new.load(path)
end

Instance Method Details

#config_dirObject



47
48
49
# File 'lib/sproutpunk/config_file.rb', line 47

def config_dir
  File.dirname(set?(:config_path) ? config_path : self.class.default_path)
end

#default_pathObject



10
11
12
# File 'lib/sproutpunk/config_file.rb', line 10

def default_path
  self.class.default_path
end

#inspectObject



43
44
45
# File 'lib/sproutpunk/config_file.rb', line 43

def inspect
  "#<SproutPunk::ConfigFile #{@table.inspect}>"
end

#load(path = self.default_path) ⇒ Object



22
23
24
25
26
27
# File 'lib/sproutpunk/config_file.rb', line 22

def load(path = self.default_path)
  check_path(path)
  @table = YAML.load_file(path) || {}
  self.config_path = path
  self
end

#root_dirObject



39
40
41
# File 'lib/sproutpunk/config_file.rb', line 39

def root_dir
  File.expand_path(@table['root_dir'] || '')
end

#set?(key) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/sproutpunk/config_file.rb', line 51

def set?(key)
  @table.has_key?(key.to_s)
end

#to_hashObject



35
36
37
# File 'lib/sproutpunk/config_file.rb', line 35

def to_hash
  @table.dup
end

#write(path = self.default_path) ⇒ Object



29
30
31
32
33
# File 'lib/sproutpunk/config_file.rb', line 29

def write(path = self.default_path)
  check_path(path)
  File.open(path, 'w') {|f| f << YAML.dump(@table) }
  path
end