module Crawfish

Constants

VERSION

Attributes

node_decorator[RW]

Public Class Methods

models(*entities) click to toggle source

Returns the unique list of models in the graphs from the specified entities.

# File lib/crawfish.rb, line 35
def models(*entities)
  visited_models = Set.new
  filter = lambda {|n| visited_models.add?(n.model)}
  nodes(*entities, filter: filter).map(&:model)
end
nodes(*args) click to toggle source

Returns the flattened list of nodes for the specified entities. Can also accept a hash of options where :filter can be a lambda to filter results.

# File lib/crawfish.rb, line 23
def nodes(*args)
  opts, *entities = extract(*args)
  filter = opts[:filter] ||= lambda{|n| true}
  node_decorator = opts[:node_decorator] || NoOpDecorator
  entities.map do |entity|
    node_decorator.decorate(Node.new(entity, node_decorator: node_decorator)).flatten(filter)
  end.flatten
end
trees(*args) click to toggle source

Returns trees of nodes for each entity. Will be an array of root nodes.

# File lib/crawfish.rb, line 11
def trees(*args)
  opts, *entities = extract(*args)
  node_decorator = opts[:node_decorator] || NoOpDecorator
  entities.map do |entity|
    node_decorator.decorate(Node.new(entity, node_decorator: node_decorator))
  end
end

Private Class Methods

extract(*args) click to toggle source
# File lib/crawfish.rb, line 42
def extract(*args)
  opts = args.last.is_a?(Hash) ? args.pop : {}
  entities = args
  [opts] + entities
end