Sha256: 2f965e02c719573a932541ac58c06cf6c588ffc57fcbaab958a0181f3df824e2

Contents?: true

Size: 955 Bytes

Versions: 9

Compression:

Stored size: 955 Bytes

Contents

# frozen_string_literal: true
# TODO: move to Praxis proper...it's not used here
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

9 entries across 9 versions & 1 rubygems

Version Path
praxis-2.0.pre.18 lib/praxis/config_hash.rb
praxis-2.0.pre.17 lib/praxis/config_hash.rb
praxis-2.0.pre.16 lib/praxis/config_hash.rb
praxis-2.0.pre.15 lib/praxis/config_hash.rb
praxis-2.0.pre.14 lib/praxis/config_hash.rb
praxis-2.0.pre.13 lib/praxis/config_hash.rb
praxis-2.0.pre.12 lib/praxis/config_hash.rb
praxis-2.0.pre.11 lib/praxis/config_hash.rb
praxis-2.0.pre.10 lib/praxis/config_hash.rb