Class: Flex::Template

Inherits:
Object
  • Object
show all
Includes:
Common, Logger
Defined in:
lib/flex/template.rb,
lib/flex/template/tags.rb,
lib/flex/template/common.rb,
lib/flex/template/logger.rb,
lib/flex/template/search.rb,
lib/flex/template/partial.rb,
lib/flex/template/slim_search.rb

Overview

Generic Flex::Template object. This class represents a generic Flex::Template object. It is used as the base class by all the more specific Flex::Template::* classes. You usually don’t need to instantiate this class manually, since it is usually used internally. For more details about templates, see the documentation.

Direct Known Subclasses

Search

Defined Under Namespace

Modules: Common, Logger Classes: Partial, Search, SlimSearch, Tag, Tags

Instance Attribute Summary collapse

Attributes included from Common

#data, #name, #partials, #tags

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Common

#interpolate_partials, #setup

Methods included from Logger

#caller_line

Constructor Details

#initialize(method, path, data = nil, *vars) ⇒ Template

Returns a new instance of Template.

Raises:



19
20
21
22
23
24
25
26
# File 'lib/flex/template.rb', line 19

def initialize(method, path, data=nil, *vars)
  @method = method.to_s.upcase
  raise ArgumentError, "#@method method not supported" \
        unless %w[HEAD GET PUT POST DELETE].include?(@method)
  @path          = path =~ /^\// ? path : "/#{path}"
  @data          = Utils.parse_source(data)
  @instance_vars = Vars.new(*vars)
end

Instance Attribute Details

#methodObject (readonly)

Returns the value of attribute method.



17
18
19
# File 'lib/flex/template.rb', line 17

def method
  @method
end

#pathObject (readonly)

Returns the value of attribute path.



17
18
19
# File 'lib/flex/template.rb', line 17

def path
  @path
end

Class Method Details

.variablesObject



13
14
15
# File 'lib/flex/template.rb', line 13

def self.variables
  Vars.new
end

Instance Method Details

#render(*vars) ⇒ Object



28
29
30
31
32
# File 'lib/flex/template.rb', line 28

def render(*vars)
  do_render(*vars) do |response, int|
    Result.new(self, int[:vars], response).to_flex_result
  end
end

#to_a(*vars) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/flex/template.rb', line 34

def to_a(*vars)
  vars = Vars.new(*vars)
  int  = interpolate(vars)
  a    = [method, int[:path], Utils.keyfy(:to_s, int[:data]), Utils.keyfy(:to_s, @instance_vars)]
  2.times { a.pop if a.last.nil? || a.last.empty? }
  a
end

#to_sourceObject



42
43
44
# File 'lib/flex/template.rb', line 42

def to_source
  {@name.to_s => to_a}.to_yaml
end