Class: Eql::Builder

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

Overview

Builder class builds ERB templates and interact with adapters to run it.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, conn) ⇒ Builder

Returns a new instance of Builder.

Parameters:

  • path (Array<String>, String)

    path to a root folder with templates

  • conn (String)

    DB connection or a cursor



15
16
17
18
# File 'lib/eql/builder.rb', line 15

def initialize(path, conn)
  @path = Array.wrap(path)
  @conn = conn
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



101
102
103
# File 'lib/eql/builder.rb', line 101

def method_missing(name, *args, &block)
  adapter.send(name, *args, &block)
end

Instance Attribute Details

#connObject (readonly)

Parameters:

  • retruns (String)

    DB connectionor a currsor



9
10
11
# File 'lib/eql/builder.rb', line 9

def conn
  @conn
end

#pathObject (readonly)

Parameters:

  • returns (String)

    path to a root folder with templates



7
8
9
# File 'lib/eql/builder.rb', line 7

def path
  @path
end

Instance Method Details

#adapterObject

Proxy’s adapter



70
71
72
# File 'lib/eql/builder.rb', line 70

def adapter
  @adapter ||= AdapterFactory.factory(conn).new(self)
end

#cloneEql::Builder

Clone current builder

Returns:



135
136
137
# File 'lib/eql/builder.rb', line 135

def clone
  self.class.new(path, conn)
end

#execute(tmpl = nil, params = nil) ⇒ Object

Execute template query

Parameters:

  • tmpl (String, Symbol) (defaults to: nil)

    template’s name

  • params (Object, nil) (defaults to: nil)

    query params

Returns:

  • (Object)

    returns execution results



113
114
115
116
# File 'lib/eql/builder.rb', line 113

def execute(tmpl = nil, params = nil)
  load(tmpl, params)
  adapter.execute
end

#execute_params(params = nil) ⇒ Object

Execute params with a query

Parameters:

  • params (Object) (defaults to: nil)

Returns:

  • (Object)

    returns execution results



125
126
127
128
# File 'lib/eql/builder.rb', line 125

def execute_params(params = nil)
  load_params(params)
  adapter.execute
end

#load(name = nil, params = nil) ⇒ Object

Load query’s template and params

Parameters:

  • name (String) (defaults to: nil)

    template’s name

  • params (Object) (defaults to: nil)

    query’s params



26
27
28
29
# File 'lib/eql/builder.rb', line 26

def load(name = nil, params = nil)
  load_template(name) if name
  load_params(params) if params
end

#load_params(params) ⇒ Object

Load query’s params

Parameters:

  • params (Object)


52
53
54
# File 'lib/eql/builder.rb', line 52

def load_params(params)
  @params = params
end

#load_template(name) ⇒ Object

Load template with given name

Parameters:

  • name (String, Symbol)

    template’s name



36
37
38
# File 'lib/eql/builder.rb', line 36

def load_template(name)
  @template_content = loader.load_template(name)
end

#loaderEql::TemplateLoader

Returns:



43
44
45
# File 'lib/eql/builder.rb', line 43

def loader
  @loader ||= TemplateLoader.new(self)
end

#proxy_classEql::Proxy

Proxy class for template

Returns:



97
98
99
# File 'lib/eql/builder.rb', line 97

def proxy_class
  Eql::Proxy.generate(adapter)
end

#renderString

Render a template

Returns:

  • (String)

    returns rendered templated



88
89
90
# File 'lib/eql/builder.rb', line 88

def render
  ERB.new(template_content).result(proxy_class.new(self, @params).get_binding)
end

#template(raw) ⇒ Object

Set content template

Parameters:

  • raw (String)

    template’s content



61
62
63
# File 'lib/eql/builder.rb', line 61

def template(raw)
  @template_content = raw
end

#template_contentString

Template’s content

Returns:

  • (String)


79
80
81
# File 'lib/eql/builder.rb', line 79

def template_content
  @template_content.to_s
end