Class: ErbComponent

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::TagHelper, ReqTools
Defined in:
lib/erb_component/version.rb,
lib/erb_component/erb_component.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.2.1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ReqTools

#method_missing, #params, #path, #path_hash

Constructor Details

#initialize(req, opts = {}) ⇒ ErbComponent

Returns a new instance of ErbComponent.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/erb_component/erb_component.rb', line 16

def initialize(req, opts = {})
  @req = req
  @template = opts[:template]
  begin
    @parent = self.class.superclass == ErbComponent ? nil : self.class.superclass.new(req)
    if @parent && !(parent.template['{{VIEW}}'] || parent.template['{{view}}'])
      @parent = parent.parent
    end
  rescue ArgumentError
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ReqTools

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



14
15
16
# File 'lib/erb_component/erb_component.rb', line 14

def parent
  @parent
end

#reqObject (readonly)

Returns the value of attribute req.



14
15
16
# File 'lib/erb_component/erb_component.rb', line 14

def req
  @req
end

Class Method Details

.call(req, opts = {}) ⇒ Object



42
43
44
# File 'lib/erb_component/erb_component.rb', line 42

def self.call(req, opts = {})
  new(req, **opts).render
end

.current_fileObject



54
55
56
# File 'lib/erb_component/erb_component.rb', line 54

def self.current_file
  @current_file
end

.current_file=(file) ⇒ Object



50
51
52
# File 'lib/erb_component/erb_component.rb', line 50

def self.current_file=(file)
  @current_file = file
end

Instance Method Details

#callObject



46
47
48
# File 'lib/erb_component/erb_component.rb', line 46

def call
  self.render
end

#renderObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/erb_component/erb_component.rb', line 28

def render
  str = ERB.new(template).result(binding)
  res = if parent
          parent.render.gsub("{{VIEW}}", str).gsub("{{view}}", str)
        else
          str
        end
  if res.respond_to? :html_safe
    res.html_safe
  else
    res
  end
end

#templateObject



62
63
64
65
66
# File 'lib/erb_component/erb_component.rb', line 62

def template
  return @template if @template
  return File.read(template_file_path) if template_file_path
  fail "not found: #{template_file_path}"
end

#template_file_pathObject



58
59
60
# File 'lib/erb_component/erb_component.rb', line 58

def template_file_path
  self.class.current_file.gsub('.rb', '.erb')
end