Class: Harbor::ViewContext
Defined Under Namespace
Modules: Helpers
Instance Attribute Summary collapse
Instance Method Summary
collapse
#cache
#merge_query_string
#split_into_columns, #split_into_groups
#h, #q, #truncate, #truncate_on_words
#form
Constructor Details
#initialize(view, variables) ⇒ ViewContext
Returns a new instance of ViewContext.
13
14
15
16
17
18
|
# File 'lib/harbor/view_context.rb', line 13
def initialize(view, variables)
@view = view
@keys = Set.new
merge(variables)
end
|
Instance Attribute Details
#keys ⇒ Object
Returns the value of attribute keys.
11
12
13
|
# File 'lib/harbor/view_context.rb', line 11
def keys
@keys
end
|
#view ⇒ Object
Returns the value of attribute view.
11
12
13
|
# File 'lib/harbor/view_context.rb', line 11
def view
@view
end
|
Instance Method Details
#[](key) ⇒ Object
67
68
69
|
# File 'lib/harbor/view_context.rb', line 67
def [](key)
instance_variable_get("@#{key}")
end
|
#[]=(key, value) ⇒ Object
71
72
73
74
|
# File 'lib/harbor/view_context.rb', line 71
def []=(key, value)
@keys << key
instance_variable_set("@#{key}", value)
end
|
#capture(*args, &block) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/harbor/view_context.rb', line 44
def capture(*args, &block)
buffer = _erb_buffer( block.binding ) rescue nil
if buffer.nil?
block.call(*args)
else
pos = buffer.length
block.call(*args)
data = buffer[pos..-1]
buffer[pos..-1] = ''
data
end
end
|
#clear ⇒ Object
88
89
90
91
92
93
|
# File 'lib/harbor/view_context.rb', line 88
def clear
keys.each { |key| remove_instance_variable("@#{key}") }
keys.clear
self
end
|
#each ⇒ Object
84
85
86
|
# File 'lib/harbor/view_context.rb', line 84
def each
keys.each { |key| yield(key, self[key]) }
end
|
#locale ⇒ Object
40
41
42
|
# File 'lib/harbor/view_context.rb', line 40
def locale
@locale ||= Harbor::Locale.default
end
|
#merge(variables) ⇒ Object
76
77
78
79
80
81
82
|
# File 'lib/harbor/view_context.rb', line 76
def merge(variables)
variables.each do |key, value|
self[key] = value
end if variables
self
end
|
#plugin(name, variables = {}) ⇒ Object
30
31
32
33
34
35
36
37
38
|
# File 'lib/harbor/view_context.rb', line 30
def plugin(name, variables = {})
if (plugin_list = Harbor::View::plugins(name)).any?
plugin_list.map do |plugin|
Plugin::prepare(plugin, self, variables)
end
else
[]
end
end
|
#render(partial, variables = nil) ⇒ Object
20
21
22
23
24
25
26
27
28
|
# File 'lib/harbor/view_context.rb', line 20
def render(partial, variables = nil)
context = to_hash
result = View.new(partial, merge(variables)).to_s
replace(context)
result
end
|
#replace(variables) ⇒ Object
95
96
97
98
|
# File 'lib/harbor/view_context.rb', line 95
def replace(variables)
clear
merge(variables)
end
|
#to_hash ⇒ Object
100
101
102
103
104
|
# File 'lib/harbor/view_context.rb', line 100
def to_hash
hash = {}
keys.each { |key| hash[key] = self[key] }
hash
end
|