Class: Pione::Global::Config

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

Overview

Config is a class for setting PIONE system configuration.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Config

Create a new configuration.

Raises:



16
17
18
19
20
21
22
23
# File 'lib/pione/global/config.rb', line 16

def initialize(path)
  @path = Pathname.new(path)
  @table = Hash.new
  (@path.exist? ? JSON.load(@path.read) : {}).each do |key, val|
    @table[key.to_sym] = val
  end
  raise InvalidConfigFile.new(@path) unless @table.kind_of?(Hash)
end

Instance Attribute Details

#pathObject (readonly)

config file path



13
14
15
# File 'lib/pione/global/config.rb', line 13

def path
  @path
end

Class Method Details

.load(path) ⇒ Object

Load configuration and apply it to global settings.



6
7
8
# File 'lib/pione/global/config.rb', line 6

def self.load(path)
  new(path).tap {|x| x.apply}
end

Instance Method Details

#applyObject

Apply config date to global settings.



26
27
28
29
30
31
32
33
# File 'lib/pione/global/config.rb', line 26

def apply
  @table.each do |key, val|
    name = key.to_sym
    if_configurable_item(name) do
      Global.set(name, val)
    end
  end
end

#get(name) ⇒ Object

Get the global item's value.

Parameters:

  • name (String)

    item name

Returns:

  • (Object)

    item value



57
58
59
60
61
62
# File 'lib/pione/global/config.rb', line 57

def get(name)
  name = name.to_sym
  if_configurable_item(name) do
    @table[name]
  end
end

#save(path = @path) ⇒ void

This method returns an undefined value.

Save the configuration items to the file.

Parameters:

  • path (Pathname) (defaults to: @path)

    file path



74
75
76
77
78
# File 'lib/pione/global/config.rb', line 74

def save(path=@path)
  path.open("w+") do |file|
    file.write(JSON.pretty_generate(@table))
  end
end

#set(name, value) ⇒ void

This method returns an undefined value.

Set the global item.

Parameters:

  • name (String)

    item name

  • value (Object)

    item value

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
# File 'lib/pione/global/config.rb', line 42

def set(name, value)
  raise ArgumentError.new if value.nil?

  name = name.to_sym
  if_configurable_item(name) do
    @table[name] = ValueConverter.convert(Global.item[name].type, value)
  end
end

#unset(name) ⇒ Object



64
65
66
67
# File 'lib/pione/global/config.rb', line 64

def unset(name)
  name = name.to_sym
  @table.delete(name)
end