Sha256: d8460c12dc3d104568f4729c3c82a13c0067fb0a4e62c0ee1666c0704d09e84b

Contents?: true

Size: 884 Bytes

Versions: 1

Compression:

Stored size: 884 Bytes

Contents

require 'dslh/version'

class Dslh
  def self.eval(options = {}, &block)
    self.new(options).eval(&block)
  end

  def initialize(options = {})
    @options = options
    @hash = {}
  end

  def eval(&block)
    self.instance_eval(&block)
    return @hash
  end

  def method_missing(method_name, *args, &block)
    key_conv = @options[:key_conv] || @options[:conv]
    value_conv = @options[:value_conv] || @options[:conv]

    nested_hash = block ? self.class.eval(@options, &block) : nil
    method_name = key_conv.call(method_name) if key_conv

    if args.empty?
      @hash[method_name] = nested_hash
    else
      value = args.length > 1 ? args : args[0]
      value = value_conv.call(value) if value_conv

      if nested_hash
        @hash[method_name] = {
          value => nested_hash
        }
      else
        @hash[method_name] = value
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dslh-0.1.0 lib/dslh.rb