Class: YARD::Bird::Rules

Inherits:
Object
  • Object
show all
Defined in:
lib/yard-bird/rules.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRules

Create new Rules object.



14
15
16
17
18
# File 'lib/yard-bird/rules.rb', line 14

def initialize()
  @patterns = {}
  load_rules
  inject_rules
end

Class Method Details

.loadObject

Returns [Rules]



9
10
11
# File 'lib/yard-bird/rules.rb', line 9

def self.load
  new
end

Instance Method Details

#inject_rulesObject

Rules are applied as YARD processes comments. All rules are applied in order until all rules have been tried, or a triggered rule returns a ‘:break` symbol, which will short-circuit further rule application.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/yard-bird/rules.rb', line 59

def inject_rules
  pats = patterns
  ::YARD::Docstring.class_eval do
    define_method :parse_comments do |comments|
      comments = comments.join("\n") if Array === comments
      result   = nil
      pats.each do |pattern, block|
        if md = pattern.match(comments)
          result = instance_exec(md, comments, &block) #block.call(md)
          break if result == :break
          comments = result if String === result
        end
      end
      comments.to_s #r.to_s
    end
  end
end

#load_rulesObject

Dot-bird filesa are loaded from ‘.yard/` project directory and sorted alphabetically. The sorting is useful for controlling application order when using multiple files –simply number the files, e.g.

.yard/01_special.bird
.yard/02_tomdoc.bird

In the example the special rules will have precedence over the tomdoc rules.

Returns:

  • Nothing



32
33
34
35
36
37
# File 'lib/yard-bird/rules.rb', line 32

def load_rules
  files = Dir[".yard/*.bird"].sort
  files.each do |file|
    instance_eval(File.read(file), file)
  end
end

#patternsObject

Patterns to apply.



52
53
54
# File 'lib/yard-bird/rules.rb', line 52

def patterns
  @patterns
end

#When(pattern, &block) ⇒ Proc

Define a new rule.

pattern - Regexp to match against comment.
block   - Proc for handling pattern match.

Returns:

  • (Proc)

    the given block



45
46
47
# File 'lib/yard-bird/rules.rb', line 45

def When(pattern, &block)
  patterns[pattern] = block
end