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.



25
26
27
# File 'lib/walrus/document.rb', line 25

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.


67
68
69
70
71
72
73
74
75
76
77
# File 'lib/walrus/document.rb', line 67

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.



80
81
82
83
84
# File 'lib/walrus/document.rb', line 80

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

#get_value(key) ⇒ Object



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

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.



43
44
45
46
# File 'lib/walrus/document.rb', line 43

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/walrus/document.rb', line 48

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



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

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.



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

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

#set_value(key, value) ⇒ Object



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

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.



93
94
# File 'lib/walrus/document.rb', line 93

def template_body
end