Class: Yome::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/yome/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir) ⇒ Parser

Returns a new instance of Parser.



8
9
10
11
12
13
# File 'lib/yome/parser.rb', line 8

def initialize(dir)
  @file_hash = {}
  @chips = []

  collect_chips(dir)
end

Instance Attribute Details

#chipsObject (readonly)

Returns the value of attribute chips.



6
7
8
# File 'lib/yome/parser.rb', line 6

def chips
  @chips
end

#file_hashObject (readonly)

Returns the value of attribute file_hash.



6
7
8
# File 'lib/yome/parser.rb', line 6

def file_hash
  @file_hash
end

Instance Method Details

#collect_chips(dir) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/yome/parser.rb', line 15

def collect_chips(dir)
  Dir.chdir(dir) do
    Find.find(".") do |path|
      Find.prune if Lib::ignore?(File.basename(path))
      next if FileTest.directory?(path) || Lib::binary?(path)
  
      begin
        contents = File.read(path).split("\n")
      rescue ArgumentError
        STDERR.puts "Skip: #{path}"
        next
      end
  
      contents.each_with_index do |line, i|
        if line =~ /YOME:/
          path = path.gsub(/^\.\//, "")
          @file_hash[path] = contents
          @chips << Chip.new(line, path, i)
        end
      end 
    end
  end
end