Class: Viewlet::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/viewlet/base.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, view) ⇒ Base

Returns a new instance of Base.



5
6
7
8
9
10
11
12
# File 'lib/viewlet/base.rb', line 5

def initialize(name, view)
  @name = name
  @view = view
  @variables = {
    @name.to_sym => self,
    :unique_id => "viewlet_#{rand(36**20).to_s(36)}"
  }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(variable_name, *args, &block) ⇒ Object (private)



20
21
22
23
24
25
26
27
28
# File 'lib/viewlet/base.rb', line 20

def method_missing(variable_name, *args, &block)
  is_write_op = if @variables[variable_name].is_a?(Proc)
    block.present?
  else
    args.any? || block.present?
  end

  send("_#{is_write_op ? :write : :read}_variable", variable_name, *args, &block)
end

Instance Method Details

#renderObject



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

def render
  Template.find(@name.to_s).render(@view, @variables)
end