Class: TechnoGate::TgConfig

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

Constant Summary collapse

TgConfigError =
Class.new Exception
NotReadableError =
Class.new TgConfigError
NotDefinedError =
Class.new TgConfigError
NotWritableError =
Class.new TgConfigError
NotValidError =
Class.new TgConfigError
IsEmptyError =
Class.new TgConfigError
ConfigFileNotSetError =
Class.new TgConfigError
MAJOR =
0
MINOR =
1
TINY =
4
PRE =
''

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file) ⇒ TgConfig

Returns a new instance of TgConfig.



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

def initialize(config_file)
  @config_file = config_file
  check_config_file
  @config = parse_config_file
end

Instance Attribute Details

#config_fileObject (readonly)

Returns the value of attribute config_file.



12
13
14
# File 'lib/tg_config.rb', line 12

def config_file
  @config_file
end

Class Method Details

.versionObject



8
9
10
11
12
13
14
15
# File 'lib/tg_config/version.rb', line 8

def self.version
  # Init the version
  version = [MAJOR, MINOR, TINY]
  # Add the pre if available
  version << PRE unless PRE.nil? || PRE !~ /\S/
  # Return the version joined by a dot
  version.join('.')
end

Instance Method Details

#[](config) ⇒ Object

Return a particular config variable from the parsed config file

Parameters:

  • config (String|Symbol)

Returns:

  • mixed

Raises:

  • (Void)


25
26
27
28
29
30
31
# File 'lib/tg_config.rb', line 25

def [](config)
  unless @config
    check_config_file
    @config = parse_config_file
  end
  @config.send(:[], config)
end

#[]=(config, value) ⇒ Object

Update the config file

Parameters:

  • config (String)
  • Values (Mixed)


37
38
39
40
41
42
43
# File 'lib/tg_config.rb', line 37

def []=(config, value)
  unless @config
    check_config_file
    @config = parse_config_file
  end
  @config.send(:[]=, config, value)
end

#saveObject

Save the config file



46
47
48
49
50
51
# File 'lib/tg_config.rb', line 46

def save
  # Make sure the config file is writable
  check_config_file(true)
  # Write the config file
  write_config_file
end