Method: Danger::PluginParser#to_h

Defined in:
lib/danger/plugin_support/plugin_parser.rb

#to_h(classes) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/danger/plugin_support/plugin_parser.rb', line 166

def to_h(classes)
  classes.map do |klass|
    # Adds the class being parsed into the ruby runtime, so that we can access it's instance_name
    require klass.file
    real_klass = Danger.const_get klass.name
    attribute_meths = klass.attributes[:instance].values.map(&:values).flatten

    methods = klass.meths - klass.inherited_meths - attribute_meths
    usable_methods = methods.select { |m| m.visibility == :public }.reject { |m| m.name == :initialize || m.name == :instance_name || m.name == :new }

    plugin_gem = klass.file.include?("gems") ? klass.file.split("gems/").last.split("-")[0..-2].join("-") : nil
    # Pull out the gem's path ( to make relative file paths )
    # if no gem is found, index  = 0, making gem_path = ""
    index_of_gem_in_path = plugin_gem ? klass.file.split("/").index { |component| component.include? plugin_gem } : 0
    gem_path = klass.file.split("/")[0..index_of_gem_in_path].join("/")

    {
      name: klass.name.to_s,
      body_md: klass.docstring,
      instance_name: real_klass.instance_name,
      gem: plugin_gem,
      gem_path: gem_path,
      files: klass.files.map { |item| [item.first.gsub(gem_path, ""), item.last] },
      example_code: klass.tags.select { |t| t.tag_name == "example" }.map { |tag| { title: tag.name, text: tag.text } }.compact,
      attributes: klass.attributes[:instance].map { |pair| { pair.first => attribute_parser(gem_path, pair.last) } },
      methods: usable_methods.map { |m| method_parser(gem_path, m) },
      tags: klass.tags.select { |t| t.tag_name == "tags" }.map(&:text).compact,
      see: klass.tags.select { |t| t.tag_name == "see" }.map(&:name).map(&:split).flatten.compact,
    }
  end
end