Class: Zafu::NodeContext
- Inherits:
-
Object
- Object
- Zafu::NodeContext
- Defined in:
- lib/zafu/node_context.rb
Instance Attribute Summary collapse
-
#dom_prefix ⇒ Object
This holds the current context’s unique name if it has it’s own or one from the hierarchy.
-
#klass ⇒ Object
readonly
The type of object contained in the current context (Node, Page, Image).
-
#name ⇒ Object
readonly
The name of the variable halding the current object or list (“@node”, “var1”).
-
#opts ⇒ Object
readonly
Any kind of information that the compiler might need to use (QueryBuilder query used to fetch the node for example).
-
#saved_dom_id ⇒ Object
This is used to force a given dom_id (in saved templates for example).
-
#up(klass = nil) ⇒ Object
readonly
The previous NodeContext.
Instance Method Summary collapse
-
#as_main(after_class = nil) ⇒ Object
Return a new node context that corresponds to the current object when rendered alone (in an ajax response or from a direct ‘show’ in a controller).
-
#class_name ⇒ Object
Return the ‘real’ class name or the superclass name if the current class is an anonymous class.
-
#dom_id(opts = {}) ⇒ Object
Generate a unique DOM id for this element based on dom_scopes defined in parent contexts.
-
#form_name ⇒ Object
Return the name to use for input fields.
-
#get(klass) ⇒ Object
Returns the first occurence of the klass up in the hierachy This does not resolve [Node] as [Node].first.
-
#initialize(name, klass, up = nil, opts = {}) ⇒ NodeContext
constructor
A new instance of NodeContext.
-
#list_context? ⇒ Boolean
Return true if the current klass is an Array.
-
#master_class(after_class) ⇒ Object
Find the class just afer ‘after_class’ in the class hierarchy.
- #move_to(name, klass, opts = {}) ⇒ Object
-
#propagate_dom_scope! ⇒ Object
Mark the current context as being a looping element (each) whose DOM id needs to be propagated to sub-nodes in order to ensure uniqueness of the dom_id (loops in loops problem).
-
#raw_dom_prefix ⇒ Object
Return dom_prefix without looking up.
- #single_class ⇒ Object
-
#to_s ⇒ Object
Since the idiom to write the node context name is the main purpose of this class, it deserves this shortcut.
-
#underscore ⇒ Object
Return the name of the current class with underscores like ‘sub_page’.
-
#will_be?(type) ⇒ Boolean
Return true if the NodeContext represents an element of the given type.
Constructor Details
#initialize(name, klass, up = nil, opts = {}) ⇒ NodeContext
Returns a new instance of NodeContext.
23 24 25 |
# File 'lib/zafu/node_context.rb', line 23 def initialize(name, klass, up = nil, opts = {}) @name, @klass, @up, @opts = name, klass, up, opts end |
Instance Attribute Details
#dom_prefix ⇒ Object
This holds the current context’s unique name if it has it’s own or one from the hierarchy. If none is found, it builds one.
111 112 113 |
# File 'lib/zafu/node_context.rb', line 111 def dom_prefix @dom_prefix || (@up ? @up.dom_prefix : nil) end |
#klass ⇒ Object (readonly)
The type of object contained in the current context (Node, Page, Image)
10 11 12 |
# File 'lib/zafu/node_context.rb', line 10 def klass @klass end |
#name ⇒ Object (readonly)
The name of the variable halding the current object or list (“@node”, “var1”)
4 5 6 |
# File 'lib/zafu/node_context.rb', line 4 def name @name end |
#opts ⇒ Object (readonly)
Any kind of information that the compiler might need to use (QueryBuilder query used to fetch the node for example).
21 22 23 |
# File 'lib/zafu/node_context.rb', line 21 def opts @opts end |
#saved_dom_id ⇒ Object
This is used to force a given dom_id (in saved templates for example).
17 18 19 |
# File 'lib/zafu/node_context.rb', line 17 def saved_dom_id @saved_dom_id end |
#up(klass = nil) ⇒ Object (readonly)
The previous NodeContext
7 8 9 |
# File 'lib/zafu/node_context.rb', line 7 def up @up end |
Instance Method Details
#as_main(after_class = nil) ⇒ Object
Return a new node context that corresponds to the current object when rendered alone (in an ajax response or from a direct ‘show’ in a controller). The returned node context has no parent (up is nil). The convention is to use the class of the current object to build this name. You can also use an ‘after_class’ parameter to move up in the current object’s class hierarchy to get ivar name (see #master_class).
52 53 54 55 56 57 58 |
# File 'lib/zafu/node_context.rb', line 52 def as_main(after_class = nil) klass = after_class ? master_class(after_class) : single_class res = self.class.new("@#{klass.to_s.underscore}", single_class, nil) res.propagate_dom_scope! if @dom_scope res.dom_prefix = self.dom_prefix res end |
#class_name ⇒ Object
Return the ‘real’ class name or the superclass name if the current class is an anonymous class.
165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/zafu/node_context.rb', line 165 def class_name klass = single_class while klass.name == '' klass = klass.superclass end if list_context? "[#{klass}]" else klass.name end end |
#dom_id(opts = {}) ⇒ Object
Generate a unique DOM id for this element based on dom_scopes defined in parent contexts. :code option returns ruby :erb option returns either string content or “<%= … %>”
default returns something to insert in interpolated string such as '#{xxx}'
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/zafu/node_context.rb', line 76 def dom_id(opts = {}) dom_prefix = opts[:dom_prefix] || self.dom_prefix = {:list => true, :erb => true}.merge(opts) if [:erb] || [:code] dom = dom_id(.merge(:erb => false, :code => false)) if dom =~ /^#\{([^\{]+)\}$/ code = $1 elsif dom =~ /#\{/ code = "%Q{#{dom}}" else str = dom code = dom.inspect end if [:code] code else str || "<%= #{code} %>" end else @saved_dom_id || if [:list] scopes = dom_scopes scopes = [dom_prefix] if scopes.empty? scopes + [make_scope_id] else scopes = dom_scopes scopes + ((@up || scopes.empty?) ? [dom_prefix] : []) end.compact.uniq.join('_') end end |
#form_name ⇒ Object
Return the name to use for input fields
178 179 180 |
# File 'lib/zafu/node_context.rb', line 178 def form_name @form_name ||= master_class(ActiveRecord::Base).name.underscore end |
#get(klass) ⇒ Object
Returns the first occurence of the klass up in the hierachy This does not resolve [Node] as [Node].first.
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/zafu/node_context.rb', line 128 def get(klass) if list_context? return @up ? @up.get(klass) : nil end if self.klass <= klass self # return self unless list_context? # # res_class = self.klass # method = self.name # while res_class.kind_of?(Array) # method = "#{method}.first" # res_class = res_class.first # end # move_to(method, res_class) elsif @up @up.get(klass) else nil end end |
#list_context? ⇒ Boolean
Return true if the current klass is an Array.
155 156 157 |
# File 'lib/zafu/node_context.rb', line 155 def list_context? klass.kind_of?(Array) end |
#master_class(after_class) ⇒ Object
Find the class just afer ‘after_class’ in the class hierarchy. For example if we have Dog < Mamal < Animal < Creature, master_class(Creature) would return Animal
63 64 65 66 67 68 69 70 |
# File 'lib/zafu/node_context.rb', line 63 def master_class(after_class) klass = single_class begin up = klass.superclass return klass if up == after_class end while klass = up return self.klass end |
#move_to(name, klass, opts = {}) ⇒ Object
27 28 29 |
# File 'lib/zafu/node_context.rb', line 27 def move_to(name, klass, opts={}) self.class.new(name, klass, self, opts) end |
#propagate_dom_scope! ⇒ Object
Mark the current context as being a looping element (each) whose DOM id needs to be propagated to sub-nodes in order to ensure uniqueness of the dom_id (loops in loops problem).
122 123 124 |
# File 'lib/zafu/node_context.rb', line 122 def propagate_dom_scope! @dom_scope = true end |
#raw_dom_prefix ⇒ Object
Return dom_prefix without looking up.
116 117 118 |
# File 'lib/zafu/node_context.rb', line 116 def raw_dom_prefix @dom_prefix end |
#single_class ⇒ Object
37 38 39 |
# File 'lib/zafu/node_context.rb', line 37 def single_class @single_class ||= Array(klass).flatten.first end |
#to_s ⇒ Object
Since the idiom to write the node context name is the main purpose of this class, it deserves this shortcut.
33 34 35 |
# File 'lib/zafu/node_context.rb', line 33 def to_s name end |
#underscore ⇒ Object
Return the name of the current class with underscores like ‘sub_page’.
160 161 162 |
# File 'lib/zafu/node_context.rb', line 160 def underscore class_name.to_s.underscore end |
#will_be?(type) ⇒ Boolean
Return true if the NodeContext represents an element of the given type. We use ‘will_be’ because it is equivalent to ‘is_a’, but for future objects (during rendering).
43 44 45 |
# File 'lib/zafu/node_context.rb', line 43 def will_be?(type) single_class <= type end |