Module: Crawfish

Defined in:
lib/crawfish.rb,
lib/crawfish/node.rb,
lib/crawfish/version.rb

Defined Under Namespace

Classes: NoOpDecorator, Node

Constant Summary collapse

VERSION =
"0.0.1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.node_decoratorObject

Returns the value of attribute node_decorator.



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

def node_decorator
  @node_decorator
end

Class Method Details

.models(*entities) ⇒ Object

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



35
36
37
38
39
# 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) ⇒ Object

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.



23
24
25
26
27
28
29
30
# 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) ⇒ Object

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



11
12
13
14
15
16
17
# 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