Class: Walrus::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/walrus/template.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Template

Accepts input of class String, Pathname or File

Raises:

  • (ArgumentError)


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_textObject (readonly)

Returns the value of attribute base_text.



28
29
30
# File 'lib/walrus/template.rb', line 28

def base_text
  @base_text
end

#originObject (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_nameObject



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

#compileObject

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

#compiledObject

Returns the compiled text of the receiver



62
63
64
# File 'lib/walrus/template.rb', line 62

def compiled
  compile
end

#fillObject

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

#filledObject



51
52
53
# File 'lib/walrus/template.rb', line 51

def filled
  fill
end

#runObject

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