Sha256: 8596bd5c4eb2259ca7db34dcea10c3cca4a33e936910b2a2d07c88611d4cb549

Contents?: true

Size: 1017 Bytes

Versions: 2

Compression:

Stored size: 1017 Bytes

Contents

module Berkshelf
  # @author Justin Campbell <justin@justincampbell.me>
  class Config < Hashie::Mash
    DEFAULT_PATH = "~/.berkshelf/config.json"

    include ActiveModel::Validations
    validates_with ConfigValidator

    class << self
      # @return [String, nil]
      #   the contents of the file
      def file
        File.read path if File.exists? path
      end

      # @param [#to_s] json
      #
      # @return [Config]
      def from_json(json)
        hash = JSON.parse(json).to_hash

        new.tap do |config|
          hash.each do |key, value|
            config[key] = value
          end
        end
      end

      # @return [Config]
      def instance
        @instance ||= if file
          from_json file
        else
          new
        end
      end

      # @return [String]
      def path
        File.expand_path DEFAULT_PATH
      end
    end

    # @param [String, Symbol] key
    #
    # @return [Config, Object]
    def [](key)
      super or self.class.new
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
berkshelf-0.6.0.beta4 lib/berkshelf/config.rb
berkshelf-0.6.0.beta3 lib/berkshelf/config.rb