Class: Eql::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/eql/proxy.rb

Overview

Proxy class proxies adapaters helpers and params to a template

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(builder, params = nil) ⇒ Proxy

Returns a new instance of Proxy.

Parameters:

  • builder (Eql::Builder)
  • params (Object) (defaults to: nil)

    template’s params to 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

Parameters:

Returns:



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

.generatedHash{Symbol => Eql::Proxy}

Cached generated proxy classes

Returns:



63
64
65
# File 'lib/eql/proxy.rb', line 63

def generated
  @generated ||= {}
end

Instance Method Details

#get_bindingBinding

Get proxy binding

Returns:

  • (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

Parameters:

  • tmpl (String)

    partial’s name

  • params (Object) (defaults to: @params)

    template’s params

Returns:

  • (String)

    returns rendered 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

Returns:

  • (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