Class: Razor::View

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

Direct Known Subclasses

Page, Script, StyleSheet

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ View

Returns a new instance of View.



125
126
127
128
129
130
131
132
133
134
# File 'lib/razor/view.rb', line 125

def initialize(*args, &block)
	@viewfile = @@last_viewfile
	@contents = lambda { '' }
	self.class.fields.each { |iv|
		instance_variable_set(*iv)
	}
	if block_given?
		self.class.definition.new(self, &block)
	end
end

Class Attribute Details

.definitionObject (readonly)

Returns the value of attribute definition.



10
11
12
# File 'lib/razor/view.rb', line 10

def definition
  @definition
end

.instancesObject (readonly)

Returns the value of attribute instances.



10
11
12
# File 'lib/razor/view.rb', line 10

def instances
  @instances
end

.playoutObject (readonly)

Returns the value of attribute playout.



10
11
12
# File 'lib/razor/view.rb', line 10

def playout
  @playout
end

Instance Attribute Details

#contentsObject

Returns the value of attribute contents.



123
124
125
# File 'lib/razor/view.rb', line 123

def contents
  @contents
end

Class Method Details

.allObject



89
90
91
92
93
# File 'lib/razor/view.rb', line 89

def all
	@subclasses.inject(@instances) { |ary, subclass|
		ary + subclass.instances
	}
end

.class_initializeObject



17
18
19
20
21
22
23
# File 'lib/razor/view.rb', line 17

def class_initialize
	@subclasses = []
	@definition = Class.new(superclass.definition)
	@playout = superclass.playout
	@fields = {}
	@instances = []
end

.eval(viewfile) ⇒ Object



30
31
32
33
34
35
# File 'lib/razor/view.rb', line 30

def eval(viewfile)
	@@last_evaluated = nil
	@@last_viewfile = viewfile
	Kernel.eval(File.read(viewfile.src), TOPLEVEL_BINDING)
	return @@last_evaluated
end

.extension(ext) ⇒ Object



37
38
39
# File 'lib/razor/view.rb', line 37

def extension(ext)
	@extension = ext
end

.field(name, default = nil) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/razor/view.rb', line 65

def field(name, default=nil)
	if name =~ /^(field|contents)$/
		raise "Try to create a #{name.inspect} field"
	end
	attr_accessor name
	@fields["@#{name}"] = default
	@definition.field(name)
end

.fieldsObject



74
75
76
77
78
79
80
# File 'lib/razor/view.rb', line 74

def fields
	if superclass.respond_to? :fields
		superclass.fields.merge(@fields)
	else
		@fields
	end
end

.get_extensionObject



53
54
55
56
57
58
59
# File 'lib/razor/view.rb', line 53

def get_extension
	if superclass.respond_to? :get_extension
		@extension or superclass.get_extension
	else
		@extension
	end
end

.inherited(subclass) ⇒ Object



12
13
14
15
# File 'lib/razor/view.rb', line 12

def inherited(subclass)
	@subclasses << subclass
	subclass.class_initialize
end

.layout(str = nil, &block) ⇒ Object



61
62
63
# File 'lib/razor/view.rb', line 61

def layout(str=nil, &block)
	@playout = str ? lambda { str } : block
end

.layout_render(instance) ⇒ Object



95
96
97
# File 'lib/razor/view.rb', line 95

def layout_render(instance)
	render(Mustache::Context.new(instance))
end

.layouts_path=(path) ⇒ Object



41
42
43
# File 'lib/razor/view.rb', line 41

def layouts_path=(path)
	@@layouts_path = path
end

.new(*args, &block) ⇒ Object



82
83
84
85
86
87
# File 'lib/razor/view.rb', line 82

def new(*args, &block)
	instance = super(*args, &block)
	@instances << instance
	@@last_evaluated = instance
	return instance
end

.render(context) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/razor/view.rb', line 99

def render(context)
	renderer = Renderer.get(@@layouts_path, @playout)
	if superclass.respond_to? :render
		if renderer.valid?
			context.push('yield' => renderer.layout_render(context))
		end
		superclass.render(context)
	else
		if renderer.valid?
			renderer.layout_render(context)
		else
			context['yield']
		end
	end
end

.reset_instances!Object



25
26
27
28
# File 'lib/razor/view.rb', line 25

def reset_instances!
	@instances = []
	@subclasses.each(&:reset_instances!)
end

.url_baseObject



45
46
47
# File 'lib/razor/view.rb', line 45

def url_base
	@@url_base
end

.url_base=(url_base) ⇒ Object



49
50
51
# File 'lib/razor/view.rb', line 49

def url_base=(url_base)
	@@url_base = url_base
end

Instance Method Details

#field(name, value) ⇒ Object



144
145
146
147
# File 'lib/razor/view.rb', line 144

def field(name, value)
	(class<<self; self; end).class_eval { attr name }
	instance_variable_set("@#{name}", value)
end

#renderObject



153
154
155
# File 'lib/razor/view.rb', line 153

def render
	self.class.layout_render(self)
end

#urlObject



136
137
138
# File 'lib/razor/view.rb', line 136

def url
	@viewfile.url
end

#url_baseObject



140
141
142
# File 'lib/razor/view.rb', line 140

def url_base
	self.class.url_base
end

#yieldObject



149
150
151
# File 'lib/razor/view.rb', line 149

def yield
	Renderer.get(@viewfile.parent.src, @contents).render(self)
end