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
26 27 28 29 30 31 32 33 34 |
# File 'lib/walrus/template.rb', line 26 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.
20 21 22 |
# File 'lib/walrus/template.rb', line 20 def base_text @base_text end |
#origin ⇒ Object (readonly)
If initialized using a Pathname or File, returns the pathname. Otherwise returns nil.
23 24 25 |
# File 'lib/walrus/template.rb', line 23 def origin @origin end |
Instance Method Details
#class_name ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/walrus/template.rb', line 61 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).
46 47 48 49 |
# File 'lib/walrus/template.rb', line 46 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
52 53 54 |
# File 'lib/walrus/template.rb', line 52 def compiled compile end |
#fill ⇒ Object
The fill method returns a string containing the output produced when executing the compiled template.
37 38 39 |
# File 'lib/walrus/template.rb', line 37 def fill @filled ||= instance_eval(compiled) end |
#filled ⇒ Object
41 42 43 |
# File 'lib/walrus/template.rb', line 41 def filled fill end |
#run ⇒ Object
Prints output obtained by running the compiled template.
57 58 59 |
# File 'lib/walrus/template.rb', line 57 def run p fill end |
#strip_extensions(path) ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'lib/walrus/template.rb', line 73 def strip_extensions(path) extension = File.extname(path) if extension != "" # recurse strip_extensions File.basename(path, extension) else # no more extensions path end end |