Class: Buff::Config::Ruby
Defined Under Namespace
Classes: Evaluator
Instance Attribute Summary
Attributes inherited from Base
Class Method Summary collapse
- .from_file(path) ⇒ Buff::Config::Ruby
- .from_ruby(contents) ⇒ Buff::Config::Ruby
-
.platform_specific_path(path) ⇒ String
Converts a path to a path usable for your current platform.
Instance Method Summary collapse
- #from_ruby(contents) ⇒ Buff::Config::Ruby
-
#initialize(path = nil, options = {}) ⇒ Ruby
constructor
A new instance of Ruby.
-
#reload ⇒ Buff::Config::Ruby
Reload the current configuration file from disk.
- #save(destination = self.path) ⇒ Object
-
#to_ruby ⇒ String
(also: #to_rb)
Convert the result to Ruby.
Methods inherited from Base
Constructor Details
#initialize(path = nil, options = {}) ⇒ Ruby
Returns a new instance of Ruby.
94 95 96 97 |
# File 'lib/buff/config/ruby.rb', line 94 def initialize(path = nil, = {}) super from_ruby(File.read(path)) if path && File.exists?(path) end |
Class Method Details
.from_file(path) ⇒ Buff::Config::Ruby
57 58 59 60 61 62 63 |
# File 'lib/buff/config/ruby.rb', line 57 def from_file(path) path = File.(path) contents = File.read(path) new(path).from_ruby(contents) rescue TypeError, Errno::ENOENT, Errno::EISDIR raise Errors::ConfigNotFound, "No configuration found at: '#{path}'" end |
.from_ruby(contents) ⇒ Buff::Config::Ruby
48 49 50 |
# File 'lib/buff/config/ruby.rb', line 48 def from_ruby(contents) new.from_ruby(contents) end |
.platform_specific_path(path) ⇒ String
Converts a path to a path usable for your current platform
70 71 72 73 74 75 76 77 |
# File 'lib/buff/config/ruby.rb', line 70 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) ⇒ Buff::Config::Ruby
102 103 104 105 106 |
# File 'lib/buff/config/ruby.rb', line 102 def from_ruby(contents) hash = Buff::Config::Ruby::Evaluator.parse(contents) mass_assign(hash) self end |
#reload ⇒ Buff::Config::Ruby
Reload the current configuration file from disk
139 140 141 142 |
# File 'lib/buff/config/ruby.rb', line 139 def reload mass_assign(self.class.from_file(path).to_hash) self end |
#save(destination = self.path) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/buff/config/ruby.rb', line 124 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_ruby ⇒ String Also known as: to_rb
Convert the result to Ruby.
111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/buff/config/ruby.rb', line 111 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 |