Class: ProScribe::Extractor

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

Overview

Class: Extractor (ProScribe) Extracts comments from list of files.

## Description

Gets the ones with comment blocks starting with `[...]`

## Common usage

ex = Extractor.new(Dir['./**/*.rb'])
ex.blocks

ex.blocks.map! { |b| b.file = "file: #{b.file}" }

ex.write!('manual/')       # Writes to manual/

Defined Under Namespace

Classes: Block

Instance Method Summary collapse

Constructor Details

#initialize(files, root, options = {}) ⇒ Extractor

Returns a new instance of Extractor.



23
24
25
26
# File 'lib/proscribe/extractor.rb', line 23

def initialize(files, root, options={})
  @files = files
  @root  = File.realpath(root)
end

Instance Method Details

#blocksObject

Method: blocks (ProScribe::Extractor) Returns an array of Blocks.



39
40
41
42
43
44
45
46
47
48
# File 'lib/proscribe/extractor.rb', line 39

def blocks
  @blocks ||= begin
    @files.map { |file|
      if File.file?(file)
        input = File.read(file)
        get_blocks(input, unroot(file))
      end
    }.compact.flatten
  end
end

#write!(output_path = '.', &blk) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/proscribe/extractor.rb', line 28

def write!(output_path = '.', &blk)
  blocks.each { |block|
    path = File.join(output_path, block.file)
    FileUtils.mkdir_p File.dirname(path)
    File.open(path, 'w') { |f| f.write block.body }
    yield block  if block_given?
  }
end