method_match 0.0.10 → 0.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0c3fc86a651ea860312b1d9bcec96db5c356197a
4
- data.tar.gz: 566b680a41969a6ae1506238ad4822ba616021bd
3
+ metadata.gz: ee873555b2c027ef3eec2bbd005abd93a019bd66
4
+ data.tar.gz: 07b5159a0c193903aa91aa2a3472b8e9418e144d
5
5
  SHA512:
6
- metadata.gz: 35b69ccc2e703d25e2573b54234fd945107c786f6b92c76f32757e58445633756fdd2cdff37eb9acabefcd7e3b06f5fe413d9d4847632c0ca65d6dd9904d0dc0
7
- data.tar.gz: 8df004f9944e29c71ca1a65ade83764f95acae8942b3dd97a55565e054c13e64da383963fe383677e3e4249a4da81525df1dfcf647c7b0a71d506b2a03d92eaa
6
+ metadata.gz: 081500d42959a52e57eca26a67374509f954d663cb00301ac6d5a92d4c60de3044dc02a7559bb5181bb8af52dfcec799156ab3cedd67beef34292d35d0c33287
7
+ data.tar.gz: f9d6e1bc2fb5683f36e718ea5598da8cd86239cc8d746be72301603f36557022e1d70bef2f91c240a72c15f58848d658e1bbe949ce919326ab4269d68b979564
data/.watchr CHANGED
@@ -1,7 +1,7 @@
1
1
  watch("spec/.*/*_spec.rb") do |match|
2
- system "bundle exec rspec spec"
2
+ system "rspec spec"
3
3
  end
4
4
 
5
5
  watch("lib/(.*/.*).rb") do |match|
6
- system "bundle exec rspec spec"
6
+ system "rspec spec"
7
7
  end
data/lib/method_match.rb CHANGED
@@ -1,9 +1,9 @@
1
1
  require "method_match/version"
2
2
 
3
3
  module MethodMatch
4
- autoload :Matcher, 'method_match/matcher'
5
- autoload :Pry, 'method_match/pry'
6
- autoload :CommandRunner, 'method_match/command_runner'
7
- autoload :NameExtractor, 'method_match/name_extractor'
8
- Pry.setup
4
+ require 'method_match/matcher'
5
+ require 'method_match/pry'
6
+ require 'method_match/command_runner'
7
+ require 'method_match/name_extractor'
8
+ Pry.new.setup
9
9
  end
@@ -9,9 +9,10 @@ module MethodMatch
9
9
  require 'pathname'
10
10
  if Pathname.new(@matcher.spec_name).file?
11
11
  ::Pry.run_command rspec_command
12
- else
13
- puts "Spec #{@matcher.spec_name} does not exist"
12
+ return true
14
13
  end
14
+ puts "Spec #{@matcher.spec_name} does not exist"
15
+ false
15
16
  end
16
17
 
17
18
  def rspec_command
@@ -3,52 +3,61 @@ module MethodMatch
3
3
  attr_accessor :name, :line, :spec_name
4
4
  SPEC_MODE = 'spec'
5
5
  CODE_MODE = 'code'
6
+ RUBY_FILETYPE = ".rb"
7
+ SPEC_FILE_ENDING = '_spec.rb'
8
+ METHOD_NAME_REGEX = /def\s(self\.(\w+)|\w+).*/
6
9
 
7
10
  def initialize name, line
8
- @name = NameExtractor.new( name ).name
11
+ @name = NameExtractor.new( name ).final_name
9
12
  @line = line
10
13
  create_spec_name
11
14
  end
12
15
 
13
16
  def mode
14
- is_spec unless @mode
17
+ set_mode unless @mode
15
18
  @mode
16
19
  end
17
20
 
18
21
  def method_name
19
- return nil if mode == SPEC_MODE
22
+ return nil if spec?
20
23
  @match = get_method_match
21
24
  return nil unless @match
22
- @match.match(/def\s(self\.(\w+)|\w+).*/)[1]
25
+ @match.match(METHOD_NAME_REGEX)[1]
23
26
  end
24
27
 
25
28
  def spec?
26
29
  mode == SPEC_MODE
27
30
  end
28
31
 
29
- def is_spec
30
- if @spec_name[0] == SPEC_MODE
31
- @mode = SPEC_MODE
32
- return true
32
+ def create_spec_name
33
+ @spec_name = name.split(File::SEPARATOR)
34
+ @spec_name = if spec?
35
+ name
36
+ else
37
+ convert_code_to_spec name.split(File::SEPARATOR)
33
38
  end
34
- @mode = CODE_MODE
35
- return false
36
39
  end
37
40
 
