Module: Berkshelf::Mixin::Config
- Included in:
- Chef::Config
- Defined in:
- lib/berkshelf/mixin/config.rb
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#path ⇒ String?
readonly
The path to the file.
Class Method Summary collapse
Instance Method Summary collapse
-
#[](key) ⇒ Object
Return the configuration value for the given key.
-
#initialize(filepath = nil) ⇒ Object
Create a new configuration file from the given path.
-
#load ⇒ Berkshelf::Mixin::Config
Read and evaluate the contents of the filepath, if one was given at the start.
- #method_missing(m, *args, &block) ⇒ Object
-
#reload! ⇒ Berkshelf::Mixin::Config
Force a reload the contents of this file from disk.
-
#save ⇒ Object
Save the contents of the file to the originally-supplied path.
-
#to_rb ⇒ String
Convert the file back to Ruby.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
129 130 131 132 133 134 135 |
# File 'lib/berkshelf/mixin/config.rb', line 129 def method_missing(m, *args, &block) if args.length > 0 configuration[m.to_sym] = (args.length == 1) ? args[0] : args else configuration[m.to_sym] end end |
Instance Attribute Details
#path ⇒ String? (readonly)
The path to the file.
91 92 93 |
# File 'lib/berkshelf/mixin/config.rb', line 91 def path @path end |
Class Method Details
.included(base) ⇒ Object
4 5 6 |
# File 'lib/berkshelf/mixin/config.rb', line 4 def self.included(base) base.send(:extend, ClassMethods) end |
Instance Method Details
#[](key) ⇒ Object
Return the configuration value for the given key.
125 126 127 |
# File 'lib/berkshelf/mixin/config.rb', line 125 def [](key) configuration[key.to_sym] end |
#initialize(filepath = nil) ⇒ Object
Create a new configuration file from the given path.
97 98 99 100 |
# File 'lib/berkshelf/mixin/config.rb', line 97 def initialize(filepath = nil) @path = filepath ? File.(filepath.to_s) : nil load end |
#load ⇒ Berkshelf::Mixin::Config
Read and evaluate the contents of the filepath, if one was given at the start.
106 107 108 109 110 |
# File 'lib/berkshelf/mixin/config.rb', line 106 def load configuration # Need to call this to make sure it's populated self.instance_eval(IO.read(path), path, 1) if path && File.exists?(path) self end |
#reload! ⇒ Berkshelf::Mixin::Config
Force a reload the contents of this file from disk.
115 116 117 118 119 |
# File 'lib/berkshelf/mixin/config.rb', line 115 def reload! @configuration = nil load self end |
#save ⇒ Object
Save the contents of the file to the originally-supplied path.
138 139 140 |
# File 'lib/berkshelf/mixin/config.rb', line 138 def save File.open(path, 'w+') { |f| f.write(to_rb + "\n") } end |
#to_rb ⇒ String
Convert the file back to Ruby.
145 146 147 |
# File 'lib/berkshelf/mixin/config.rb', line 145 def to_rb configuration.map { |k,v| "#{k}(#{v.inspect})" }.join("\n") end |