Sha256: 7ffc434e8ed5483a03cc196b2466dcd4c95b8dea1ee2a192d1a7d58d5f0b827c

Contents?: true

Size: 1011 Bytes

Versions: 1

Compression:

Stored size: 1011 Bytes

Contents

# frozen_string_literal: true

module Xip
  class Configuration < Hash

    def initialize(hash)
      hash.each do |k, v|
        self[k] = store(v)
      end

      self
    end

    def method_missing(method, *args)
      key = create_config_attribute(method)

      if setter?(method)
        self[key] = args.first
      else
        self[key]
      end
    end

    def set_default(key, default_value)
      if self[key.to_s] == nil
        self[key.to_s] = store(default_value)
      end
    end

    private

      def store(value)
        if value.is_a?(Hash)
          Xip::Configuration.new(value)
        else
          value
        end
      end

      def setter?(method)
        method.slice(-1, 1) == "="
      end

      def create_config_attribute(method)
        key = basic_config_attribute_from_method(method)

        key?(key.to_s) ? key.to_s : key
      end

      def basic_config_attribute_from_method(method)
        setter?(method) ? method.to_s.chop : method
      end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
xip-2.0.0.beta2 lib/xip/configuration.rb