Class: TreasureData::Config

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

Constant Summary collapse

@@path =

class variables

ENV['TREASURE_DATA_CONFIG_PATH'] || ENV['TD_CONFIG_PATH'] || File.join(ENV['HOME'], '.td', 'td.conf')
@@apikey =
nil
@@cl_apikey =

flag to indicate whether an apikey has been provided through the command-line

false
@@endpoint =
nil
@@cl_endpoint =

flag to indicate whether an endpoint has been provided through the command-line

false
@@secure =
true

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



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

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

Class Method Details

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



31
32
33
# File 'lib/td/config.rb', line 31

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

Instance Method Details

#[](cate_key) ⇒ Object



35
36
37
# File 'lib/td/config.rb', line 35

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

#[]=(cate_key, val) ⇒ Object



39
40
41
# File 'lib/td/config.rb', line 39

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

#delete(cate_key) ⇒ Object



43
44
45
# File 'lib/td/config.rb', line 43

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

#read(path = @path) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/td/config.rb', line 47

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



78
79
80
81
# File 'lib/td/config.rb', line 78

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