Class: TreasureData::Config

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

Constant Summary collapse

@@apikey =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



16
17
18
19
# File 'lib/td/config.rb', line 16

def initialize
  @path = nil
  @conf = {}   # section.key = val
end

Class Method Details

.read(path = Config.path, create = false) ⇒ Object



21
22
23
# File 'lib/td/config.rb', line 21

def self.read(path=Config.path, create=false)
  new.read(path)
end

Instance Method Details

#[](cate_key) ⇒ Object



25
26
27
# File 'lib/td/config.rb', line 25

def [](cate_key)
  @conf[cate_key]
end

#[]=(cate_key, val) ⇒ Object



29
30
31
# File 'lib/td/config.rb', line 29

def []=(cate_key, val)
  @conf[cate_key] = val
end

#delete(cate_key) ⇒ Object



33
34
35
# File 'lib/td/config.rb', line 33

def delete(cate_key)
  @conf.delete(cate_key)
end

#read(path = @path) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/td/config.rb', line 37

def read(path=@path)
  @path = path
  begin
    data = File.read(@path)
  rescue
    e = ConfigNotFoundError.new($!.to_s)
    e.set_backtrace($!.backtrace)
    raise e
  end

  section = ""

  data.each_line {|line|
    line.strip!
    case line
    when /^#/
      next
    when /\[(.+)\]/
      section = $~[1]
    when /^(\w+)\s*=\s*(.+?)\s*$/
      key = $~[1]
      val = $~[2]
      @conf["#{section}.#{key}"] = val
    else
      raise ConfigParseError, "invalid config line '#{line}' at #{@path}"
    end
  }

  self
end

#save(path = @path||Config.path) ⇒ Object



68
69
70
71
# File 'lib/td/config.rb', line 68

def save(path=@path||Config.path)
  @path = path
  write
end