Class: Bruter::Mustache

Inherits:
Mustache
  • Object
show all
Defined in:
lib/bruter/mustache.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request, response, router) ⇒ Mustache

Sets the Rack request and response objects and the calling router object. Subclasses can define a :setup method that will be invoked after initialization is complete.



59
60
61
62
63
64
# File 'lib/bruter/mustache.rb', line 59

def initialize(request, response, router)
  @request  = request
  @response = response
  @router   = router
  send :setup if respond_to?(:setup)
end

Class Attribute Details

._directoryObject

Returns the value of attribute _directory.



5
6
7
# File 'lib/bruter/mustache.rb', line 5

def _directory
  @_directory
end

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



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

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



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

def response
  @response
end

#routerObject (readonly)

Returns the value of attribute router.



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

def router
  @router
end

Class Method Details

.inherited(klass) ⇒ Object

Hack to set :_directory to the file location in which this class is subclassed. Those not subclassing Bruter::Mustache in a file may want to explicitly set :_directory or the @template_path variable with :template_path=(path).



11
12
13
# File 'lib/bruter/mustache.rb', line 11

def inherited(klass)
  klass._directory = File.dirname(caller.first[/^[^:]+/])
end

.template_nameObject

Sets the default template name to an underscored version of the class name.

Example:

# in '/path/to/app/views/tagged.rb'
module Views
  class TaggedPosts < Bruter::Mustache
    puts self.template_name
    puts self.template_file
  end
end

=> 'tagged_posts'
=> '/path/to/app/templates/tagged_posts.mustache'


49
50
51
# File 'lib/bruter/mustache.rb', line 49

def template_name
  @template_name ||= self.to_s.underscore.split('/').last
end

.template_pathObject

Sets the default template path to ‘../templates’ relative to where the this class is subclassed.

Example:

# in '/path/to/app/views/tagged.rb'
module Views
  class TaggedPosts < Bruter::Mustache
    puts self.template_path
  end
end

=> '/path/to/app/templates'


27
28
29
30
31
32
33
# File 'lib/bruter/mustache.rb', line 27

def template_path
  if _directory
    @template_path ||= File.expand_path('../templates', _directory)
  else
    super
  end
end

Instance Method Details

#envObject

Retrieves the Rack request’s env.



67
68
69
# File 'lib/bruter/mustache.rb', line 67

def env
  @request.env
end

#paramsObject

Retrieves the params from a name variable or globbed route.



72
73
74
# File 'lib/bruter/mustache.rb', line 72

def params
  env['router.params']
end

#path(name, *args) ⇒ Object

Retrieves the path for a named route given the name and any arguments.



77
78
79
# File 'lib/bruter/mustache.rb', line 77

def path(name, *args)
  args.any? ? router.url(name, *args) : router.url(name)
end