Class: Pione::Global::Config
- Inherits:
-
Object
- Object
- Pione::Global::Config
- Defined in:
- lib/pione/global/config.rb
Overview
Config is a class for setting PIONE system configuration.
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
config file path.
Class Method Summary collapse
-
.load(path) ⇒ Object
Load configuration and apply it to global settings.
Instance Method Summary collapse
-
#apply ⇒ Object
Apply config date to global settings.
-
#get(name) ⇒ Object
Get the global item's value.
-
#initialize(path) ⇒ Config
constructor
Create a new configuration.
-
#save(path = @path) ⇒ void
Save the configuration items to the file.
-
#set(name, value) ⇒ void
Set the global item.
- #unset(name) ⇒ Object
Constructor Details
#initialize(path) ⇒ Config
Create a new configuration.
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
#path ⇒ Object (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
#apply ⇒ Object
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.
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.
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.
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 |