Class: Crawfish::Node
- Inherits:
-
Object
- Object
- Crawfish::Node
- Defined in:
- lib/crawfish/node.rb
Overview
A node within the graph of models from a root or entity node.
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#node_decorator ⇒ Object
Returns the value of attribute node_decorator.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#ref_key ⇒ Object
readonly
Returns the value of attribute ref_key.
-
#reflection ⇒ Object
readonly
Returns the value of attribute reflection.
Instance Method Summary collapse
- #aside? ⇒ Boolean
- #associated_nodes ⇒ Object
- #belongs_to? ⇒ Boolean (also: #above?)
-
#flatten(filter = lambda{|n| true}, result = Set.new) ⇒ Object
Returns a flattened array of nodes from the node.
- #has_and_belongs_to_many? ⇒ Boolean
- #has_many? ⇒ Boolean (also: #below?)
- #has_one? ⇒ Boolean
-
#initialize(model, opts = {}) ⇒ Node
constructor
A new instance of Node.
- #locate(path) ⇒ Object
- #nodes_above ⇒ Object
- #nodes_aside ⇒ Object
- #nodes_below ⇒ Object
- #other_model ⇒ Object
- #path ⇒ Object
- #root? ⇒ Boolean (also: #entity?)
- #through? ⇒ Boolean
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
#model ⇒ Object (readonly)
Returns the value of attribute model.
7 8 9 |
# File 'lib/crawfish/node.rb', line 7 def model @model end |
#node_decorator ⇒ Object
Returns the value of attribute node_decorator.
8 9 10 |
# File 'lib/crawfish/node.rb', line 8 def node_decorator @node_decorator end |
#parent ⇒ Object
Returns the value of attribute parent.
8 9 10 |
# File 'lib/crawfish/node.rb', line 8 def parent @parent end |
#ref_key ⇒ Object (readonly)
Returns the value of attribute ref_key.
7 8 9 |
# File 'lib/crawfish/node.rb', line 7 def ref_key @ref_key end |
#reflection ⇒ Object (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
106 107 108 |
# File 'lib/crawfish/node.rb', line 106 def aside? has_one? || has_and_belongs_to_many? end |
#associated_nodes ⇒ Object
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?
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
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?
91 92 93 |
# File 'lib/crawfish/node.rb', line 91 def has_many? reflection && reflection.macro == :has_many end |
#has_one? ⇒ 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_above ⇒ Object
49 50 51 |
# File 'lib/crawfish/node.rb', line 49 def nodes_above associated_nodes.select(&:above?) end |
#nodes_aside ⇒ Object
57 58 59 |
# File 'lib/crawfish/node.rb', line 57 def nodes_aside associated_nodes.select(&:aside?) end |
#nodes_below ⇒ Object
53 54 55 |
# File 'lib/crawfish/node.rb', line 53 def nodes_below associated_nodes.select(&:below?) end |
#other_model ⇒ Object
74 75 76 |
# File 'lib/crawfish/node.rb', line 74 def other_model reflection ? reflection.klass : nil end |
#path ⇒ Object
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?
78 79 80 |
# File 'lib/crawfish/node.rb', line 78 def root? parent.nil? end |
#through? ⇒ Boolean
99 100 101 |
# File 'lib/crawfish/node.rb', line 99 def through? reflection && reflection.[:through].present? end |