Module: Malline::ViewWrapper

Defined in:
lib/malline/view_wrapper.rb,
lib/malline/rails.rb,
lib/malline/adapters/rails-2.0.rb,
lib/malline/adapters/rails-2.1.rb

Overview

ViewWrapper is extended into used view object, like ActiveView::Base. Every method in ViewWrapper will pollute the original namespace.

Constant Summary collapse

@@malline_methods =

List of all methods that may override some custom view methods If is_malline?, then their malline -prefix versions are called

%w{_erbout capture _ tag! << txt!}

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(s, *args, &block) ⇒ Object

Define a new tag of call a helper (if _prefixed)



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/malline/view_wrapper.rb', line 70

def method_missing s, *args, &block
  return super unless is_malline?
  if @malline.tags[s]
    @malline.tag s, *args, &block
  else
    helper = ((s.to_s[0] == ?_) ? s.to_s[1..-1] : s).to_sym
    if respond_to?(helper)
      @malline.helper(helper, *args, &block)
    else
      return super if @malline.options[:strict]
      _malline_tag! s, *args, &block
    end
  end
end

Instance Method Details

#_malline__(*args) ⇒ Object Also known as: _malline_txt!

_’escaped text’



59
60
61
# File 'lib/malline/view_wrapper.rb', line 59

def _malline__ *args
  @malline.add_text(*args)
end

#_malline__erboutObject

erbout emulator



49
50
51
# File 'lib/malline/view_wrapper.rb', line 49

def _malline__erbout
  @_erbout ||= ErbOut.new(self)
end

#_malline_cache(name = {}, options = {}, &block) ⇒ Object

Rails caching



34
35
36
37
38
39
40
41
42
43
# File 'lib/malline/rails.rb', line 34

def _malline_cache name = {}, options = {}, &block
  return block.call unless @controller.perform_caching
  cache = @controller.read_fragment(name, options)

  unless cache
    cache = _malline_capture { block.call }
    @controller.write_fragment(name, cache, options)
  end
  @malline.add_unescaped_text cache
end

#_malline_capture(&block) ⇒ Object

capture and return the output of the block



54
55
56
# File 'lib/malline/view_wrapper.rb', line 54

def _malline_capture &block
  @malline.run &block
end

#_malline_ltlt(*args) ⇒ Object

self << “<unescaped text>”



65
66
67
# File 'lib/malline/view_wrapper.rb', line 65

def _malline_ltlt *args
  @malline.add_unescaped_text *args
end

#_malline_tag!(*args, &block) ⇒ Object

Define a new tag



86
87
88
# File 'lib/malline/view_wrapper.rb', line 86

def _malline_tag! *args, &block
  @malline.tag *args, &block
end

#init_malline_methodsObject

Initialize @@malline_methods



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

def init_malline_methods
  @malline_methods_inited = true
  @@malline_methods.each do |m|
    mf = m.gsub('<', 'lt')
    eval %{def #{m}(*x, &b) is_malline? ? _malline_#{mf}(*x, &b) : super; end}
  end
end

#is_malline?Boolean

Activate Malline if we are not using ActionView::Base or if the current template is a Malline template

Returns:

  • (Boolean)


91
92
93
# File 'lib/malline/view_wrapper.rb', line 91

def is_malline?
  true
end

#malline(opts = nil) ⇒ Object

Returns a current Template instance, makes a new if called first time Can also be used to set options to Template by giving them as hash opts: malline :whitespace => true



38
39
40
41
42
43
44
45
46
# File 'lib/malline/view_wrapper.rb', line 38

def malline opts = nil
  if @malline
    @malline.options.merge!(opts) if opts.is_a?(Hash)
  else
    @malline = Template.new(self, opts || {})
  end
  init_malline_methods unless @malline_methods_inited
  @malline
end