38
- def create_spec_name
39
- @spec_name = @name.split('/')
40
- if is_spec
41
- return @spec_name = @name
42
- end
43
- @spec_name[0] = SPEC_MODE
44
- @spec_name[-1].sub!('.rb', '_spec.rb')
45
- @spec_name = @spec_name.join('/')
41
+ def convert_code_to_spec name
42
+ name[0] = SPEC_MODE
43
+ name.last.sub!(RUBY_FILETYPE, SPEC_FILE_ENDING)
44
+ name.join File::SEPARATOR
46
45
  end
47
46
 
48
47
  def get_method_match
49
- File.readlines(name).first(line).reverse.grep(/\sdef\s/).first
48
+ File.readlines(name).first(line).reverse.grep(METHOD_NAME_REGEX).first
50
49
  rescue LoadError
51
50
  return nil
52
51
  end
52
+
53
+ private
54
+
55
+ def set_mode
56
+ @mode = if spec_name.first == SPEC_MODE
57
+ SPEC_MODE
58
+ else
59
+ CODE_MODE
60
+ end
61
+ end
53
62
  end
54
63
  end
@@ -1,18 +1,33 @@
1
1
  module MethodMatch
2
2
  class NameExtractor
3
3
 
4
+ attr_accessor :name
5
+
6
+ PWD = "pwd"
7
+
4
8
  def initialize name
5
9
  @name = name
6
10
  end
7
11
 
8
- def name
9
- pw = pwd.chomp
10
- return @name.sub "#{pw}/", '' if @name.index( pw ) == 0
11
- @name
12
+ def final_name
13
+ @name.slice! base_directory if project_path?
14
+ name
15
+ end
16
+
17
+ def project_path?
18
+ name.index( full_path ) && name.index( full_path ).zero?
12
19
  end
13
20
 
14
21
  def pwd
15
- `pwd`
22
+ `#{PWD}`
23
+ end
24
+
25
+ def full_path
26
+ pwd.chomp
27
+ end
28
+
29
+ def base_directory
30
+ full_path + File::SEPARATOR
16
31
  end
17
32
  end
18
33
  end
@@ -1,17 +1,28 @@
1
1
  module MethodMatch
2
2
  class Pry
3
- def self.setup
4
- require 'pry'
5
- ::Pry::CommandSet.new do
6
- create_command "rspec_method", "runs " do
7
- group "Testing"
8
- def process(*args)
9
- CommandRunner.new(
10
- Matcher.new(args[0], Integer(args[1]))
11
- ).run_command
12
- end
3
+ PRY_PACKAGE = 'pry'
4
+ COMMAND_NAME = "rspec_method"
5
+ COMMAND_DESCRIPTION = "runs spec given name and line number"
6
+
7
+ def rspec_method
8
+ Proc.new do
9
+ create_command COMMAND_NAME, COMMAND_DESCRIPTION, &MethodMatch::Pry.new.command_proc
10
+ end
11
+ end
12
+
13
+ def command_proc
14
+ Proc.new do
15
+ def process(*args)
16
+ CommandRunner.new(
17
+ Matcher.new(args[0], Integer(args[1]))
18
+ ).run_command
13
19
  end
14
- end.tap { |cmd| ::Pry::Commands.import cmd }
20
+ end
21
+ end
22
+
23
+ def setup
24
+ require PRY_PACKAGE
25
+ ::Pry::CommandSet.new(&rspec_method).tap { |cmd| ::Pry::Commands.import cmd }
15
26
  end
16
27
  end
17
28
  end
@@ -1,3 +1,3 @@
1
1
  module MethodMatch
2
- VERSION = "0.0.10".freeze
2
+ VERSION = "0.0.11".freeze
3
3
  end
@@ -1,4 +1,5 @@
1
- require File.join(File.dirname(__FILE__), %w[.. spec_helper])
1
+ require 'spec_helper'
2
+
2
3
  describe MethodMatch::CommandRunner do
3
4
  let(:matcher){MethodMatch::Matcher.new 'data/test_data/matcher.rb', 13}
4
5
  let(:command_runner){MethodMatch::CommandRunner.new matcher}
@@ -11,4 +12,13 @@ describe MethodMatch::CommandRunner do
11
12
  it { should eq('rspec spec/test_data/matcher_spec.rb -e \'.some_class_method\'')}
12
13
  end
13
14
  end
15
+
16
+ describe '#run_command' do
17
+ it 'requires pathname' do
18
+ require 'pathname'
19
+ command_runner.should_receive(:require).with('pathname')
20
+ command_runner.should_receive(:puts).with('Spec spec/test_data/matcher_spec.rb does not exist')
21
+ expect(command_runner.run_command).to be_false
22
+ end
23
+ end
14
24
  end
@@ -1,4 +1,5 @@
1
- require File.join(File.dirname(__FILE__), %w[.. spec_helper])
1
+ require 'spec_helper'
2
+
2
3
  describe MethodMatch::Matcher do
