Sha256: 441a65243927d76d7deacadeafe62bed5a5aebdcaaef2bcddf1031a60b947879
Contents?: true
Size: 1.94 KB
Versions: 1
Compression:
Stored size: 1.94 KB
Contents
require 'chozo/errors' require 'multi_json' module Chozo module Config # @author Jamie Winsor <jamie@vialstudios.com> class JSON < Config::Abstract class << self # @param [String] data # # @return [~Chozo::Config::JSON] def from_json(data) new.from_json(data) end # @param [String] path # # @raise [Chozo::Errors::ConfigNotFound] # # @return [~Chozo::Config::JSON] 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 Chozo::Errors::ConfigNotFound, "No configuration found at: '#{path}'" end end # @param (see MultiJson.encode) options # # @return [String] def to_json(options = {}) MultiJson.encode(self.attributes, options) end alias_method :as_json, :to_json # @param (see MultiJson.decode) json # @param (see MultiJson.decode) options # # @raise [Chozo::Errors::InvalidConfig] # # @return [~Chozo::Config::JSON] def from_json(json, options = {}) mass_assign(MultiJson.decode(json, options)) self rescue MultiJson::DecodeError => e raise Chozo::Errors::InvalidConfig, e end 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(self.to_json(pretty: true)) end end # Reload the current configuration file from disk # # @return [Chozo::Config::JSON] def reload mass_assign(self.class.from_file(path).attributes) self end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
chozo-0.3.0 | lib/chozo/config/json.rb |