Class: DSLCompose::Parser::ForChildrenOfParser::Descendants

Inherits:
Object
  • Object
show all
Defined in:
lib/dsl_compose/parser/for_children_of_parser/descendants.rb

Instance Method Summary collapse

Constructor Details

#initialize(base_class, final_children_only, skip_classes = []) ⇒ Descendants

Returns a new instance of Descendants.



7
8
9
10
11
# File 'lib/dsl_compose/parser/for_children_of_parser/descendants.rb', line 7

def initialize base_class, final_children_only, skip_classes = []
  @base_class = base_class
  @final_children_only = final_children_only
  @skip_classes = skip_classes
end

Instance Method Details

#classesObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/dsl_compose/parser/for_children_of_parser/descendants.rb', line 13

def classes
  # all objects which extend the provided base class
  extending_classes = subclasses @base_class

  # sort the results
  #
  # classes are ordered first by name
  extending_classes.sort_by! do |child_class|
    "#{child_class.name.split("::").count}_#{has_descendants(child_class) ? 0 : 1}_#{child_class.name}"
  end
  # then by ansestory chain (i.e. ancestors before descendants)
  extending_classes.sort! do |a, b|
    if a < b
      1
    elsif a > b
      -1
    else
      a.name <=> b.name
    end
  end

  # reject any classes which we are skipping
  extending_classes.reject! do |child_class|
    @skip_classes.include? child_class.name
  end

  # if this is not a final child, but we are processing final children only, then skip it
  if @final_children_only
    # reject any classes which have descendants
    extending_classes.reject! do |child_class|
      has_descendants child_class
    end
  end

  extending_classes
end