Class: Mousevc::View

Inherits:
Object
  • Object
show all
Defined in:
lib/mousevc/view.rb

Overview

Note:

Currently only supports ERB templates and a file naming convention of: [VIEW_NAME].txt.erb

The view rendering class of Mousevc.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ View

Create a new Mousevc::View instance



21
22
23
# File 'lib/mousevc/view.rb', line 21

def initialize(options={})
  @dir = options[:dir]
end

Instance Attribute Details

#dirString



16
17
18
# File 'lib/mousevc/view.rb', line 16

def dir
  @dir
end

Instance Method Details

#render(view, *args) ⇒ String

Note:

If the string passed to the view parameter is an existing file it will be used as the ERB template. Otherwise the string will be parsed as ERB.

Note:

Optionally the view output can be suppressed via setting output to false. The view will be returned as a string allowing later output e.g. render(‘view’, {:data => data}, false)

Note:

In the event that you want to suppress output and not provide any data you may substitute data for output in the parameter list e.g. render(‘view’, false)

Renders a view, passing it the given data. In the ERB template the hash variables will be available as instance variables e.g. @view_variable



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mousevc/view.rb', line 41

def render(view, *args)
  data = args[0].is_a?(Hash) ? args[0] : {}
  output = true
  output = false if args[0] == false || args[1] == false
  path = "#{Dir.pwd}/#{@dir}/#{view}.txt.erb"
  view = File.file?(path) ? File.read(path) : view
  to_ivars(data)
  result = ERB.new(view).result(binding)
  puts result if output
  result
end