Class: Babl::Utils::DslProxy
- Inherits:
-
Object
- Object
- Babl::Utils::DslProxy
- Defined in:
- lib/babl/utils/dsl_proxy.rb
Overview
The idea is to make it possible to call method defined in block’s context in addition to DSL methods. Inspired from github.com/ms-ati/docile/blob/master/lib/docile/fallback_context_proxy.rb, but here we do not try to handle instance variables, because as far as I know there is no way to do it correctly.
Constant Summary collapse
- NON_PROXIED_METHODS =
Set[ :__send__, :send, :object_id, :__id__, :equal?, :instance_eval, :instance_exec, :respond_to?, :method, :freeze ].freeze
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(receiver, fallback) ⇒ DslProxy
constructor
A new instance of DslProxy.
-
#method_missing(method, *args, &block) ⇒ Object
rubocop:disable Style/MethodMissingSuper.
-
#respond_to_missing?(method, include_private = false) ⇒ Boolean
rubocop:enable Style/MethodMissingSuper.
Constructor Details
#initialize(receiver, fallback) ⇒ DslProxy
Returns a new instance of DslProxy.
39 40 41 42 |
# File 'lib/babl/utils/dsl_proxy.rb', line 39 def initialize(receiver, fallback) @__receiver__ = receiver @__fallback__ = fallback end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
rubocop:disable Style/MethodMissingSuper
21 22 23 24 25 26 27 |
# File 'lib/babl/utils/dsl_proxy.rb', line 21 def method_missing(method, *args, &block) if @__receiver__.respond_to?(method) @__receiver__.__send__(method, *args, &block) else @__fallback__.__send__(method, *args, &block) end end |
Class Method Details
.eval(dsl, &block) ⇒ Object
35 36 37 |
# File 'lib/babl/utils/dsl_proxy.rb', line 35 def self.eval(dsl, &block) new(dsl, block.binding.receiver).instance_exec(&block) end |
Instance Method Details
#respond_to_missing?(method, include_private = false) ⇒ Boolean
rubocop:enable Style/MethodMissingSuper
30 31 32 33 |
# File 'lib/babl/utils/dsl_proxy.rb', line 30 def respond_to_missing?(method, include_private = false) @__receiver__.respond_to?(method, include_private) || @__fallback__.respond_to?(method, include_private) end |