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.



20
21
22
# File 'lib/zafu/node_context.rb', line 20

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.



87
88
89
# File 'lib/zafu/node_context.rb', line 87

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



18
19
20
# File 'lib/zafu/node_context.rb', line 18

def opts
  @opts
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).



45
46
47
48
# File 'lib/zafu/node_context.rb', line 45

def as_main(after_class = nil)
  klass = after_class ? master_class(after_class) : Array(self.klass).first
  NodeContext.new("@#{klass.to_s.underscore}", Array(self.klass).first)
end

#class_nameObject

Return the class name or the superclass name if the current class is an anonymous class. FIXME: just use klass.to_s (so that we can do clever things with ‘to_s’)



131
132
133
134
135
136
137
138
# File 'lib/zafu/node_context.rb', line 131

def class_name
  if list_context?
    klass = @klass.first
    "[#{(klass.name.blank? ? klass.superclass : klass).name}]"
  else
    (@klass.name.blank? ? @klass.superclass : @klass).name
  end
end

#dom_id(opts = {}) ⇒ Object

Generate a unique DOM id for this element based on dom_scopes defined in parent contexts.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/zafu/node_context.rb', line 64

def dom_id(opts = {})
  options = {:list => true, :erb => true}.merge(opts)

  if options[:erb]
    dom = dom_id(options.merge(:erb => false))
    if dom =~ /^#\{([^\{]+)\}$/
      "<%= #{$1} %>"
    elsif dom =~ /#\{/
      "<%= %Q{#{dom}} %>"
    else
      dom
    end
  else
    if @up
      [dom_prefix] + @up.dom_scopes + (options[:list] ? [make_scope_id] : [])
    else
      [dom_prefix] + (options[:list] ? [make_scope_id] : [])
    end.compact.uniq.join('_')
  end
end

#form_nameObject

Return the name to use for input fields



141
142
143
# File 'lib/zafu/node_context.rb', line 141

def form_name
  @form_name ||= master_class(ActiveRecord::Base).name.underscore
end

#get(klass) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/zafu/node_context.rb', line 97

def get(klass)
  if list_context?
    if self.klass.first <= klass
      NodeContext.new("#{self.name}.first", self.klass.first)
    elsif @up
      @up.get(klass)
    else
      nil
    end
  elsif self.klass <= klass
    return self
  elsif @up
    @up.get(klass)
  else
    nil
  end
end

#list_context?Boolean

Return true if the current klass is an Array.

Returns:

  • (Boolean)


120
121
122
# File 'lib/zafu/node_context.rb', line 120

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



53
54
55
56
57
58
59
60
61
# File 'lib/zafu/node_context.rb', line 53

def master_class(after_class)
  klass = self.klass
  klass = klass.first if klass.kind_of?(Array)
  begin
    up = klass.superclass
    return klass if up == after_class
  end while klass = up
  return self.klass
end

#move_to(name, klass, opts = {}) ⇒ Object



24
25
26
# File 'lib/zafu/node_context.rb', line 24

def move_to(name, klass, opts={})
  NodeContext.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).



93
94
95
# File 'lib/zafu/node_context.rb', line 93

def propagate_dom_scope!
  @dom_scope = true
end

#to_sObject

Since the idiom to write the node context name is the main purpose of this class, it deserves this shortcut.



30
31
32
# File 'lib/zafu/node_context.rb', line 30

def to_s
  name
end

#underscoreObject

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



125
126
127
# File 'lib/zafu/node_context.rb', line 125

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)


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

def will_be?(type)
  klass.kind_of?(Array) ? klass.first.ancestors.include?(type) : klass.ancestors.include?(type)
end