Sha256: e36f9fb2a3da6de7b41c1adcf4b249cdee27fa8a040772cc84a85a713859f4a1

Contents?: true

Size: 904 Bytes

Versions: 3

Compression:

Stored size: 904 Bytes

Contents

# frozen_string_literal: true
module Praxis
  class ConfigHash < BasicObject
    attr_reader :hash

    def self.from(hash = {}, &block)
      new(hash, &block)
    end

    def initialize(hash = {}, &block)
      @hash = hash
      @block = block
    end

    def to_hash
      instance_eval(&@block)
      @hash
    end

    def respond_to_missing?(_method_name, _include_private = false)
      true
    end

    def method_missing(name, value, *rest, &block) # rubocop:disable Style/MethodMissing
      if (existing = @hash[name])
        if block
          existing << [value, block]
        else
          existing << value
          rest.each do |v|
            existing << v
          end
        end
      else
        @hash[name] = if rest.any?
                        [value] + rest
                      else
                        value
                      end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
praxis-blueprints-3.5 lib/praxis-blueprints/config_hash.rb
praxis-blueprints-3.4 lib/praxis-blueprints/config_hash.rb
praxis-blueprints-3.3 lib/praxis-blueprints/config_hash.rb