Module Boson::CommentInspector

  1. lib/boson/inspectors/comment_inspector.rb

Scrapes comments right before a method for its attributes. Method attributes must begin with ’@’ i.e.:

# @desc Does foo
# @options :verbose=>true
def foo(options={})

Some rules about these attributes:

  • Attribute definitions can span multiple lines. When a new attribute starts a line or the comments end, then a definition ends.
  • If no @desc is found in the comment block, then the first comment line directly above the method is assumed to be the value for @desc. This means that no multi-line attribute definitions can occur without a description since the last line is assumed to be a description.
  • options, config and render_options attributes can take any valid ruby since they’re evaled in their module’s context.
  • desc attribute is not evaled and is simply text to be set as a string.

This module was inspired by pragdave.

Methods

public instance

  1. scrape

Constants

EVAL_ATTRIBUTES = [:options, :render_options, :config]

Public instance methods

scrape (file_string, line, mod, attribute=nil)

Given a method’s file string, line number and defining module, returns a hash of attributes defined for that method.

[show source]
# File lib/boson/inspectors/comment_inspector.rb, line 24
    def scrape(file_string, line, mod, attribute=nil)
      hash = scrape_file(file_string, line) || {}
      hash.select {|k,v| v && (attribute.nil? || attribute == k) }.each do |k,v|
        hash[k] = EVAL_ATTRIBUTES.include?(k) ? eval_comment(v.join(' '), mod) : v.join(' ')
      end
      attribute ? hash[attribute] : hash
    end