Class: Malline::Base

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

Overview

Template-handler class that is registered to ActionView and initialized by it.

Constant Summary collapse

@@options =

Default options for new instances, can be changed with setopt

{ :strict => true, :xhtml => true, :encoding => 'UTF-8', :lang => 'en', :form_for_proxy => true }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*opts) ⇒ Base

First parameter is the view object (if any) Last parameter is optional options hash



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/malline.rb', line 36

def initialize(*opts)
  @options = @@options.dup
  @options.merge! opts.pop if opts.last.is_a?(Hash)

  @view = opts.shift || Class.new
  unless @view.is_a?(ViewWrapper)
    @view.extend ViewWrapper
    @view.malline @options
    Malline::XHTML.load_plugin self if @options[:xhtml]
  else
    @view.malline @options
  end

  if @options[:form_for_proxy]
    begin
      ActionView::Base.default_form_builder = ::Malline::FormBuilder
    rescue NameError
    end
  end
end

Instance Attribute Details

#viewObject (readonly)

Returns the value of attribute view.



32
33
34
# File 'lib/malline.rb', line 32

def view
  @view
end

Class Method Details

.render(tpl = nil, local_assigns = {}, &block) ⇒ Object



84
85
86
# File 'lib/malline.rb', line 84

def self.render tpl = nil, local_assigns = {}, &block
  self.new.render(tpl, local_assigns, &block)
end

.setopt(hash) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/malline.rb', line 61

def self.setopt hash
  output = nil
  if block_given?
    o = @@options.dup
    @@options.merge!(hash) if hash
    begin
      output = yield
    ensure
      @@options = o
    end
  else
    @@options.merge!(hash)
  end
  output
end

Instance Method Details

#definetags(*tags) ⇒ Object

TODO: These should also be able to disable



89
90
91
92
93
94
95
96
97
# File 'lib/malline.rb', line 89

def definetags *tags
  tags.each do |tag|
    eval %{
      def @view.#{tag}(*args, &block)
        tag!('#{tag}', *args, &block)
      end
    }
  end
end

#render(tpl = nil, local_assigns = {}, n = nil, &block) ⇒ Object

n is there to keep things compatible with Markaby



78
79
80
81
82
# File 'lib/malline.rb', line 78

def render tpl = nil, local_assigns = {}, n = nil, &block
  add_local_assigns local_assigns
  @view.malline_is_active = true
  @view.malline.run tpl, &block
end

#set_path(path) ⇒ Object



57
58
59
# File 'lib/malline.rb', line 57

def set_path path
  @view.malline.path = path
end