Module: WithRefinements
- Defined in:
- lib/with_refinements.rb,
lib/with_refinements/version.rb
Constant Summary collapse
- VERSION =
"0.5.0"
Class Method Summary collapse
- .context(refinements) ⇒ Object
- .refined_proc(c, block) ⇒ Object
- .refined_proc_light(c, block) ⇒ Object
- .with_refinements(*refinements, &block) ⇒ Object
- .with_refinements_light(*refinements, args: [], &block) ⇒ Object
Class Method Details
.context(refinements) ⇒ Object
9 10 11 12 13 14 |
# File 'lib/with_refinements.rb', line 9 def context(refinements) @context_cache[refinements] ||= clean_binding.tap do |b| b.local_variable_set(:__refinements__, refinements) b.eval('__refinements__.each {|r| using r }') end end |
.refined_proc(c, block) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/with_refinements.rb', line 16 def refined_proc(c, block) @refined_proc_cache[c][block.source_location] ||= ( lvars = block.binding.local_variables c.eval(<<~RUBY) proc do |__binding__| proc { |#{lvars.join(",")}| ret = __binding__.receiver.instance_exec #{code_from_block(block)} #{lvars.map {|v| "__binding__.local_variable_set(:#{v}, #{v})" }.join("\n")} ret }.call(*__binding__.local_variables.map {|v| __binding__.local_variable_get(v) }) end RUBY ) end |
.refined_proc_light(c, block) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/with_refinements.rb', line 31 def refined_proc_light(c, block) @refined_proc_light_cache[c][block.source_location] ||= ( c.eval(<<~RUBY) proc { |__receiver__, __args__| __receiver__.instance_exec(*__args__) #{code_from_block(block)} } RUBY ) end |
.with_refinements(*refinements, &block) ⇒ Object
69 70 71 72 73 |
# File 'lib/with_refinements.rb', line 69 def with_refinements(*refinements, &block) c = WithRefinements.context(refinements) p = WithRefinements.refined_proc(c, block) p.call(block.binding) end |
.with_refinements_light(*refinements, args: [], &block) ⇒ Object
75 76 77 78 79 |
# File 'lib/with_refinements.rb', line 75 def with_refinements_light(*refinements, args: [], &block) c = WithRefinements.context(refinements) p = WithRefinements.refined_proc_light(c, block) p.call(block.binding.receiver, args) end |