Class: Razor::View
- Inherits:
-
Object
- Object
- Razor::View
- Defined in:
- lib/razor/view.rb
Direct Known Subclasses
Class Attribute Summary collapse
-
.definition ⇒ Object
readonly
Returns the value of attribute definition.
-
.instances ⇒ Object
readonly
Returns the value of attribute instances.
-
.playout ⇒ Object
readonly
Returns the value of attribute playout.
Instance Attribute Summary collapse
-
#contents ⇒ Object
Returns the value of attribute contents.
Class Method Summary collapse
- .all ⇒ Object
- .class_initialize ⇒ Object
- .eval(viewfile) ⇒ Object
- .extension(ext) ⇒ Object
- .field(name, default = nil) ⇒ Object
- .fields ⇒ Object
- .get_extension ⇒ Object
- .inherited(subclass) ⇒ Object
- .layout(str = nil, &block) ⇒ Object
- .layout_render(instance) ⇒ Object
- .layouts_path=(path) ⇒ Object
- .new(*args, &block) ⇒ Object
- .render(context) ⇒ Object
- .reset_instances! ⇒ Object
- .url_base ⇒ Object
- .url_base=(url_base) ⇒ Object
Instance Method Summary collapse
- #field(name, value) ⇒ Object
-
#initialize(*args, &block) ⇒ View
constructor
A new instance of View.
- #render ⇒ Object
- #url ⇒ Object
- #url_base ⇒ Object
- #yield ⇒ Object
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
.definition ⇒ Object (readonly)
Returns the value of attribute definition.
10 11 12 |
# File 'lib/razor/view.rb', line 10 def definition @definition end |
.instances ⇒ Object (readonly)
Returns the value of attribute instances.
10 11 12 |
# File 'lib/razor/view.rb', line 10 def instances @instances end |
.playout ⇒ Object (readonly)
Returns the value of attribute playout.
10 11 12 |
# File 'lib/razor/view.rb', line 10 def playout @playout end |
Instance Attribute Details
#contents ⇒ Object
Returns the value of attribute contents.
123 124 125 |
# File 'lib/razor/view.rb', line 123 def contents @contents end |
Class Method Details
.all ⇒ Object
89 90 91 92 93 |
# File 'lib/razor/view.rb', line 89 def all @subclasses.inject(@instances) { |ary, subclass| ary + subclass.instances } end |
.class_initialize ⇒ Object
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 |
.fields ⇒ Object
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_extension ⇒ Object
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_base ⇒ Object
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 |
#render ⇒ Object
153 154 155 |
# File 'lib/razor/view.rb', line 153 def render self.class.layout_render(self) end |
#url ⇒ Object
136 137 138 |
# File 'lib/razor/view.rb', line 136 def url @viewfile.url end |
#url_base ⇒ Object
140 141 142 |
# File 'lib/razor/view.rb', line 140 def url_base self.class.url_base end |