Sha256: 612b139c950efb6912cb5aecaa175a9c92e96da5dc4d2885f2cb0180054f92a1
Contents?: true
Size: 1.78 KB
Versions: 2
Compression:
Stored size: 1.78 KB
Contents
require 'dslh/version' class Dslh def self.eval(expr_or_options = nil, options = nil, &block) if options and not options.kind_of?(Hash) raise TypeError, "wrong argument type #{options.class} (expected Hash)" end expr = nil options ||= {} if expr_or_options case expr_or_options when String expr = expr_or_options when Hash options.update(expr_or_options) else raise TypeError, "wrong argument type #{expr_or_options.class} (expected String or Hash)" end end self.new(options).eval(expr, &block) end def initialize(options = {}) @options = options.dup end def eval(expr = nil, &block) retval = {} scope = Scope.new scope.instance_variable_set(:@__options__, @options) scope.instance_variable_set(:@__hash__, retval) if expr eval_args = [expr] [:filename, :lineno].each do |k| eval_args << @options[k] if @options[k] end scope.instance_eval(*eval_args) else scope.instance_eval(&block) end return retval end class Scope def method_missing(method_name, *args, &block) key_conv = @__options__[:key_conv] || @__options__[:conv] value_conv = @__options__[:value_conv] || @__options__[:conv] nested_hash = block ? Dslh.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 return @__hash__ end end # of Scope end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
dslh-0.1.2 | lib/dslh.rb |
dslh-0.1.1 | lib/dslh.rb |