Class: Nagoro::Template
- Inherits:
-
Object
- Object
- Nagoro::Template
- Defined in:
- lib/nagoro/template.rb
Instance Attribute Summary collapse
-
#binding ⇒ Object
Returns the value of attribute binding.
-
#compiled ⇒ Object
Returns the value of attribute compiled.
-
#file ⇒ Object
Returns the value of attribute file.
-
#pipes ⇒ Object
Returns the value of attribute pipes.
Class Method Summary collapse
Instance Method Summary collapse
- #compile(io, options = {}) ⇒ Object
-
#initialize(options = {}) ⇒ Template
constructor
A new instance of Template.
- #parse_option(options = {}) ⇒ Object
-
#pipeline(io) ⇒ Object
use inject?.
- #render(io, options = {}) ⇒ Object
- #result(options = {}) ⇒ Object
- #tidy_result(options = {}) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Template
Returns a new instance of Template.
19 20 21 22 |
# File 'lib/nagoro/template.rb', line 19 def initialize( = {}) parse_option() @compiled = false end |
Instance Attribute Details
#binding ⇒ Object
Returns the value of attribute binding.
17 18 19 |
# File 'lib/nagoro/template.rb', line 17 def binding @binding end |
#compiled ⇒ Object
Returns the value of attribute compiled.
17 18 19 |
# File 'lib/nagoro/template.rb', line 17 def compiled @compiled end |
#file ⇒ Object
Returns the value of attribute file.
17 18 19 |
# File 'lib/nagoro/template.rb', line 17 def file @file end |
#pipes ⇒ Object
Returns the value of attribute pipes.
17 18 19 |
# File 'lib/nagoro/template.rb', line 17 def pipes @pipes end |
Class Method Details
.[](*pipes) ⇒ Object
13 14 15 |
# File 'lib/nagoro/template.rb', line 13 def self.[](*pipes) new(:pipes => pipes.flatten) end |
Instance Method Details
#compile(io, options = {}) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/nagoro/template.rb', line 32 def compile(io, = {}) parse_option() unless .empty? case io when String io = File.read(io) if io.size < 1024 and File.file?(io) @compiled = pipeline(io) when StringIO, IO @compiled = pipeline(io.read) else raise(ArgumentError, "Cannot compile %p" % io) end return self end |
#parse_option(options = {}) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/nagoro/template.rb', line 24 def parse_option( = {}) @binding = .fetch(:binding, BindingProvider.binding) @file = [:filename] || DEFAULT_FILE @line = [:line] || DEFAULT_LINE @pipes = [:pipes] || DEFAULT_PIPES @variables = [:variables] end |
#pipeline(io) ⇒ Object
use inject?
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/nagoro/template.rb', line 49 def pipeline(io) @pipes.each do |pipe| if pipe.respond_to?(:new) io = pipe.new(io).result elsif pipe.respond_to?(:call) io = pipe.call(io) else io = Pipe.const_get(pipe).new(io).result end end return io rescue Scanner::Stuck => exception exception. << " In: #{@file}" puts exception '' end |
#render(io, options = {}) ⇒ Object
86 87 88 |
# File 'lib/nagoro/template.rb', line 86 def render(io, = {}) compile(io, ).result end |
#result(options = {}) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/nagoro/template.rb', line 67 def result( = {}) parse_option() unless .empty? if vars = @variables obj = eval('self', @binding) vars.each do |key, value| obj.instance_variable_set("@#{key}", value) end end eval(@compiled, @binding, @file, @line).strip end |