Class: Harbor::ViewContext

Inherits:
Object
  • Object
show all
Includes:
Helpers::Cache, Helpers::Form, Helpers::Html, Helpers::Text, Helpers::Url
Defined in:
lib/harbor/view_context.rb

Direct Known Subclasses

XMLViewContext

Defined Under Namespace

Modules: Helpers

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Cache

#cache

Methods included from Helpers::Url

#merge_query_string

Methods included from Helpers::Html

#split_into_columns, #split_into_groups

Methods included from Helpers::Text

#h, #q, #truncate, #truncate_on_words

Methods included from Helpers::Form

#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

#keysObject

Returns the value of attribute keys.



11
12
13
# File 'lib/harbor/view_context.rb', line 11

def keys
  @keys
end

#viewObject

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)
  # get the buffer from the block's binding
  buffer = _erb_buffer( block.binding ) rescue nil

  # If there is no buffer, just call the block and get the contents
  if buffer.nil?
    block.call(*args)
  # If there is a buffer, execute the block, then extract its contents
  else
    pos = buffer.length

    block.call(*args)

    # extract the block
    data = buffer[pos..-1]

    # replace it in the original with empty string
    buffer[pos..-1] = ''

    data
  end
end

#clearObject



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

#eachObject



84
85
86
# File 'lib/harbor/view_context.rb', line 84

def each
  keys.each { |key| yield(key, self[key]) }
end

#localeObject



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_hashObject



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