Class: Walrus::Document

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

Overview

All compiled templates inherit from this class.

Instance Method Summary collapse

Constructor Details

#initializeDocument

Returns a new instance of Document.



29
30
31
# File 'lib/walrus/document.rb', line 29

def initialize
  @internal_hash = {}
end

Instance Method Details

#accumulate(string = nil) ⇒ Object

Supports two calling methods:

- if passed a string it will be appended to the accumulator.
- if not passed a string but given a block will evaluate the block and
  append the (string) result to the accumulator.


72
73
74
75
76
77
78
79
80
81
82
# File 'lib/walrus/document.rb', line 72

def accumulate(string = nil)
  if (@accumulators.last.nil?)  # accumulator will be nil if hasn't been used yet
    @accumulators.pop           # replace temporary nil accumulator
    @accumulators.push("")      # with proper string accumulator
  end
  if block_given?
    @accumulators.last << yield.to_s
  elsif not string.nil?
    @accumulators.last << string.to_s
  end
end

#fillObject

Fills (executes) the template body of the receiver and returns the result.



86
87
88
89
90
# File 'lib/walrus/document.rb', line 86

def fill
  @accumulators = [nil]         # reset accumulators stack
  template_body
  @accumulators.last or ""
end

#get_value(key) ⇒ Object



37
38
39
# File 'lib/walrus/document.rb', line 37

def get_value(key)
  @internal_hash[key.to_sym]
end

#lookup_and_accumulate_placeholder(placeholder, *params) ⇒ Object

Expects a placeholder symbol or String. The parameters are optional.



47
48
49
50
# File 'lib/walrus/document.rb', line 47

def lookup_and_accumulate_placeholder(placeholder, *params)
  output = lookup_and_return_placeholder(placeholder, *params)
  accumulate(output) if output
end

#lookup_and_return_placeholder(placeholder, *params) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/walrus/document.rb', line 52

def lookup_and_return_placeholder(placeholder, *params)
  # if exists a method responding to placeholder, call it
  if respond_to? placeholder
    @accumulators << nil                # push new accumulator onto the stack
    output = send(placeholder, *params) # call method
    accumulated = @accumulators.pop     # pop last accumulator from the stack
    if accumulated
      return accumulated
    elsif output
      return output
    end
  else  # otherwise, try looking it up in the values hash
    return get_value(placeholder)
  end
end

#remove_value(key) ⇒ Object



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

def remove_value(key)
  @internal_hash.delete(key.to_sym)
end

#runObject

Prints to standard out the result of filling the receiver. Note that no trailing newline is printed. As a result, if running a template from the terminal be aware that the last line may not be visible or may be partly obscured by the command prompt that is drawn (starting at the first column) after execution completes.



97
98
99
100
# File 'lib/walrus/document.rb', line 97

def run
  printf('%s', fill)
  $stdout.flush
end

#set_value(key, value) ⇒ Object



33
34
35
# File 'lib/walrus/document.rb', line 33

def set_value(key, value)
  @internal_hash[key.to_sym] = value
end

#template_bodyObject

By default, there is nothing at all in the template body.



103
104
# File 'lib/walrus/document.rb', line 103

def template_body
end