class Buff::Config::JSON

Public Class Methods

from_file(path) click to toggle source

@param [String] path

@raise [Buff::Errors::ConfigNotFound]

@return [Buff::Config::JSON]

# File lib/buff/config/json.rb, line 27
def from_file(path)
  path = File.expand_path(path)
  data = File.read(path)
  new(path).from_json(data)
rescue TypeError, Errno::ENOENT, Errno::EISDIR
  raise Errors::ConfigNotFound, "No configuration found at: '#{path}'"
end
from_hash(hash) click to toggle source

@param [Hash] hash

@return [Buff::Config::JSON]

# File lib/buff/config/json.rb, line 18
def from_hash(hash)
  new.from_hash(hash)
end
from_json(data) click to toggle source

@param [String] data

@return [Buff::Config::JSON]

# File lib/buff/config/json.rb, line 11
def from_json(data)
  new.from_json(data)
end

Public Instance Methods

from_json(*args) click to toggle source

@see {VariaModel#from_json}

@raise [Buff::Errors::InvalidConfig]

@return [Buff::Config::JSON]

Calls superclass method
# File lib/buff/config/json.rb, line 41
def from_json(*args)
  super
rescue ::JSON::ParserError => ex
  raise Errors::InvalidConfig, ex
end
reload() click to toggle source

Reload the current configuration file from disk

@return [Buff::Config::JSON]

# File lib/buff/config/json.rb, line 61
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/json.rb, line 47
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(::JSON.pretty_generate(self.to_hash))
  end
end