Sha256: 05b54f943084c016918930d40e349f0440151e530c1fe150e322be2a5f2150cc

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

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
    rescue Exception => e
      raise Settings::InvalidRb, "Configuration has invalid Ruby\n" + e.message
    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
    rescue => e
      raise Settings::InvalidRb, "Configuration has invalid Ruby\n" + e.message
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
figleaf-0.2.9 lib/figleaf/config.rb