Class: Buff::Config::Ruby

Inherits:
Base
  • Object
show all
Defined in:
lib/buff/config/ruby.rb

Defined Under Namespace

Classes: Evaluator

Instance Attribute Summary

Attributes inherited from Base

#path

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#to_hash

Constructor Details

#initialize(path = nil, options = {}) ⇒ Ruby

Returns a new instance of Ruby.



110
111
112
113
# 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

Class Method Details

.from_file(path) ⇒ Buff::Config::Ruby

Parameters:

  • path (String)

Returns:

Raises:



73
74
75
76
77
78
79
# 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) ⇒ Buff::Config::Ruby

Parameters:

  • contents (String)
  • path (String) (defaults to: nil)

Returns:



64
65
66
# File 'lib/buff/config/ruby.rb', line 64

def from_ruby(contents, path=nil)
  new.from_ruby(contents, path)
end

.platform_specific_path(path) ⇒ String

Converts a path to a path usable for your current platform

Parameters:

  • path (String)

Returns:

  • (String)


86
87
88
89
90
91
92
93
# 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

Instance Method Details

#from_ruby(contents, path = nil) ⇒ Buff::Config::Ruby



118
119
120
121
122
# 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

#reloadBuff::Config::Ruby

Reload the current configuration file from disk

Returns:



155
156
157
158
# 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) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
# 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_rubyString Also known as: to_rb

Convert the result to Ruby.

Returns:

  • (String)


127
128
129
130
131
132
133
134
135
136
137
# 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