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 14
14:     def initialize(view, variables)
15:       @view = view
16:       @keys = Set.new
17: 
18:       merge(variables)
19:     end

Public Instance Methods

[](key)

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

[]=(key, value)

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

capture(*args, &block)

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

clear()

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

each()

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

locale()

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

merge(variables)

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

plugin(name, variables = {})

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

render(partial, variables = nil)

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

replace(variables)

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

to_hash()

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

Private Instance Methods

request()

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

response()

       # File lib/harbor/view_context.rb, line 113
113:     def response
114:       @response
115:     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 130
130:     def with_buffer(block)
131:       yield(_erb_buffer(block.binding))
132:     end