switch_file 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ *.gem
2
+ *.ps
3
+ git.bundle
4
+ .geanyprj
5
+ .rvmrc
6
+ .yardoc
7
+ Gemfile.lock
8
+ doc/*
9
+ files/*
10
+ pkg/*
11
+ tmp/*
data/.switch_file ADDED
@@ -0,0 +1,13 @@
1
+ [
2
+ {
3
+ shortcut: :s,
4
+ open_command: lambda {|class_name| "geany spec/lib/#{class_name}_spec.rb"},
5
+ path_regex: %r{spec/lib/(.*)_spec.rb}
6
+ },
7
+
8
+ {
9
+ shortcut: :l,
10
+ open_command: lambda {|class_name| "geany lib/#{class_name}.rb"},
11
+ path_regex: %r{lib/(.*).rb}
12
+ }
13
+ ]
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in switch_file.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,8 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec', :version => 2, :cli => "--color" do
5
+ watch(%r{^spec/.+_spec\.rb})
6
+ watch(%r{^lib/(.+)\.rb}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/switch_file ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require 'switch_file'
3
+
4
+ SwitchFile::App.start
@@ -0,0 +1,24 @@
1
+ module SwitchFile
2
+ class App < Thor
3
+ desc "execute PATH [OPTIONS]...", "Switch to the matching file type of PATH."
4
+
5
+ method_option "shortcut",
6
+ :aliases => "-s",
7
+ :desc => "the shortcut of the file type you wish to open",
8
+ :type => :string
9
+
10
+ method_option "config",
11
+ :aliases => "-c",
12
+ :desc => "configuration path of the project. PRESENT_WORKING_DIRECTORY/.switch_file by default.",
13
+ :type => :string
14
+
15
+ def execute(source_path)
16
+ SwitchFile.config_path = options['config']
17
+ sp = SourcePath.new(value: source_path)
18
+ shortcut = FileTypeShortcut.new(value: options['shortcut'] || ask(sp.prompt))
19
+ target_command = shortcut.file_type.generate_open_command(sp)
20
+ `#{target_command}`
21
+ end
22
+ end
23
+ end
24
+
@@ -0,0 +1,20 @@
1
+ module SwitchFile
2
+ # A type of file within the project
3
+ class FileType < Valuable
4
+ has_value :open_command
5
+ has_value :path_regex
6
+ has_value :shortcut
7
+
8
+ def self.all
9
+ @all ||= SwitchFile.file_type_attributes.map{|attributes| FileType.new(attributes) }
10
+ end
11
+
12
+ def self.all=(file_types)
13
+ @all = file_types
14
+ end
15
+
16
+ def generate_open_command(source_path)
17
+ open_command.call(source_path.class_name)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,10 @@
1
+ module SwitchFile
2
+ # Short cut of a FileType
3
+ class FileTypeShortcut < Valuable
4
+ has_value :value
5
+
6
+ def file_type
7
+ FileType.all.detect{|file_type| self.value.to_s == file_type.shortcut.to_s}
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,14 @@
1
+ module SwitchFile
2
+ # Path to a source file
3
+ class SourcePath < Valuable
4
+ has_value :value
5
+
6
+ def class_name
7
+ value.match(file_type.path_regex)[1]
8
+ end
9
+
10
+ def file_type
11
+ FileType.all.detect{|file_type| value =~ file_type.path_regex }
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module SwitchFile
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,27 @@
1
+ require 'pow'
2
+ require 'thor'
3
+ require 'valuable'
4
+
5
+ require 'switch_file/app'
6
+ require 'switch_file/file_type'
7
+ require 'switch_file/file_type_shortcut'
8
+ require 'switch_file/source_path'
9
+ require "switch_file/version"
10
+
11
+ module SwitchFile
12
+ def self.config_path
13
+ @config_path ||= (Pow(Dir.pwd) / '.switch_file')
14
+ end
15
+
16
+ def self.config_path=(value)
17
+ @config_path = value && Pow(value)
18
+ end
19
+
20
+ def self.file_type_attributes
21
+ @file_type_attributes ||= eval(config_path.read)
22
+ end
23
+
24
+ def self.file_type_attributes=(attributes_array)
25
+ @file_type_attributes = attributes_array
26
+ end
27
+ end
@@ -0,0 +1,13 @@
1
+ [
2
+ {
3
+ shortcut: :s,
4
+ open_command: lambda {|class_name| "echo spec/lib/#{class_name}_spec.rb > tmp/result.txt"},
5
+ path_regex: %r{spec/fixtures/project/spec/lib/(.*)_spec.rb}
6
+ },
7
+
8
+ {
9
+ shortcut: :l,
10
+ open_command: lambda {|class_name| "echo lib/#{class_name}_spec.rb > tmp/result.txt"},
11
+ path_regex: %r{spec/fixtures/project/lib/(.*).rb}
12
+ }
13
+ ]
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe SwitchFile::App do
4
+ describe "#execute" do
5
+ it "should open the matching file_type of the source_path" do
6
+ app = described_class.new
7
+ app.invoke :execute, ["spec/fixtures/project/lib/project/sample.rb"], "shortcut" => 's', "config" => 'spec/fixtures/project/.switch_file'
8
+ (test_tmp_dir / 'result.txt').read.chomp.should == 'spec/lib/project/sample_spec.rb'
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe SwitchFile::FileTypeShortcut do
4
+ describe "#file_type" do
5
+ it "should be the file_type with the same name as the short cut" do
6
+ file_type = FileType.new(shortcut: :s)
7
+ FileType.all = [file_type]
8
+
9
+ short_cut = described_class.new(value: :s)
10
+ short_cut.file_type.shortcut.should == :s
11
+ end
12
+
13
+ it "should be nil if there is no matching file type" do
14
+ file_type = FileType.new(shortcut: :l)
15
+ FileType.all = [file_type]
16
+
17
+ short_cut = described_class.new(value: :s)
18
+ short_cut.file_type.should be_nil
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe SwitchFile::FileType do
4
+ describe "#all" do
5
+ it "should be the set file types, if available" do
6
+ file_type = FileType.new(shortcut: :s)
7
+ FileType.all = [file_type]
8
+ FileType.all.should == [file_type]
9
+ end
10
+
11
+ it "should be the file types from the file_type_attributes, if there are no set file types" do
12
+ FileType.all = nil
13
+
14
+ SwitchFile.file_type_attributes = [{shortcut: :s}]
15
+
16
+ FileType.all.should have(1).file_type
17
+ FileType.all[0].shortcut.should == :s
18
+ end
19
+ end
20
+
21
+ describe "#generate_open_command" do
22
+ it "should return the command for opening the desired file type of the source_path" do
23
+ spec_file_type = FileType.new(open_command: lambda{|class_name|
24
+ "geany spec/lib/#{class_name}_spec.rb"
25
+ })
26
+
27
+ lib_file_type = FileType.new(path_regex: %r{lib/(.*).rb$})
28
+
29
+ FileType.all = [lib_file_type, spec_file_type]
30
+
31
+ command = spec_file_type.generate_open_command(SourcePath.new(value: '/home/user/project/lib/project/some_class.rb'))
32
+ command.should == "geany spec/lib/project/some_class_spec.rb"
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe SwitchFile::SourcePath do
4
+ describe "#class_name" do
5
+ it "should be the part of the value that is the class name of the source path" do
6
+ FileType.all = [FileType.new(path_regex: %r{lib/(.*).rb$})]
7
+
8
+ source_path = SourcePath.new(value: '/home/user/project/lib/project/some_class.rb')
9
+ source_path.class_name.should == 'project/some_class'
10
+ end
11
+ end
12
+
13
+ describe "#file_type" do
14
+ it "should be the file type that matches the source path" do
15
+ file_type = FileType.new(path_regex: %r{lib/(.*).rb$})
16
+ FileType.all = [file_type]
17
+
18
+ source_path = SourcePath.new(value: '/home/user/project/lib/project/some_class.rb')
19
+ source_path.file_type.should == file_type
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe SwitchFile do
4
+ describe ".config_path" do
5
+ it "should be the .switch_file in the present working directory, by default" do
6
+ SwitchFile.config_path = nil
7
+ SwitchFile.config_path.should == `pwd`.chomp + '/.switch_file'
8
+ end
9
+
10
+ it "should be the given config_path, if provided" do
11
+ SwitchFile.config_path = 'spec/fixtures/project/.switch_file'
12
+ SwitchFile.config_path.should be_a(Pow::Base)
13
+ SwitchFile.config_path.to_s.should =~ /spec\/fixtures\/project\/.switch_file/
14
+ end
15
+ end
16
+
17
+ describe ".config_path=" do
18
+ it "should set the stored config_path to nil, if value is nil" do
19
+ SwitchFile.config_path = nil
20
+ SwitchFile.instance_variable_get(:@config_path).should be_nil
21
+ end
22
+ end
23
+
24
+ describe ".file_type_attributes" do
25
+ it "should be the set file_type_attributes, if available" do
26
+ attribute_hash = {shortcut: :s}
27
+ SwitchFile.file_type_attributes = [attribute_hash]
28
+ SwitchFile.file_type_attributes.should == [attribute_hash]
29
+ end
30
+
31
+ it "should be the file_type_attributes from the config_path, if there are no set file_type_attributes" do
32
+ SwitchFile.file_type_attributes = nil
33
+ SwitchFile.config_path = Pow('spec/fixtures/project/.switch_file')
34
+ SwitchFile.file_type_attributes.should be_an(Array)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,18 @@
1
+ require 'pry'
2
+ require 'switch_file'
3
+
4
+ RSpec.configure do |config|
5
+ include SwitchFile
6
+
7
+ config.before :each do
8
+ test_tmp_dir.delete! if test_tmp_dir.exists?
9
+ test_tmp_dir.create_directory
10
+ SwitchFile.config_path = Pow('spec/fixtures/project/.switch_file')
11
+ SwitchFile.file_type_attributes = nil
12
+ FileType.all = nil
13
+ end
14
+
15
+ def test_tmp_dir
16
+ Pow('tmp')
17
+ end
18
+ end
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "switch_file/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "switch_file"
7
+ s.version = SwitchFile::VERSION
8
+ s.authors = ["George Mendoza"]
9
+ s.email = ["gsmendoza@gmail.com"]
10
+ s.homepage = "https://github.com/gsmendoza/switch_file"
11
+ s.summary = "A configurable utility for jumping across files"
12
+ s.description = "A configurable utility for jumping across files"
13
+
14
+ s.rubyforge_project = "switch_file"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_runtime_dependency 'pow'
22
+ s.add_runtime_dependency 'thor'
23
+ s.add_runtime_dependency 'valuable'
24
+
25
+ s.add_development_dependency "guard"
26
+ s.add_development_dependency "guard-rspec"
27
+ s.add_development_dependency "pry"
28
+ s.add_development_dependency "rspec"
29
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: switch_file
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - George Mendoza
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: pow
16
+ requirement: &69981220 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *69981220
25
+ - !ruby/object:Gem::Dependency
26
+ name: thor
27
+ requirement: &69980910 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *69980910
36
+ - !ruby/object:Gem::Dependency
37
+ name: valuable
38
+ requirement: &69980370 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *69980370
47
+ - !ruby/object:Gem::Dependency
48
+ name: guard
49
+ requirement: &69979850 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *69979850
58
+ - !ruby/object:Gem::Dependency
59
+ name: guard-rspec
60
+ requirement: &69979400 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *69979400
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: &69979100 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *69979100
80
+ - !ruby/object:Gem::Dependency
81
+ name: rspec
82
+ requirement: &69978770 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *69978770
91
+ description: A configurable utility for jumping across files
92
+ email:
93
+ - gsmendoza@gmail.com
94
+ executables:
95
+ - switch_file
96
+ extensions: []
97
+ extra_rdoc_files: []
98
+ files:
99
+ - .gitignore
100
+ - .switch_file
101
+ - Gemfile
102
+ - Guardfile
103
+ - Rakefile
104
+ - bin/switch_file
105
+ - lib/switch_file.rb
106
+ - lib/switch_file/app.rb
107
+ - lib/switch_file/file_type.rb
108
+ - lib/switch_file/file_type_shortcut.rb
109
+ - lib/switch_file/source_path.rb
110
+ - lib/switch_file/version.rb
111
+ - spec/fixtures/project/.switch_file
112
+ - spec/lib/switch_file/app_spec.rb
113
+ - spec/lib/switch_file/file_type_shortcut_spec.rb
114
+ - spec/lib/switch_file/file_type_spec.rb
115
+ - spec/lib/switch_file/source_path_spec.rb
116
+ - spec/lib/switch_file_spec.rb
117
+ - spec/spec_helper.rb
118
+ - switch_file.gemspec
119
+ homepage: https://github.com/gsmendoza/switch_file
120
+ licenses: []
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ none: false
127
+ requirements:
128
+ - - ! '>='
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ none: false
133
+ requirements:
134
+ - - ! '>='
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubyforge_project: switch_file
139
+ rubygems_version: 1.8.6
140
+ signing_key:
141
+ specification_version: 3
142
+ summary: A configurable utility for jumping across files
143
+ test_files: []