Class: Danger::PluginParser

Inherits:
Object
  • Object
show all
Defined in:
lib/danger/plugin_support/plugin_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ PluginParser

Returns a new instance of PluginParser.



7
8
9
# File 'lib/danger/plugin_support/plugin_parser.rb', line 7

def initialize(path)
  @path = path
end

Instance Attribute Details

#registryObject

Returns the value of attribute registry.



5
6
7
# File 'lib/danger/plugin_support/plugin_parser.rb', line 5

def registry
  @registry
end

Instance Method Details

#classes_in_fileObject



20
21
22
23
24
# File 'lib/danger/plugin_support/plugin_parser.rb', line 20

def classes_in_file
  self.registry.all
      .select { |thing| thing.type == :class }
      .select { |klass| klass.file == @path }
end

#parseObject



11
12
13
14
15
16
17
18
# File 'lib/danger/plugin_support/plugin_parser.rb', line 11

def parse
  # could this go in a singleton-y place instead?
  # like class initialize?
  YARD::Tags::Library.define_tag('tags', :tags)

  files = ["lib/danger/plugin_support/plugin.rb", @path]
  self.registry = YARD::Registry.load(files, true)
end

#plugins_from_classes(classes) ⇒ Object



26
27
28
# File 'lib/danger/plugin_support/plugin_parser.rb', line 26

def plugins_from_classes(classes)
  classes.select { |klass| klass.inheritance_tree.map(&:name).include? :Plugin }
end

#to_dict(classes) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/danger/plugin_support/plugin_parser.rb', line 30

def to_dict(classes)
  d_meth = lambda do |meth|
    return nil if meth.nil?
    {
      name: meth.name,
      body_md: meth.docstring,
      tags: meth.tags.map do |t|
        {
           name: t.tag_name,
           types: t.types
        }
      end
    }
  end

  d_attr = lambda do |attribute|
    {
      read: d_meth.call(attribute[:read]),
      write: d_meth.call(attribute[:write])
    }
  end

  classes.map do |klass|
    {
    name: klass.name.to_s,
    body_md: klass.docstring,
    example_code: klass.tags.select { |t| t.tag_name == "example" }.map(&:text).compact,
    attributes: klass.attributes[:instance].map do |pair|
      {
      pair.first => d_attr.call(pair.last)
    }
    end,
    methods: (klass.meths - klass.inherited_meths).select { |m| m.visibility == :public }.map { |m| d_meth.call(m) },
    tags: klass.tags.select { |t| t.tag_name == "tags" }.map(&:name).compact,
    see: klass.tags.select { |t| t.tag_name == "see" }.map(&:name).map(&:split).flatten.compact,
    file: klass.file
  }
  end
end