Class: FileParser

Inherits:
Object
  • Object
show all
Defined in:
lib/file_parser.rb

Overview

Parses code in a file

Constant Summary collapse

RUNNABLE_TAG =
:runnable

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ FileParser

Parse file with #YARD::CLI::Stats

Parameters:

  • file_path (String)

    path to the file to be parsed



14
15
16
17
18
19
# File 'lib/file_parser.rb', line 14

def initialize(file_path)
  raise ConsoleRunnerError "Cannot find file #{file_path}" unless File.exist?(file_path)
  code = YARD::CLI::Stats.new
  code.run(file_path)
  @all_objects = code.all_objects
end

Instance Attribute Details

#all_objectsObject (readonly)

array of #YARD::CodeObjects



9
10
11
# File 'lib/file_parser.rb', line 9

def all_objects
  @all_objects
end

Class Method Details

.select_option_tags(yard_object) ⇒ Array(YARD::Tags::Tag)

Select all @option tags from of specified object

Parameters:

  • yard_object (YARD::CodeObjects::MethodObject, YARD::CodeObjects::ClassObject)

    YARD object

Returns:

  • (Array(YARD::Tags::Tag))


76
77
78
# File 'lib/file_parser.rb', line 76

def self.select_option_tags(yard_object)
  yard_object.tags.select { |t| t.tag_name == 'option' }
end

.select_param_tags(yard_object) ⇒ Array(YARD::Tags::Tag)

Select all @param tags from of specified object

Parameters:

  • yard_object (YARD::CodeObjects::MethodObject, YARD::CodeObjects::ClassObject)

    YARD object

Returns:

  • (Array(YARD::Tags::Tag))


84
85
86
# File 'lib/file_parser.rb', line 84

def self.select_param_tags(yard_object)
  yard_object.tags.select { |t| t.tag_name == 'param' }
end

.select_runnable_tags(yard_object) ⇒ Array(YARD::Tags::Tag)

Select all @runnable tags from of specified object

Parameters:

  • yard_object (YARD::CodeObjects::MethodObject, YARD::CodeObjects::ClassObject)

    YARD object

Returns:

  • (Array(YARD::Tags::Tag))


68
69
70
# File 'lib/file_parser.rb', line 68

def self.select_runnable_tags(yard_object)
  yard_object.tags.select { |t| t.tag_name == RUNNABLE_TAG.to_s }
end

Instance Method Details

#list_classes(scope = :all) ⇒ Array(YARD::CodeObjects::ClassObject)

List classes

Parameters:

  • scope (Symbol) (defaults to: :all)

    :all - list all classes, :runnable - list only runnable classes

Returns:

  • (Array(YARD::CodeObjects::ClassObject))


52
53
54
55
56
57
58
59
60
61
62
# File 'lib/file_parser.rb', line 52

def list_classes(scope= :all)
  all_classes = @all_objects.select { |o| o.type == :class }
  case scope
    when :all
      all_classes
    when RUNNABLE_TAG
      all_classes.select { |m| m.has_tag? RUNNABLE_TAG }
    else
      raise ":scope can be :all or #{RUNNABLE_TAG}"
  end
end

#list_methods(scope = :all, clazz = nil) ⇒ Array(YARD::CodeObjects::MethodObject)

List of methods

Parameters:

  • scope (Symbol) (defaults to: :all)

    :all - list all methods, :runnable - list only runnable methods

  • clazz (YARD::CodeObjects::ClassObject, nil) (defaults to: nil)

    list methods of specified class only

Returns:

  • (Array(YARD::CodeObjects::MethodObject))


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/file_parser.rb', line 25

def list_methods(scope = :all, clazz = nil)
  all_methods       = @all_objects.select { |o| o.type == :method }
  all_class_methods = []
  all_class_methods = clazz.children.select { |m| m.class == YARD::CodeObjects::MethodObject } if clazz

  case scope
    when :all
      if clazz
        all_class_methods
      else
        all_methods
      end
    when RUNNABLE_TAG
      if clazz
        all_class_methods.select { |m| m.has_tag? RUNNABLE_TAG }
      else
        all_methods.select { |m| m.has_tag? RUNNABLE_TAG }
      end
    else
      raise ":scope can be :all or #{RUNNABLE_TAG}"
  end
end