Class: Arbre::Context

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

Overview

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"

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, #inspect, #parent?, #remove_child, #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 Arbre::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/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

Instance Method Details

#arbre_contextObject


49
50
51
# File 'lib/arbre/context.rb', line 49

def arbre_context
  self
end

#assignsObject


53
54
55
# File 'lib/arbre/context.rb', line 53

def assigns
  @_assigns
end

#bytesizeObject Also known as: length


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

def bytesize
  cached_html.bytesize
end

#current_arbre_elementObject


86
87
88
# File 'lib/arbre/context.rb', line 86

def current_arbre_element
  @_current_arbre_element_buffer.last
end

#helpersObject


57
58
59
# File 'lib/arbre/context.rb', line 57

def helpers
  @_helpers
end

#indent_levelObject


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

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

#respond_to_missing?(method, include_all) ⇒ Boolean

Returns:

  • (Boolean)

71
72
73
# 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) ⇒ Object Also known as: within

Raises:

  • (ArgumentError)

90
91
92
93
94
95
# 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