class Buff::Config::Ruby

Public Class Methods

from_file(path) click to toggle source

@param [String] path

@raise [Buff::Errors::ConfigNotFound]

@return [Buff::Config::Ruby]

# File lib/buff/config/ruby.rb, line 73
def from_file(path)
  path = File.expand_path(path)
  contents = File.read(path)
  new(path).from_ruby(contents, path)
rescue TypeError, Errno::ENOENT, Errno::EISDIR
  raise Errors::ConfigNotFound, "No configuration found at: '#{path}'"
end
from_ruby(contents, path=nil) click to toggle source

@param [String] contents @param [String] path

@return [Buff::Config::Ruby]

# File lib/buff/config/ruby.rb, line 64
def from_ruby(contents, path=nil)
  new.from_ruby(contents, path)
end
new(path = nil, options = {}) click to toggle source
Calls superclass method Buff::Config::Base::new
# File lib/buff/config/ruby.rb, line 110
def initialize(path = nil, options = {})
  super
  from_ruby(File.read(path), path) if path && File.exists?(path)
end
platform_specific_path(path) click to toggle source

Converts a path to a path usable for your current platform

@param [String] path

@return [String]

# File lib/buff/config/ruby.rb, line 86
def platform_specific_path(path)
  if RUBY_PLATFORM =~ /mswin|mingw|windows/
    system_drive = ENV['SYSTEMDRIVE'] ? ENV['SYSTEMDRIVE'] : ""
    path         = win_slashify File.join(system_drive, path.split('/')[2..-1])
  end

  path
end

Private Class Methods

win_slashify(path) click to toggle source

Convert a unixy filepath to a windowsy filepath. Swaps forward slashes for double backslashes

@param [String] path

filepath to convert

@return [String]

converted filepath
# File lib/buff/config/ruby.rb, line 105
def win_slashify(path)
  path.gsub(File::SEPARATOR, (File::ALT_SEPARATOR || '\\'))
end

Public Instance Methods

from_ruby(contents, path=nil) click to toggle source

@raise [Buff::Errors::InvalidConfig]

@return [Buff::Config::Ruby]

# File lib/buff/config/ruby.rb, line 118
def from_ruby(contents, path=nil)
  hash = Buff::Config::Ruby::Evaluator.parse(contents, path, self)
  mass_assign(hash)
  self
end
reload() click to toggle source

Reload the current configuration file from disk

@return [Buff::Config::Ruby]

# File lib/buff/config/ruby.rb, line 155
def reload
  mass_assign(self.class.from_file(path).to_hash)
  self
end
save(destination = self.path) click to toggle source
# File lib/buff/config/ruby.rb, line 140
def save(destination = self.path)
  if destination.nil?
    raise Errors::ConfigSaveError, "Cannot save configuration without a destination. " +
      "Provide one to save or set one on the object."
  end

  FileUtils.mkdir_p(File.dirname(destination))
  File.open(destination, 'w+') do |f|
    f.write(to_ruby)
  end
end
to_rb()
Alias for: to_ruby
to_ruby() click to toggle source

Convert the result to Ruby.

@return [String]

# File lib/buff/config/ruby.rb, line 127
def to_ruby
  self.to_hash.map do |k,v|
    value = if const = find_constant(v)
      const
    else
      v.inspect
    end

    "#{k.to_s}(#{value})"
  end.join("\n")
end
Also aliased as: to_rb

Private Instance Methods

find_constant(name) click to toggle source
# File lib/buff/config/ruby.rb, line 162
def find_constant(name)
  Module.constants.find do |const|
    begin
      Module.const_get(const) == name
    rescue NameError; end
  end
end