3
4
  let(:matcher){MethodMatch::Matcher.new 'data/test_data/matcher.rb', 13}
4
5
  subject {matcher}
@@ -1,4 +1,5 @@
1
- require File.join(File.dirname(__FILE__), %w[.. spec_helper])
1
+ require 'spec_helper'
2
+
2
3
  describe MethodMatch::NameExtractor do
3
4
  let(:extractor) do
4
5
  e = MethodMatch::NameExtractor.new filename
@@ -6,16 +7,84 @@ describe MethodMatch::NameExtractor do
6
7
  e
7
8
  end
8
9
 
9
- describe '#name' do
10
- subject { extractor.name }
10
+ subject { MethodMatch::NameExtractor.new filename }
11
+
12
+ describe "#pwd" do
13
+ let(:filename) { '/home/othername/project/file.rb'}
14
+ it "executes pwd" do
15
+ subject.should_receive(:`).with 'pwd'
16
+ subject.pwd
17
+ end
18
+ end
19
+
20
+ describe '#final_name' do
21
+ subject { extractor.final_name }
11
22
  context 'project dir' do
12
23
  let(:filename) { '/home/name/project/file.rb'}
13
24
  it { should eq( 'file.rb')}
14
25
  end
15
26
 
16
- context 'somwhere else' do
27
+ context 'somewhere else' do
17
28
  let(:filename) { '/home/othername/project/file.rb'}
18
29
  it { should eq( '/home/othername/project/file.rb')}
19
30
  end
20
31
  end
32
+
33
+ describe "#full_path" do
34
+ let(:extractor) do
35
+ e = MethodMatch::NameExtractor.new ''
36
+ e.stub(:pwd) { "/home/name/project\n"}
37
+ e
38
+ end
39
+
40
+ subject { extractor.full_path }
41
+
42
+ it "cleans line breaks of pwd" do
43
+ expect(subject).to eq('/home/name/project')
44
+
45
+ end
46
+ end
47
+
48
+ describe "#project_path?" do
49
+ subject { extractor.project_path? }
50
+
51
+ context "matching" do
52
+ let(:extractor) do
53
+ e = MethodMatch::NameExtractor.new ''
54
+ e.stub(:full_path) { "a" }
55
+ e.stub(:name) { "aaa" }
56
+ e
57
+ end
58
+
59
+ it "cleans line breaks of pwd" do
60
+ expect(subject).to be_true
61
+ end
62
+ end
63
+
64
+ context "not matching" do
65
+ let(:extractor) do
66
+ e = MethodMatch::NameExtractor.new ''
67
+ e.stub(:full_path) { "a" }
68
+ e.stub(:name) { "bbb" }
69
+ e
70
+ end
71
+
72
+ it "cleans line breaks of pwd" do
73
+ expect(subject).to be_false
74
+ end
75
+ end
76
+
77
+ context "not on start" do
78
+ let(:extractor) do
79
+ e = MethodMatch::NameExtractor.new ''
80
+ e.stub(:full_path) { "a" }
81
+ e.stub(:name) { "bba" }
82
+ e
83
+ end
84
+
85
+ it "cleans line breaks of pwd" do
86
+ expect(subject).to be_false
87
+ end
88
+ end
89
+ end
21
90
  end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe MethodMatch::Pry do
4
+ subject { MethodMatch::Pry.new }
5
+ describe "#setup" do
6
+
7
+ end
8
+
9
+ describe '#rspec_method' do
10
+ it "returns proc creating rspec_method command" do
11
+ expect(subject.rspec_method).to be_instance_of Proc
12
+ expect(subject.rspec_method.lambda?).to be_false
13
+ end
14
+ end
15
+
16
+ describe '#command_proc' do
17
+ it "returns proc passed to create_command" do
18
+ expect(subject.command_proc).to be_instance_of Proc
19
+ expect(subject.command_proc.lambda?).to be_false
20
+ end
21
+ end
22
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: method_match
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomasz Tokarski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-14 00:00:00.000000000 Z
11
+ date: 2014-01-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -91,6 +91,7 @@ files:
91
91
  - spec/method_match/command_runner_spec.rb
92
92
  - spec/method_match/matcher_spec.rb
93
93
  - spec/method_match/name_extractor_spec.rb
94
+ - spec/method_match/pry_spec.rb
94
95
  - spec/spec_helper.rb
95
96
  homepage: http://tomasztokarski.com/method_match
96
97
  licenses: []
@@ -120,4 +121,5 @@ test_files:
120
121
  - spec/method_match/command_runner_spec.rb
121
122
  - spec/method_match/matcher_spec.rb
122
123
  - spec/method_match/name_extractor_spec.rb
124
+ - spec/method_match/pry_spec.rb
123
125
  - spec/spec_helper.rb