class Arbre::Context

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

The simplest example possible:

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

html.to_s #=> "<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 = Arbre::Context.new({one: 1}) do
  h1 "Your number #{one}"
end

html.to_s #=> "Your number 1"

Public Class Methods

new(assigns = {}, helpers = nil, &block) click to toggle source

Initialize a new Arbre::Context

@param [Hash] assigns A hash of objecs that you would like to be

availble as local variables within the Context

@param [Object] helpers An object that has methods on it which will become

instance methods within the context.

@yield [] The block that will get instance eval'd in the context

Calls superclass method
# File lib/arbre/context.rb, line 38
def initialize(assigns = {}, helpers = nil, &block)
  assigns = assigns || {}
  @_assigns = assigns.symbolize_keys

  @_helpers = helpers
  @_current_arbre_element_buffer = [self]

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

Public Instance Methods

arbre_context() click to toggle source
# File lib/arbre/context.rb, line 49
def arbre_context
  self
end
assigns() click to toggle source
# File lib/arbre/context.rb, line 53
def assigns
  @_assigns
end
bytesize() click to toggle source
# File lib/arbre/context.rb, line 66
def bytesize
  cached_html.bytesize
end
Also aliased as: length
current_arbre_element() click to toggle source
# File lib/arbre/context.rb, line 86
def current_arbre_element
  @_current_arbre_element_buffer.last
end
helpers() click to toggle source
# File lib/arbre/context.rb, line 57
def helpers
  @_helpers
end
indent_level() click to toggle source
Calls superclass method
# File lib/arbre/context.rb, line 61
def indent_level
  # A context does not increment the indent_level
  super - 1
end
length()
Alias for: bytesize
method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/arbre/context.rb, line 78
               def method_missing(method, *args, &block)
  if cached_html.respond_to? method
    cached_html.send method, *args, &block
  else
    super
  end
end
respond_to_missing?(method, include_all) click to toggle source
Calls superclass method
# File lib/arbre/context.rb, line 71
def respond_to_missing?(method, include_all)
  super || cached_html.respond_to?(method, include_all)
end
with_current_arbre_element(tag) { || ... } click to toggle source
# File lib/arbre/context.rb, line 90
def with_current_arbre_element(tag)
  raise ArgumentError, "Can't be in the context of nil. #{@_current_arbre_element_buffer.inspect}" unless tag
  @_current_arbre_element_buffer.push tag
  yield
  @_current_arbre_element_buffer.pop
end
Also aliased as: within
within(tag)

Private Instance Methods

cached_html() click to toggle source

Caches the rendered HTML so that we don't re-render just to get the content lenght or to delegate a method to the HTML

# File lib/arbre/context.rb, line 103
def cached_html
  if defined?(@cached_html)
    @cached_html
  else
    html = to_s
    @cached_html = html if html.length > 0
    html
  end
end