Harbor Documentation

Harbor::ViewContext

Parent

Included Modules

Attributes

  • view [RW] (Not documented)
  • keys [RW] (Not documented)

Public Class Methods

new(view, variables)

      # File lib/harbor/view_context.rb, line 13
13:     def initialize(view, variables)
14:       @view = view
15:       @keys = Set.new
16: 
17:       merge(variables)
18:     end

Public Instance Methods

[](key)

      # File lib/harbor/view_context.rb, line 67
67:     def [](key)
68:       instance_variable_get("@#{key}")
69:     end

[]=(key, value)

      # File lib/harbor/view_context.rb, line 71
71:     def []=(key, value)
72:       @keys << key
73:       instance_variable_set("@#{key}", value)
74:     end

capture(*args, &block)

      # File lib/harbor/view_context.rb, line 44
44:     def capture(*args, &block)
45:       # get the buffer from the block's binding
46:       buffer = _erb_buffer( block.binding ) rescue nil
47: 
48:       # If there is no buffer, just call the block and get the contents
49:       if buffer.nil?
50:         block.call(*args)
51:       # If there is a buffer, execute the block, then extract its contents
52:       else
53:         pos = buffer.length
54: 
55:         block.call(*args)
56: 
57:         # extract the block
58:         data = buffer[pos..-1]
59: 
60:         # replace it in the original with empty string
61:         buffer[pos..-1] = ''
62: 
63:         data
64:       end
65:     end

clear()

      # File lib/harbor/view_context.rb, line 88
88:     def clear
89:       keys.each { |key| remove_instance_variable("@#{key}") }
90:       keys.clear
91: 
92:       self
93:     end

each()

      # File lib/harbor/view_context.rb, line 84
84:     def each
85:       keys.each { |key| yield(key, self[key]) }
86:     end

locale()

      # File lib/harbor/view_context.rb, line 40
40:     def locale
41:       @locale ||= Harbor::Locale.default
42:     end

merge(variables)

      # File lib/harbor/view_context.rb, line 76
76:     def merge(variables)
77:       variables.each do |key, value|
78:         self[key] = value
79:       end if variables
80: 
81:       self
82:     end

plugin(name, variables = {})

      # File lib/harbor/view_context.rb, line 30
30:     def plugin(name, variables = {})
31:       if (plugin_list = Harbor::View::plugins(name)).any?
32:         plugin_list.map do |plugin|
33:           Plugin::prepare(plugin, self, variables)
34:         end
35:       else
36:         []
37:       end
38:     end

render(partial, variables = nil)

      # File lib/harbor/view_context.rb, line 20
20:     def render(partial, variables = nil)
21:       context = to_hash
22: 
23:       result = View.new(partial, merge(variables)).to_s
24: 
25:       replace(context)
26: 
27:       result
28:     end

replace(variables)

      # File lib/harbor/view_context.rb, line 95
95:     def replace(variables)
96:       clear
97:       merge(variables)
98:     end

to_hash()

       # File lib/harbor/view_context.rb, line 100
100:     def to_hash
101:       hash = {}
102:       keys.each { |key| hash[key] = self[key] }
103:       hash
104:     end

Private Instance Methods

request()

       # File lib/harbor/view_context.rb, line 108
108:     def request
109:       @request
110:     end

response()

       # File lib/harbor/view_context.rb, line 112
112:     def response
113:       @response
114:     end

with_buffer(block)

Useful when you need to output content to the buffer.

  def wrap_with_p_tag(&block)
    with_buffer(block) do |buffer|
      buffer << "<p>" << capture(&block) << "</p>"
    end
  end
       # File lib/harbor/view_context.rb, line 129
129:     def with_buffer(block)
130:       yield(_erb_buffer(block.binding))
131:     end