Class: Cfoo::FileSystem

Inherits:
Object show all
Defined in:
lib/cfoo/file_system.rb

Instance Method Summary collapse

Constructor Details

#initialize(project_root, yaml_parser) ⇒ FileSystem

Returns a new instance of FileSystem.



22
23
24
# File 'lib/cfoo/file_system.rb', line 22

def initialize(project_root, yaml_parser)
    @project_root, @yaml_parser = project_root, yaml_parser
end

Instance Method Details

#find_coordinates(string, file_name) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cfoo/file_system.rb', line 38

def find_coordinates(string, file_name)
    open(file_name) do |file|
        content = file.read
        lines = string.split "\n"
        patterns = lines.map { |line| Regexp.escape(line) }
        pattern = patterns.join '\n\s*'
        regex = %r[#{pattern}]
        index = regex =~ content
        if index.nil?
            raise CoordinatesNotFound, "Couldn't find '#{string.inspect}' in '#{file_name}'"
        else
            content.coordinates_of_index(index)
        end
    end
end

#glob_relative(path) ⇒ Object



54
55
56
57
58
59
# File 'lib/cfoo/file_system.rb', line 54

def glob_relative(path)
    absolute_files = Dir.glob(resolve_file path)
    absolute_files.map do |file|
        file.gsub(@project_root + '/', '')
    end
end

#list(path) ⇒ Object



61
62
63
# File 'lib/cfoo/file_system.rb', line 61

def list(path)
    Dir.glob("#{resolve_file path}/*")
end

#open(file_name, &block) ⇒ Object



34
35
36
# File 'lib/cfoo/file_system.rb', line 34

def open(file_name, &block)
    File.open(resolve_file(file_name), &block)
end

#parse_file(file_name) ⇒ Object



30
31
32
# File 'lib/cfoo/file_system.rb', line 30

def parse_file(file_name)
    @yaml_parser.load_file(resolve_file file_name)
end

#resolve_file(file_name) ⇒ Object



26
27
28
# File 'lib/cfoo/file_system.rb', line 26

def resolve_file(file_name)
    "#{@project_root}/#{file_name}"
end