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.



19
20
21
# File 'lib/walrus/document.rb', line 19

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.


61
62
63
64
65
66
67
68
69
70
71
# File 'lib/walrus/document.rb', line 61

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.



74
75
76
77
78
# File 'lib/walrus/document.rb', line 74

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

#get_value(key) ⇒ Object



27
28
29
# File 'lib/walrus/document.rb', line 27

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.



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

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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/walrus/document.rb', line 42

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



31
32
33
# File 'lib/walrus/document.rb', line 31

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.



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

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

#set_value(key, value) ⇒ Object



23
24
25
# File 'lib/walrus/document.rb', line 23

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.



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

def template_body
end