Class: Zafu::NodeContext

Inherits:
Object
  • Object
show all
Defined in:
lib/zafu/node_context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_prefixObject

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.



110
111
112
# File 'lib/zafu/node_context.rb', line 110

def dom_prefix
  @dom_prefix || (@up ? @up.dom_prefix : nil)
end

#klassObject (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

#nameObject (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

#optsObject (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_idObject

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
# 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.dom_prefix = self.dom_prefix
  res
end

#class_nameObject

Return the ‘real’ class name or the superclass name if the current class is an anonymous class.



159
160
161
162
163
164
165
166
167
168
169
# File 'lib/zafu/node_context.rb', line 159

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}'


75
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
# File 'lib/zafu/node_context.rb', line 75

def dom_id(opts = {})
  dom_prefix = opts[:dom_prefix] || self.dom_prefix
  options = {:list => true, :erb => true}.merge(opts)

  if options[:erb] || options[:code]
    dom = dom_id(options.merge(:erb => false, :code => false))

    if dom =~ /^#\{([^\{]+)\}$/
      code = $1
    elsif dom =~ /#\{/
      code = "%Q{#{dom}}"
    else
      str  = dom
      code = dom.inspect
    end

    if options[:code]
      code
    else
      str || "<%= #{code} %>"
    end
  else
    @saved_dom_id || if options[: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_nameObject

Return the name to use for input fields



172
173
174
# File 'lib/zafu/node_context.rb', line 172

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.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/zafu/node_context.rb', line 122

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.

Returns:

  • (Boolean)


149
150
151
# File 'lib/zafu/node_context.rb', line 149

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



62
63
64
65
66
67
68
69
# File 'lib/zafu/node_context.rb', line 62

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).



116
117
118
# File 'lib/zafu/node_context.rb', line 116

def propagate_dom_scope!
  @dom_scope = true
end

#single_classObject



37
38
39
# File 'lib/zafu/node_context.rb', line 37

def single_class
  @single_class ||= Array(klass).flatten.first
end

#to_sObject

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

#underscoreObject

Return the name of the current class with underscores like ‘sub_page’.



154
155
156
# File 'lib/zafu/node_context.rb', line 154

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).

Returns:

  • (Boolean)


43
44
45
# File 'lib/zafu/node_context.rb', line 43

def will_be?(type)
  single_class <= type
end