Class: Arbo::Context

Inherits:
Element show all
Defined in:
lib/arbo/context.rb

Overview

The Arbo::Context class is the frontend for using Arbo.

The simplest example possible:

html = Arbo::Context.new do
  h1 "Hello World"
end

html.render_in #=> "<h1>Hello World</h1>"

The contents of the block are instance eval’d within the Context object. This means that you lose context to the outside world from within the block. To pass local variables into the Context, use the assigns param.

html = Arbo::Context.new({one: 1}) do
  h1 "Your number #{one}"
end

html.render_in #=> "Your number 1"

Instance Attribute Summary

Attributes inherited from Element

#children, #parent

Instance Method Summary collapse

Methods inherited from Element

#+, #<<, #add_child, #ancestors, #build, #children?, #content, #content=, #each, #find_first_ancestor, #get_elements_by_class_name, #get_elements_by_tag_name, #html_safe, #parent?, #remove_child, #render_in, #render_in_or_to_s, #tag_name, #to_ary, #to_s, #to_str

Methods included from Element::BuilderMethods

#build_tag, included, #insert_tag

Constructor Details

#initialize(assigns = {}, helpers = nil) { ... } ⇒ Context

Initialize a new Arbo::Context

Parameters:

  • assigns (Hash) (defaults to: {})

    A hash of objecs that you would like to be availble as local variables within the Context

  • helpers (Object) (defaults to: nil)

    An object that has methods on it which will become instance methods within the context.

Yields:

  • The block that will get instance eval’d in the context



38
39
40
41
42
43
44
45
46
47
# File 'lib/arbo/context.rb', line 38

def initialize(assigns = {}, helpers = nil, &block)
  assigns = assigns || {}
  @_assigns = assigns.symbolize_keys

  @_helpers = helpers
  @_current_arbo_element_buffer = [self]

  super(self)
  instance_eval(&block) if block_given?
end

Instance Method Details

#arbo_contextObject



54
55
56
# File 'lib/arbo/context.rb', line 54

def arbo_context
  self
end

#assignsObject



58
59
60
# File 'lib/arbo/context.rb', line 58

def assigns
  @_assigns
end

#bytesizeObject Also known as: length



71
72
73
# File 'lib/arbo/context.rb', line 71

def bytesize
  cached_html.bytesize
end

#current_arbo_elementObject



91
92
93
# File 'lib/arbo/context.rb', line 91

def current_arbo_element
  @_current_arbo_element_buffer.last
end

#helpersObject



62
63
64
# File 'lib/arbo/context.rb', line 62

def helpers
  @_helpers
end

#indent_levelObject



66
67
68
69
# File 'lib/arbo/context.rb', line 66

def indent_level
  # A context does not increment the indent_level
  super - 1
end

#inspectObject

Override default implementation, which would call deprecated Element#to_s



50
51
52
# File 'lib/arbo/context.rb', line 50

def inspect
  "#<#{self.class}:0x#{(object_id << 1).to_s(16)}"
end

#output_bufferObject



103
104
105
# File 'lib/arbo/context.rb', line 103

def output_buffer
  @output_buffer ||= ActiveSupport::SafeBuffer.new
end

#respond_to_missing?(method, include_all) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/arbo/context.rb', line 76

def respond_to_missing?(method, include_all)
  super || cached_html.respond_to?(method, include_all)
end

#with_current_arbo_element(tag) ⇒ Object Also known as: within

Raises:

  • (ArgumentError)


95
96
97
98
99
100
# File 'lib/arbo/context.rb', line 95

def with_current_arbo_element(tag)
  raise ArgumentError, "Can't be in the context of nil. #{@_current_arbo_element_buffer.inspect}" unless tag
  @_current_arbo_element_buffer.push tag
  yield
  @_current_arbo_element_buffer.pop
end