Class: Walrus::Template
- Inherits:
-
Object
- Object
- Walrus::Template
- Defined in:
- lib/walrus/template.rb
Instance Attribute Summary collapse
-
#base_text ⇒ Object
readonly
Returns the value of attribute base_text.
-
#origin ⇒ Object
readonly
If initialized using a Pathname or File, returns the pathname.
Instance Method Summary collapse
- #class_name ⇒ Object
-
#compile ⇒ Object
Parses template, returning compiled input (suitable for writing to disk).
-
#compiled ⇒ Object
Returns the compiled text of the receiver.
-
#fill ⇒ Object
The fill method returns a string containing the output produced when executing the compiled template.
- #filled ⇒ Object
-
#initialize(input) ⇒ Template
constructor
Accepts input of class String, Pathname or File.
-
#run ⇒ Object
Prints output obtained by running the compiled template.
- #strip_extensions(path) ⇒ Object
Constructor Details
#initialize(input) ⇒ Template
Accepts input of class String, Pathname or File
35 36 37 38 39 40 41 42 43 |
# File 'lib/walrus/template.rb', line 35 def initialize input raise ArgumentError if input.nil? if input.respond_to? :read # should work with Pathname or File @base_text = input.read @origin = input.to_s else @base_text = input.to_s.clone end end |
Instance Attribute Details
#base_text ⇒ Object (readonly)
Returns the value of attribute base_text.
28 29 30 |
# File 'lib/walrus/template.rb', line 28 def base_text @base_text end |
#origin ⇒ Object (readonly)
If initialized using a Pathname or File, returns the pathname. Otherwise returns nil.
32 33 34 |
# File 'lib/walrus/template.rb', line 32 def origin @origin end |
Instance Method Details
#class_name ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/walrus/template.rb', line 71 def class_name if @class_name @class_name else if @origin.nil? @class_name = Compiler::DEFAULT_CLASS # "DocumentSubclass" else @class_name = strip_extensions(@origin).to_class_name end end end |
#compile ⇒ Object
Parses template, returning compiled input (suitable for writing to disk).
56 57 58 59 |
# File 'lib/walrus/template.rb', line 56 def compile @parser ||= Parser.new @compiled ||= @parser.compile(@base_text, :class_name => class_name, :origin => @origin) end |
#compiled ⇒ Object
Returns the compiled text of the receiver
62 63 64 |
# File 'lib/walrus/template.rb', line 62 def compiled compile end |
#fill ⇒ Object
The fill method returns a string containing the output produced when executing the compiled template.
47 48 49 |
# File 'lib/walrus/template.rb', line 47 def fill @filled ||= instance_eval(compiled) end |
#filled ⇒ Object
51 52 53 |
# File 'lib/walrus/template.rb', line 51 def filled fill end |
#run ⇒ Object
Prints output obtained by running the compiled template.
67 68 69 |
# File 'lib/walrus/template.rb', line 67 def run p fill end |
#strip_extensions(path) ⇒ Object
83 84 85 86 87 88 89 90 |
# File 'lib/walrus/template.rb', line 83 def strip_extensions(path) extension = File.extname(path) if extension != "" # recurse strip_extensions File.basename(path, extension) else # no more extensions path end end |