class Crawfish::Node

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

Attributes

model[R]
node_decorator[RW]
parent[RW]
ref_key[R]
reflection[R]

Public Class Methods

new(model, opts={}) click to toggle source
# 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

Public Instance Methods

above?()
Alias for: belongs_to?
aside?() click to toggle source
# File lib/crawfish/node.rb, line 106
def aside?
  has_one? || has_and_belongs_to_many?
end
associated_nodes() click to toggle source
# 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?() click to toggle source
# File lib/crawfish/node.rb, line 87
def belongs_to?
  reflection && reflection.macro == :belongs_to
end
Also aliased as: above?
below?()
Alias for: has_many?
entity?()
Alias for: root?
flatten(filter=lambda{|n| true}, result=Set.new) click to toggle source

Returns a flattened array of nodes from the node

# 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?() click to toggle source
# File lib/crawfish/node.rb, line 95
def has_and_belongs_to_many?
  reflection && reflection.macro == :has_and_belongs_to_many
end
has_many?() click to toggle source
# File lib/crawfish/node.rb, line 91
def has_many?
  reflection && reflection.macro == :has_many
end
Also aliased as: below?
has_one?() click to toggle source
# File lib/crawfish/node.rb, line 83
def has_one?
  reflection && reflection.macro == :has_one
end
locate(path) click to toggle source
# 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() click to toggle source
# File lib/crawfish/node.rb, line 49
def nodes_above
  associated_nodes.select(&:above?)
end
nodes_aside() click to toggle source
# File lib/crawfish/node.rb, line 57
def nodes_aside
  associated_nodes.select(&:aside?)
end
nodes_below() click to toggle source
# File lib/crawfish/node.rb, line 53
def nodes_below
  associated_nodes.select(&:below?)
end
other_model() click to toggle source
# File lib/crawfish/node.rb, line 74
def other_model
  reflection ? reflection.klass : nil
end
path() click to toggle source
# File lib/crawfish/node.rb, line 28
def path
  if root?
    model.to_s
  else
    "#{parent.path}/#{ref_key}"
  end
end
root?() click to toggle source
# File lib/crawfish/node.rb, line 78
def root?
  parent.nil?
end
Also aliased as: entity?
through?() click to toggle source
# File lib/crawfish/node.rb, line 99
def through?
  reflection && reflection.options[:through].present?
end

Private Instance Methods

visited_models(result) click to toggle source
# File lib/crawfish/node.rb, line 111
def visited_models(result)
  lambda do |node|
    result.detect{|n| n.model == node.model}
  end
end