Class: DressCode::Extractor

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Extractor

Returns a new instance of Extractor.



8
9
10
11
12
# File 'lib/dress_code/extractor.rb', line 8

def initialize(opts)
  @files = opts[:files]
  @base_dir = opts[:base_dir]
  @matcher = %r{/*\s+@styleguide\s+([^\n]+)(.+?)\*/}m
end

Instance Attribute Details

#filesObject

Returns the value of attribute files.



6
7
8
# File 'lib/dress_code/extractor.rb', line 6

def files
  @files
end

Instance Method Details

#create_doc(match, path) ⇒ Object

must return an instance of Doc



35
36
37
38
39
40
41
42
# File 'lib/dress_code/extractor.rb', line 35

def create_doc(match, path)
  DressCode::Document.new({
    :component => match[0],
    :prose => match[1].strip,
    :path => path,
    :relative_path => path.gsub(@base_dir, '').gsub(/^\//, '')
  })
end

#extractObject Also known as: docs



14
15
16
# File 'lib/dress_code/extractor.rb', line 14

def extract
  parse_files.sort_by { |doc| doc.component }
end

#parse_file(path) ⇒ Object



23
24
25
26
27
28
# File 'lib/dress_code/extractor.rb', line 23

def parse_file(path)
  src = File.read(path)
  matches = scan(src)
  return unless matches.length
  matches.map { |match| create_doc(match, path) }
end

#parse_filesObject



19
20
21
# File 'lib/dress_code/extractor.rb', line 19

def parse_files
  @files.flat_map { |path| parse_file(path) }.compact
end

#scan(src) ⇒ Object



30
31
32
# File 'lib/dress_code/extractor.rb', line 30

def scan(src)
  src.scan(@matcher)
end