Class: LXC::Configuration
- Inherits:
-
Object
- Object
- LXC::Configuration
- Includes:
- ConfigurationOptions
- Defined in:
- lib/lxc/configuration.rb
Constant Summary
Constants included from ConfigurationOptions
LXC::ConfigurationOptions::VALID_OPTIONS
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
Class Method Summary collapse
-
.load_file(path) ⇒ LXC::Configuration
Load an existing LXC container configuration from file.
Instance Method Summary collapse
-
#[](key) ⇒ String
Get attribute value.
-
#attributes ⇒ Array
Get all configuration attributes.
-
#initialize(data = nil) ⇒ Configuration
constructor
Initialize a new LXC::Configuration instance.
- #method_missing(key) ⇒ Object
-
#save_to_file(path) ⇒ Object
Save configuration into file.
Constructor Details
#initialize(data = nil) ⇒ Configuration
Initialize a new LXC::Configuration instance
9 10 11 12 13 |
# File 'lib/lxc/configuration.rb', line 9 def initialize(data=nil) if data.kind_of?(String) @content = parse(data) end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(key) ⇒ Object
41 42 43 |
# File 'lib/lxc/configuration.rb', line 41 def method_missing(key) @content[key.to_s] end |
Instance Attribute Details
#content ⇒ Object (readonly)
Returns the value of attribute content.
5 6 7 |
# File 'lib/lxc/configuration.rb', line 5 def content @content end |
Class Method Details
.load_file(path) ⇒ LXC::Configuration
Load an existing LXC container configuration from file
18 19 20 21 22 23 24 25 26 |
# File 'lib/lxc/configuration.rb', line 18 def self.load_file(path) fullpath = File.(path) if !File.exists?(fullpath) raise ArgumentError, "File '#{path}' does not exist." end LXC::Configuration.new(File.read(fullpath)) end |
Instance Method Details
#[](key) ⇒ String
Get attribute value
37 38 39 |
# File 'lib/lxc/configuration.rb', line 37 def [](key) @content[key.to_s] end |
#attributes ⇒ Array
Get all configuration attributes
30 31 32 |
# File 'lib/lxc/configuration.rb', line 30 def attributes @content.keys end |
#save_to_file(path) ⇒ Object
Save configuration into file
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/lxc/configuration.rb', line 47 def save_to_file(path) fullpath = File.(path) lines = [] @content.each_pair do |key,value| k = "lxc.#{key.gsub('_', '.')}" if value.kind_of?(Array) lines << value.map { |v| "#{k} = #{v}" } else lines << "#{k} = #{value}" end end File.open(path, 'w') do |f| f.write(lines.flatten.join("\n")) end end |