Class: DSLCompose::Parser::ForChildrenOfParser::Descendents

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

Instance Method Summary collapse

Constructor Details

#initialize(base_class, final_children_only) ⇒ Descendents

Returns a new instance of Descendents.



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

def initialize base_class, final_children_only
  @base_class = base_class
  @final_children_only = final_children_only
end

Instance Method Details

#classesObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/dsl_compose/parser/for_children_of_parser/descendents.rb', line 12

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

  # sort the results, classes are ordered first by the depth of their namespace, and second
  # by the presence of decendents and finally by their name
  extending_classes.sort_by! do |child_class|
    "#{child_class.name.split("::").count}_#{has_descendents(child_class) ? 0 : 1}_#{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 descendents
    extending_classes.reject! do |child_class|
      has_descendents child_class
    end
  end

  extending_classes
end