Class: SwitchFile::Source

Inherits:
Valuable
  • Object
show all
Defined in:
lib/switch_file/source.rb

Overview

A source file

Defined Under Namespace

Classes: CannotFindProjectPath, NoMatchingFileTypeForPath

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.project_path(path) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/switch_file/source.rb', line 6

def self.project_path(path)
  parent = Pow(path).parent
  if (parent / '.switch_file').file?
    parent.to_s
  elsif parent == Pow(path)
    raise CannotFindProjectPath.new(path.to_s)
  else
    project_path(parent)
  end
end

Instance Method Details

#class_nameObject



17
18
19
# File 'lib/switch_file/source.rb', line 17

def class_name
  path.match(file_type!.path_regex)[1]
end

#file_type!Object



21
22
23
24
25
# File 'lib/switch_file/source.rb', line 21

def file_type!
  project.file_types.detect{|file_type| path =~ file_type.path_regex }.tap do |result|
    raise NoMatchingFileTypeForPath.new(path) if result.nil?
  end
end

#projectObject



27
28
29
# File 'lib/switch_file/source.rb', line 27

def project
  @project ||= Project.new(:path => self.class.project_path(path))
end

#prompt_messageObject



31
32
33
34
35
36
37
# File 'lib/switch_file/source.rb', line 31

def prompt_message
  file_type_options = project.file_types.map{|file_type|
    "[#{file_type.shortcut}] #{file_type.name}: #{file_type.relative_path(self)}"
  }

  "Enter the shortcut of the file you want to open:\n\n#{file_type_options.join("\n")}\n\n"
end