Class: Eql::Proxy
- Inherits:
-
Object
- Object
- Eql::Proxy
- Defined in:
- lib/eql/proxy.rb
Overview
Proxy class proxies adapaters helpers and params to a template
Class Method Summary collapse
-
.generate(adapter) ⇒ Eql::Proxy
Generate a proxy class for given adapter.
-
.generated ⇒ Hash{Symbol => Eql::Proxy}
Cached generated proxy classes.
Instance Method Summary collapse
-
#get_binding ⇒ Binding
Get proxy binding.
-
#initialize(builder, params = nil) ⇒ Proxy
constructor
A new instance of Proxy.
- #method_missing(name, *args, &block) ⇒ Object
-
#render(tmpl, params = @params) ⇒ String
Render partial template.
- #respond_to_missing?(name) ⇒ Boolean
Constructor Details
#initialize(builder, params = nil) ⇒ Proxy
Returns a new instance of Proxy.
10 11 12 13 14 15 16 17 18 |
# File 'lib/eql/proxy.rb', line 10 def initialize(builder, params = nil) @builder = builder @params = params if @params.is_a?(Hash) @params = params.keys.each_with_object({}) do |key, ret| ret[key.respond_to?(:to_sym) ? key.to_sym : key] = params[key] end end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
34 35 36 37 |
# File 'lib/eql/proxy.rb', line 34 def method_missing(name, *args, &block) return @params[name] if @params.is_a?(Hash) && @params.key?(name) @params.send(name, *args, &block) end |
Class Method Details
.generate(adapter) ⇒ Eql::Proxy
Generate a proxy class for given adapter
74 75 76 77 78 79 80 81 |
# File 'lib/eql/proxy.rb', line 74 def generate(adapter) generated[adapter.class] ||= begin helpers = AdapterFactory.adapter_helpers(adapter) Class.new(self) do helpers.each { |h| include h } end end end |
.generated ⇒ Hash{Symbol => Eql::Proxy}
Cached generated proxy classes
63 64 65 |
# File 'lib/eql/proxy.rb', line 63 def generated @generated ||= {} end |
Instance Method Details
#get_binding ⇒ Binding
Get proxy binding
50 51 52 53 54 55 |
# File 'lib/eql/proxy.rb', line 50 def get_binding # binding.name # => :binding # if we have variable with the 'name' name we need to make a closure name = send(:name) if respond_to?(:name) binding end |
#render(tmpl, params = @params) ⇒ String
Render partial template
28 29 30 31 32 |
# File 'lib/eql/proxy.rb', line 28 def render(tmpl, params = @params) b = @builder.clone b.load(tmpl, params) b.render end |
#respond_to_missing?(name) ⇒ Boolean
39 40 41 42 43 |
# File 'lib/eql/proxy.rb', line 39 def respond_to_missing?(name, *) @params.is_a?(Hash) && @params.key?(name) || @params.respond_to?(name) || super end |