Class: Crawfish::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/crawfish/node.rb

Overview

A node within the graph of models from a root or entity node.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, opts = {}) ⇒ Node

Returns a new instance of Node.



9
10
11
12
13
14
15
# File 'lib/crawfish/node.rb', line 9

def initialize(model, opts={})
  @model = model
  @ref_key = opts[:ref_key]
  @parent = opts[:parent]
  @reflection = opts[:reflection]
  @node_decorator = opts[:node_decorator]
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



7
8
9
# File 'lib/crawfish/node.rb', line 7

def model
  @model
end

#node_decoratorObject

Returns the value of attribute node_decorator.



8
9
10
# File 'lib/crawfish/node.rb', line 8

def node_decorator
  @node_decorator
end

#parentObject

Returns the value of attribute parent.



8
9
10
# File 'lib/crawfish/node.rb', line 8

def parent
  @parent
end

#ref_keyObject (readonly)

Returns the value of attribute ref_key.



7
8
9
# File 'lib/crawfish/node.rb', line 7

def ref_key
  @ref_key
end

#reflectionObject (readonly)

Returns the value of attribute reflection.



7
8
9
# File 'lib/crawfish/node.rb', line 7

def reflection
  @reflection
end

Instance Method Details

#aside?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/crawfish/node.rb', line 106

def aside?
  has_one? || has_and_belongs_to_many?
end

#associated_nodesObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/crawfish/node.rb', line 36

def associated_nodes
  @associated_nodes ||= model.reflections.map do |key, reflection|
    node = Node.new reflection.klass, ref_key: key,
                                      parent: self,
                                      reflection: reflection,
                                      node_decorator: node_decorator
    if node_decorator
      node = node_decorator.decorate(node)
    end
    node
  end
end

#belongs_to?Boolean Also known as: above?

Returns:

  • (Boolean)


87
88
89
# File 'lib/crawfish/node.rb', line 87

def belongs_to?
  reflection && reflection.macro == :belongs_to
end

#flatten(filter = lambda{|n| true}, result = Set.new) ⇒ Object

Returns a flattened array of nodes from the node



19
20
21
22
23
24
25
26
# File 'lib/crawfish/node.rb', line 19

def flatten(filter=lambda{|n| true}, result=Set.new)
  result.add(self)
  associated_nodes.reject(&visited_models(result)).select(&filter).each do |node|
    result.add(node)
    node.flatten(filter, result)
  end
  result.to_a
end

#has_and_belongs_to_many?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/crawfish/node.rb', line 95

def has_and_belongs_to_many?
  reflection && reflection.macro == :has_and_belongs_to_many
end

#has_many?Boolean Also known as: below?

Returns:

  • (Boolean)


91
92
93
# File 'lib/crawfish/node.rb', line 91

def has_many?
  reflection && reflection.macro == :has_many
end

#has_one?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/crawfish/node.rb', line 83

def has_one?
  reflection && reflection.macro == :has_one
end

#locate(path) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/crawfish/node.rb', line 61

def locate(path)
  return self if path == model.to_s || path == ref_key.to_s

  next_element, *other_elements = path.split('/')
  if next_element == model.to_s
    next_element, *other_elements = other_elements
  end
  return self unless next_element

  next_node = associated_nodes.detect{|node| node.ref_key.to_s == next_element}
  next_node.locate(other_elements.join("/"))
end

#nodes_aboveObject



49
50
51
# File 'lib/crawfish/node.rb', line 49

def nodes_above
  associated_nodes.select(&:above?)
end

#nodes_asideObject



57
58
59
# File 'lib/crawfish/node.rb', line 57

def nodes_aside
  associated_nodes.select(&:aside?)
end

#nodes_belowObject



53
54
55
# File 'lib/crawfish/node.rb', line 53

def nodes_below
  associated_nodes.select(&:below?)
end

#other_modelObject



74
75
76
# File 'lib/crawfish/node.rb', line 74

def other_model
  reflection ? reflection.klass : nil
end

#pathObject



28
29
30
31
32
33
34
# File 'lib/crawfish/node.rb', line 28

def path
  if root?
    model.to_s
  else
    "#{parent.path}/#{ref_key}"
  end
end

#root?Boolean Also known as: entity?

Returns:

  • (Boolean)


78
79
80
# File 'lib/crawfish/node.rb', line 78

def root?
  parent.nil?
end

#through?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/crawfish/node.rb', line 99

def through?
  reflection && reflection.options[:through].present?
end