Sha256: e5b8200887c4f936d478bfc01066dc9f031d1854d638042fe8273b4020ecf986

Contents?: true

Size: 1.59 KB

Versions: 3

Compression:

Stored size: 1.59 KB

Contents

require 'chozo/errors'

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

      # @see {VariaModel#from_json}
      #
      # @raise [Chozo::Errors::InvalidConfig]
      #
      # @return [Chozo::Config::JSON]
      def from_json(*args)
        super
      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).to_hash)
        self
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
chozo-0.6.1 lib/chozo/config/json.rb
chozo-0.6.0 lib/chozo/config/json.rb
chozo-0.5.0 lib/chozo/config/json.rb