Class: BangBang::Views

Inherits:
Object
  • Object
show all
Defined in:
lib/bang-bang/views.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_instance) ⇒ Views

Returns a new instance of Views.



33
34
35
# File 'lib/bang-bang/views.rb', line 33

def initialize(app_instance)
  @app_instance = app_instance
end

Instance Attribute Details

#app_instanceObject (readonly)

Returns the value of attribute app_instance.



31
32
33
# File 'lib/bang-bang/views.rb', line 31

def app_instance
  @app_instance
end

Class Method Details

.define_method_via_include(method_name, &definition) ⇒ Object



24
25
26
27
28
# File 'lib/bang-bang/views.rb', line 24

def define_method_via_include(method_name, &definition)
  include(Module.new do
    define_method(method_name, &definition)
  end)
end

.metadata(path) ⇒ Object



17
18
19
20
21
22
# File 'lib/bang-bang/views.rb', line 17

def (path)
  {
    'data-template' => path,
    'data-type' => "Template"
  }
end

.mustache(template_path, &definition) ⇒ Object



6
7
8
9
10
11
# File 'lib/bang-bang/views.rb', line 6

def mustache(template_path, &definition)
  define_method_via_include(template_path, &definition)
  define_method_via_include(template_path) do |*args|
    Mustache.render(template_content(template_path), self.class.(template_path).merge(super(*args)))
  end
end

.text(path, &definition) ⇒ Object



13
14
15
# File 'lib/bang-bang/views.rb', line 13

def text(path, &definition)
  define_method_via_include(path, &definition)
end

Instance Method Details

#[](path) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/bang-bang/views.rb', line 45

def [](path)
  unless respond_to?(path)
    config.services.find do |service|
      presenter_path = service.get_presenter_file_path(path)
      if presenter_path
        class_eval File.read(presenter_path), presenter_path, 1
      end
    end || begin
      self.class.mustache(path) {|*_| {}}
    end
  end

  o = lambda do |*args|
    send(path, *args)
  end

  def o.render(*args)
    self.call(*args)
  end
  o
end

#appObject



37
38
39
# File 'lib/bang-bang/views.rb', line 37

def app
  app_instance.class
end

#configObject



41
42
43
# File 'lib/bang-bang/views.rb', line 41

def config
  app_instance.config
end

#template_content(relative_path) ⇒ Object



67
68
69
# File 'lib/bang-bang/views.rb', line 67

def template_content(relative_path)
  File.read(full_path(relative_path))
end