Class: Arbre::Context
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
Instance Method Summary collapse
- #arbre_context ⇒ Object
- #assigns ⇒ Object
- #bytesize ⇒ Object (also: #length)
- #current_arbre_element ⇒ Object
- #helpers ⇒ Object
- #indent_level ⇒ Object
-
#initialize(assigns = {}, helpers = nil) { ... } ⇒ Context
constructor
Initialize a new Arbre::Context.
-
#method_missing(method, *args, &block) ⇒ Object
Webservers treat Arbre::Context as a string.
- #respond_to?(method) ⇒ Boolean
- #with_current_arbre_element(tag) ⇒ Object (also: #within)
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
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/arbre/context.rb', line 37 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 |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Webservers treat Arbre::Context as a string. We override method_missing to delegate to the string representation of the html.
77 78 79 80 81 82 83 |
# File 'lib/arbre/context.rb', line 77 def method_missing(method, *args, &block) if cached_html.respond_to? method cached_html.send method, *args, &block else super end end |
Instance Method Details
#arbre_context ⇒ Object
48 49 50 |
# File 'lib/arbre/context.rb', line 48 def arbre_context self end |
#assigns ⇒ Object
52 53 54 |
# File 'lib/arbre/context.rb', line 52 def assigns @_assigns end |
#bytesize ⇒ Object Also known as: length
65 66 67 |
# File 'lib/arbre/context.rb', line 65 def bytesize cached_html.bytesize end |
#current_arbre_element ⇒ Object
85 86 87 |
# File 'lib/arbre/context.rb', line 85 def current_arbre_element @_current_arbre_element_buffer.last end |
#helpers ⇒ Object
56 57 58 |
# File 'lib/arbre/context.rb', line 56 def helpers @_helpers end |
#indent_level ⇒ Object
60 61 62 63 |
# File 'lib/arbre/context.rb', line 60 def indent_level # A context does not increment the indent_level super - 1 end |
#respond_to?(method) ⇒ Boolean
70 71 72 |
# File 'lib/arbre/context.rb', line 70 def respond_to?(method) super || cached_html.respond_to?(method) end |
#with_current_arbre_element(tag) ⇒ Object Also known as: within
89 90 91 92 93 94 |
# File 'lib/arbre/context.rb', line 89 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 |