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)


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

Returns the value of attribute base_text.



20
21
22
# File 'lib/walrus/template.rb', line 20

def base_text
  @base_text
end

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



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

#compileObject

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

#compiledObject

Returns the compiled text of the receiver



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

def compiled
  compile
end

#fillObject

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

#filledObject



41
42
43
# File 'lib/walrus/template.rb', line 41

def filled
  fill
end

#runObject

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