Module: Berkshelf::Mixin::Config

Included in:
Chef::Config
Defined in:
lib/berkshelf/mixin/config.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#pathString? (readonly)

The path to the file.

Returns:



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.

Parameters:

  • key (#to_sym)

    they key to find a configuration value for



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.

Parameters:

  • filepath (#to_s, nil) (defaults to: nil)

    the filepath to read



97
98
99
100
# File 'lib/berkshelf/mixin/config.rb', line 97

def initialize(filepath = nil)
  @path = filepath ? File.expand_path(filepath.to_s) : nil
  load
end

#loadBerkshelf::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

#saveObject

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_rbString

Convert the file back to Ruby.

Returns:



145
146
147
# File 'lib/berkshelf/mixin/config.rb', line 145

def to_rb
  configuration.map { |k,v| "#{k}(#{v.inspect})" }.join("\n")
end