Sha256: dc30f4c049d5bba82cd0b48a59263312e304512225163986e52168fa045245e0

Contents?: true

Size: 977 Bytes

Versions: 2

Compression:

Stored size: 977 Bytes

Contents

module Figleaf
  # Convert a ruby block to nested hash
  class Config
    def initialize
      @property = LazyBlockHash.new
    end

    def call(&block)
      instance_eval(&block)

      property
    end

    def method_missing(method_name, *args, &block)
      process_method(method_name, *args, &block)
    end

    def test(&block)
      process_method(:test, [], &block)
    end

    def process_method(method_name, *args, &block)
      @property[method_name.to_s] =
        if block_given?
          obj = self.class.new
          Proc.new { obj.call(&block) }
        else
          if args.count == 1
            args[0]
          else
            args
          end
        end
    end

    def respond_to_missing?(method_name, *args)
      true
    end

    private

      attr_reader :property
  end

  class LazyBlockHash < Hash
    def [](attr)
      val = super(attr)
      if val.is_a?(Proc)
        val.call
      else
        val
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
figleaf-0.2.7 lib/figleaf/config.rb
figleaf-0.2.6 lib/figleaf/config.rb