Module: HashBuilder
- Defined in:
- lib/hash_builder.rb,
lib/hash_builder/version.rb,
lib/hash_builder/template.rb
Defined Under Namespace
Classes: Template
Constant Summary collapse
- VERSION =
"0.2.1"
Class Method Summary collapse
Class Method Details
.build(args: [], scope: nil, locals: {}, &block) ⇒ Object
Build a Hash.
args - Arguments to be passed to the hash scope - Method calls in the block will be sent to scope object locals - Local variables to be injected into the block
Examples
HashBuilder.build(locals: {foo: 1}) do
foo foo
2
end
# => { foo: 1, bar: 2}
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/hash_builder.rb', line 20 def self.build (args: [], scope: nil, locals: {}, &block) env = ExecEnv::Env.new env.scope = scope env.locals = locals block_result = env.exec(*args, &block) = env. if .size > 0 hash = {} .each do |(name, args, block)| arg = args.first # ActiveRecord relations respond to :map but are no Enumerable if args.size == 1 && block && arg.respond_to?(:map) && !arg.is_a?(Hash) hash[name] = arg.map do |*objects| HashBuilder.build(args: objects, scope: scope, locals: locals, &block) end elsif block hash[name] = HashBuilder.build(scope: scope, locals: locals, &block) elsif args.size == 1 hash[name] = arg end end hash else block_result end end |