Class: Guide::Node

Inherits:
Object
  • Object
show all
Defined in:
app/models/guide/node.rb

Direct Known Subclasses

Document, Structure

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Node

Returns a new instance of Node.



23
24
25
26
# File 'app/models/guide/node.rb', line 23

def initialize(options = {})
  @id = infer_id_from_class_name
  @options = options
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



21
22
23
# File 'app/models/guide/node.rb', line 21

def id
  @id
end

#optionsObject (readonly)

Returns the value of attribute options.



21
22
23
# File 'app/models/guide/node.rb', line 21

def options
  @options
end

Class Method Details

.child_node_class(id) ⇒ Object



12
13
14
15
16
17
18
19
# File 'app/models/guide/node.rb', line 12

def self.child_node_class(id)
  child_node_class_name = "#{self}::#{id.to_s.camelize}"
  child_node_class_name.constantize
rescue NameError => name_error
  raise Guide::Errors::InvalidNode,
    "I can't build the tree that backs Guide because I could not load the class #{child_node_class_name}.",
    name_error.backtrace
end

.contains(id, options = {}) ⇒ Object



8
9
10
# File 'app/models/guide/node.rb', line 8

def self.contains(id, options = {})
  child_nodes[id] = child_node_class(id).new(options)
end

.inherited(sub_class) ⇒ Object



4
5
6
# File 'app/models/guide/node.rb', line 4

def self.inherited(sub_class)
  sub_class.child_nodes = {}
end

Instance Method Details

#==(other) ⇒ Object



40
41
42
# File 'app/models/guide/node.rb', line 40

def ==(other)
  self.class == other.class
end

#can_be_rendered?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'app/models/guide/node.rb', line 49

def can_be_rendered?
  false
end

#child_nodesObject



36
37
38
# File 'app/models/guide/node.rb', line 36

def child_nodes
  self.class.child_nodes
end

#image_path(image_name, extension = "png") ⇒ Object



57
58
59
# File 'app/models/guide/node.rb', line 57

def image_path(image_name, extension = "png")
  Guide::Photographer.new(image_name, extension).image_path
end

#leaf_node?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'app/models/guide/node.rb', line 32

def leaf_node?
  self.child_nodes.empty?
end

#nameObject



28
29
30
# File 'app/models/guide/node.rb', line 28

def name
  @id.to_s.titleize
end

#node_typeObject



44
45
46
47
# File 'app/models/guide/node.rb', line 44

def node_type
  # for direct children, this will be :node
  self.class.superclass.name.demodulize.underscore.to_sym
end

#view_modelObject



53
54
55
# File 'app/models/guide/node.rb', line 53

def view_model
  Guide::ViewModel.new
end