class Mousevc::View

The view rendering class of Mousevc. @note Currently only supports ERB templates and a file naming convention of: [VIEW_NAME].txt.erb

Attributes

dir[R]

@!attribute dir

@note as of v0.0.6 Mousevc requires that the views path be absolute, e.g +“#{File.dirname(__FILE__)}/views”+

@return [String] the absolute path to the views directory

Public Class Methods

new(options={}) click to toggle source

Create a new Mousevc::View instance

# File lib/mousevc/view.rb, line 23
def initialize(options={})
        @dir = options[:dir]
end

Public Instance Methods

render(view, *args) click to toggle source

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+

@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)+

@param view [String] the name of the view with .txt.erb omitted @param args [Array] accepts 2 additional parameters.

- +data+ [Hash] the data to pass to the view (optionally omit)
- +output+ [Boolean] false if output is to be suppressed

@return [String] the rendered view as a string

# File lib/mousevc/view.rb, line 43
def render(view, *args)
        data = args[0].is_a?(Hash) ? args[0] : {}
        output = ! (args[0] == false || args[1] == false)

        path = "#{@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

Private Instance Methods

to_ivars(data) click to toggle source

Take the given data hash and converts the key/value pairs into instance variables.

@param data [Hash] the data to convert into instance variables.

# File lib/mousevc/view.rb, line 67
def to_ivars(data)
        data.to_h.each do |key, value|
                instance_variable_set("@#{key.to_s}", value)
        end
end