Class: Malline::ViewProxy

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

Overview

Every tag returns a ViewProxy object that binds the tag to the template. ViewProxy also chains the process by returning itself.

This Proxy object then makes possible the attribute syntax: div.foo.bar! { stuff }

div returns new ViewProxy instance, so div.foo actually calls ViewProxy#foo, which is then generated to class=“foo” -attribute to the original tag div. div.foo returns the same ViewProxy, and foo.bar! calls ViewProxy#bar!, which is interpreted as a id=“bar” -attribute.

Finally the given block { stuff } is evaluated the same way than it would be evaluated without the ViewProxy:

div { stuff }

Instance Method Summary collapse

Constructor Details

#initialize(template, tag) ⇒ ViewProxy

Returns a new instance of ViewProxy.



34
35
36
37
# File 'lib/malline/view_proxy.rb', line 34

def initialize template, tag
	@tpl = template
	@tag = tag
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Capture attribute definitions, special modifiers and blocks



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

def method_missing s, *args, &block
	# div.id!
	if /^(.*)!$/ =~ s.to_s
		@tag[:attrs]['id'] = $1
	elsif s
		# div.class
		if @tag[:attrs]['class']
			@tag[:attrs]['class'] << " #{s}"
		else
			@tag[:attrs]['class'] = s.to_s
		end
	end

	__yld *args, &block
end

Instance Method Details

#__yld(*args, &block) ⇒ Object

Allows to add new content to already closed tag, for example: t = div do _’text’ end t.__yld :whitespace { stuff }

Intended for internal use only



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/malline/view_proxy.rb', line 46

def __yld *args, &block
	# div :title => 'data'
	if args.last.is_a?(Hash)
		@tag[:attrs].merge!(args.pop)
	end

	# Modifiers
	whitespace = @tpl.whitespace
	@tpl.whitespace = true if args.delete(:whitespace)

	# Rest is just content separated by a space
	txt = args.flatten.join ' '
	@tag[:children] << txt unless txt.empty?

	# Block
	@tpl.execute @tag[:children], &block if block_given?

	# Restore modifiers
	@tpl.whitespace = whitespace

	# Chain the calls, for example: div.foo.bar!.yeah.boring
	self
end