Module: ComponentDescriptors::NodeTraversal

Defined in:
lib/component_descriptors.rb

Overview

Methods needed by descriptors to travel up and down the descriptor tree.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



347
348
349
# File 'lib/component_descriptors.rb', line 347

def self.included(base)
  base.send(:attr_accessor, :parent)
end

Instance Method Details

#allObject

Array of all descriptor tree members.



413
414
415
# File 'lib/component_descriptors.rb', line 413

def all
  root.branch
end

#branchObject

Self plus all descendent descriptors.



408
409
410
# File 'lib/component_descriptors.rb', line 408

def branch
  descendents.unshift(self)
end

#clear_indexObject

Clears the index hash.



429
430
431
# File 'lib/component_descriptors.rb', line 429

def clear_index
  @descriptor_index.clear if @descriptor_index
end

#descendent(section_key) ⇒ Object

Returns the first descendent matching the given section_key or nil.



377
378
379
380
381
382
383
384
385
386
387
# File 'lib/component_descriptors.rb', line 377

def descendent(section_key)
  return nil unless respond_to?(:fetch)
  unless descendent = fetch(section_key, nil)
    values.each do |v|
      raise(NoMethodError, "Node #{v} does not respond to descendent()") unless v.respond_to?(:descendent)
      descendent = v.descendent(section_key)
      break unless descendent.nil?
    end
  end
  return descendent 
end

#descendentsObject

Array of all the descendent descriptors of the current descriptor.



390
391
392
# File 'lib/component_descriptors.rb', line 390

def descendents
  respond_to?(:values) ? values.map { |d| d.branch }.flatten : []
end

#find(index_key) ⇒ Object

Looks up a descriptor in the tree by index_key.



403
404
405
# File 'lib/component_descriptors.rb', line 403

def find(index_key)
  index[index_key]
end

#first_ancestors(method) ⇒ Object

Walks up the chain of ancestors, returning the first non-nil result for the given method, or nil if no result.



368
369
370
371
372
373
374
# File 'lib/component_descriptors.rb', line 368

def first_ancestors(method)
  return nil if root?
  if value = parent.send(method)
    return value
  end
  parent.first_ancestors(method) 
end

#indexObject

Lazily initialized index of all tree members by index_key().



418
419
420
421
422
423
424
425
426
# File 'lib/component_descriptors.rb', line 418

def index
  unless @descriptor_index
    @descriptor_index = all.inject({}) do |hash,d|
      hash[d.index_key] = d
      hash
    end
  end
  @descriptor_index
end

#parent_index_keyObject



398
399
400
# File 'lib/component_descriptors.rb', line 398

def parent_index_key
  parent.try(:index_key)
end

#parent_section_keyObject



394
395
396
# File 'lib/component_descriptors.rb', line 394

def parent_section_key
  parent.try(:section_key)
end

#rootObject

Returns the root Component



352
353
354
# File 'lib/component_descriptors.rb', line 352

def root 
  parent.nil? ? self : parent.root
end

#root?Boolean

True if this is the root component

Returns:

  • (Boolean)


357
358
359
# File 'lib/component_descriptors.rb', line 357

def root?
  parent.nil?
end

#root_elementObject

Returns the root xml element



362
363
364
# File 'lib/component_descriptors.rb', line 362

def root_element
  root.xml 
end