tryrb 1.0.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a7252d4539cd37e1802b97cef43868e1c2c4ec3d
4
- data.tar.gz: 86c19f2d675deb2b3b29f0edcaf5815a143286ec
3
+ metadata.gz: 5938ffd17f0b2e187e365bf48e959c3113189c82
4
+ data.tar.gz: a00e195b696b75dc49183127cea6a3c4f534dbe4
5
5
  SHA512:
6
- metadata.gz: b19eb84f36fd761916f048f60e75a3a5e6e971475db75b17919226e5a63e54b36e795f3615d6420ef77a4a4d16df76c2292c3042399dbd123540b7da0a5cba16
7
- data.tar.gz: 3f7dcfd287acd5b637498b0a97b488588dbe1e5768852d404ff8c61ef303e7112d997378bbcde8837651226bb5a88988531610bc17abe2ee50a052353a2a1dce
6
+ metadata.gz: 0fd3a62db0ff9417e7241d7ce645300f7bb7e13698ca49a10095c93268d8a627d850e70bc2038f6a1094ea4738cc3206b0aacba690fa55d24c02720e55e1d4c0
7
+ data.tar.gz: 88a942b594d4a84202d5b8e47ad6e48075874024c73656e3403b58a19929e4da41bc04faa1e521e07a13ac06c2e8ca675971eaaf6cb7a7672ef7eba73fc15d78
@@ -1,18 +1,45 @@
1
- require 'tryrb/config'
2
1
  require 'fileutils'
2
+ require 'thor'
3
+ require 'tryrb/config'
3
4
 
4
5
  module TryRb
5
- class CLI
6
- @config = TryRb::Config.instance
7
- class << self
8
- attr_writer :config
9
- def start(args)
10
- system([@config.editor, fullpath(args[0])] * ' ')
6
+ class CLI < Thor
7
+ def initialize(*)
8
+ @config = TryRb::Config.instance
9
+ super
10
+ end
11
+
12
+ desc "create [FILENAME]", 'create a file to edit (short-cut alias: "c")'
13
+ def create(key_name="")
14
+ @key_name = key_name
15
+ system([@config.editor, fullpath] * ' ')
16
+ end
17
+
18
+ map 'c' => :create
19
+
20
+ option :last, aliases: "-l", banner: 'N', desc: 'open the last N file in the files you specify via filename, default is the 1', type: :numeric, lazy_default: 1
21
+ desc "exec [FILENAME]", 'open a file to edit, default is last one of all files (short-cut alias: "e")'
22
+ def exec(filename=nil)
23
+ file_path = find_file(filename: filename, last_n: options[:last])
24
+ abort "Can't find the file you want" unless file_path
25
+ system(['ruby', file_path] * ' ')
26
+ end
27
+
28
+ map 'e' => :exec
29
+
30
+ private
31
+
32
+ def find_file(options={})
33
+ filename = options[:filename]
34
+ last_n = options[:last_n] || 1
35
+ names = Dir[File.join(@config.expanded_tmp_dir, '*.rb')]
36
+ names = names.select { |n| n =~ /#{filename}\.rb$/ } if filename
37
+ names[-last_n]
11
38
  end
12
39
 
13
- def fullpath(filename)
14
- filename = get_filename(filename)
15
- expanded_path = File.expand_path(@config.tmp_dir)
40
+ def fullpath
41
+ filename = get_filename
42
+ expanded_path = @config.expanded_tmp_dir
16
43
  FileUtils.mkdir_p expanded_path unless Dir.exists?(expanded_path)
17
44
  File.join @config.tmp_dir, filename
18
45
  rescue Errno::EEXIST => e
@@ -27,11 +54,10 @@ module TryRb
27
54
  '.rb'
28
55
  end
29
56
 
30
- def get_filename(name)
57
+ def get_filename
31
58
  parts = [filename_prefix]
32
- parts << name.gsub(/\.rb$/, '') if name
59
+ parts << @key_name.gsub(/\.rb$/, '') unless @key_name.empty?
33
60
  parts * '_' + filename_extname
34
61
  end
35
- end
36
62
  end
37
63
  end
@@ -30,6 +30,10 @@ module TryRb
30
30
  default_config
31
31
  end
32
32
 
33
+ def expanded_tmp_dir
34
+ File.expand_path(tmp_dir)
35
+ end
36
+
33
37
  private
34
38
 
35
39
  def default_config
@@ -1,8 +1,8 @@
1
1
  module TryRb
2
2
  class Version
3
3
  MAJOR = 1
4
- MINOR = 0
5
- PATCH = 2
4
+ MINOR = 1
5
+ PATCH = 0
6
6
  PRE = nil
7
7
 
8
8
  class << self
@@ -3,80 +3,55 @@ require 'helper'
3
3
  describe TryRb::CLI do
4
4
  before :each do
5
5
  Time.stub(:now) { Date.parse('2014-1-1').to_time }
6
+ TryRb::Config.instance.path = fixture_file_path('tryrbrc')
7
+ @cli = TryRb::CLI.new
6
8
  end
7
- describe '::start' do
8
- it "should call system method with an editor" do
9
- TryRb::Config.instance.path = fixture_file_path('tryrbrc')
10
- expect(TryRb::CLI).to receive(:system).with('emacs ~/tmp/foo/tryrb/20140101000000_filename.rb')
11
- TryRb::CLI.start(["filename"])
12
- end
13
9
 
14
- it "should call system method with right filepath and default editor" do
15
- TryRb::Config.instance.path = fixture_file_path('foorc')
16
- expect(TryRb::CLI).to receive(:system).with('mvim ~/tmp/tryrb/20140101000000_filename.rb')
17
- TryRb::CLI.start(["filename"])
10
+ describe '#create' do
11
+ it 'create a file 201401010000.rb' do
12
+ expect(@cli).to receive(:system).with('emacs ~/tmp/foo/tryrb/20140101000000.rb')
13
+ @cli.create
14
+ end
15
+ it 'create a file 20140101000000_foo.rb' do
16
+ expect(@cli).to receive(:system).with('emacs ~/tmp/foo/tryrb/20140101000000_foo.rb')
17
+ @cli.create 'foo'
18
18
  end
19
19
  end
20
20
 
21
- describe '::fullpath' do
21
+ describe '#exec' do
22
22
  before :each do
23
- TryRb::Config.instance.path = fixture_file_path('tryrbrc')
24
- FileUtils.mkdir project_path + '/tmp' unless Dir.exists?(project_path + '/tmp')
23
+ FileUtils.mkdir_p 'tmp/foo/tryrb'
24
+ FileUtils.touch 'tmp/foo/tryrb/20140101000000_foo.rb'
25
+ FileUtils.touch 'tmp/foo/tryrb/20140102000000_foo.rb'
26
+ FileUtils.touch 'tmp/foo/tryrb/20140103000000_bar.rb'
27
+ expect(TryRb::Config.instance).to receive(:expanded_tmp_dir).and_return('tmp/foo/tryrb')
25
28
  end
26
29
  after :each do
27
- FileUtils.rmdir project_path + '/tmp'
28
- end
29
- it 'return fullpath of the file' do
30
- expect(TryRb::CLI.fullpath('bar')).to eq('~/tmp/foo/tryrb/20140101000000_bar.rb')
31
- end
32
- it 'return right filename with a file *.rb' do
33
- expect(TryRb::CLI.fullpath('bar.rb')).to eq('~/tmp/foo/tryrb/20140101000000_bar.rb')
34
- end
35
-
36
- context 'when the Dir does not exists' do
37
- it 'create a dir' do
38
- Dir.stub(:exists?) { false }
39
- TryRb::Config.instance.stub(:tmp_dir) { project_path + '/tmp/tryrb' }
40
- expect(FileUtils).to receive(:mkdir_p).with(project_path + '/tmp/tryrb')
41
- TryRb::CLI.fullpath('bar')
42
- end
43
- end
44
-
45
- context 'when the Dir exists' do
46
- it 'does not create a dir' do
47
- Dir.stub(:exists?) { true }
48
- TryRb::Config.instance.stub(:tmp_dir) { project_path + '/tmp/tryrb' }
49
- expect(FileUtils).to_not receive(:mkdir_p).with(project_path + '/tmp/tryrb')
50
- TryRb::CLI.fullpath('bar')
51
- end
52
- end
53
-
54
- context 'when there is a file which has same name with dir' do
55
- it 'aborts' do
56
- TryRb::Config.instance.stub(:tmp_dir) { project_path + '/tmp/foo/tryrb' }
57
- FileUtils.touch project_path + '/tmp/foo'
58
- expect(TryRb::CLI).to receive(:abort).with(/File .+ exists. The tmp directory can't be created! Please delete the file first./)
59
- TryRb::CLI.fullpath('bar')
60
- end
61
- end
62
- end
63
-
64
- describe '#filename_prefix' do
65
- it "return now string" do
66
- expect(TryRb::CLI.filename_prefix).to eq("20140101000000")
67
- end
68
- end
69
-
70
- describe '#get_filename' do
71
- context 'when name is given' do
72
- it 'returns string containing the name' do
73
- expect(TryRb::CLI.get_filename('bar')).to eq('20140101000000_bar.rb')
74
- end
75
- end
76
- context 'when name is not given' do
77
- it 'returns just time string' do
78
- expect(TryRb::CLI.get_filename(nil)).to eq('20140101000000.rb')
79
- end
30
+ FileUtils.rm_rf 'tmp/foo'
31
+ end
32
+ it 'exec last file' do
33
+ expect(@cli).to receive(:system).with('ruby tmp/foo/tryrb/20140103000000_bar.rb')
34
+ @cli.exec
35
+ end
36
+ it 'exec last file with filename' do
37
+ expect(@cli).to receive(:system).with('ruby tmp/foo/tryrb/20140102000000_foo.rb')
38
+ @cli.exec 'foo'
39
+ end
40
+ it 'exec last second file' do
41
+ expect(@cli).to receive(:options).and_return({last: 2})
42
+ expect(@cli).to receive(:system).with('ruby tmp/foo/tryrb/20140102000000_foo.rb')
43
+ @cli.exec
44
+ end
45
+ it 'exec last second file with filename' do
46
+ expect(@cli).to receive(:options).and_return({last: 2})
47
+ expect(@cli).to receive(:system).with('ruby tmp/foo/tryrb/20140101000000_foo.rb')
48
+ @cli.exec 'foo'
49
+ end
50
+ it 'abort because there is no file' do
51
+ @cli.stub(:system)
52
+ expect(@cli).to receive(:options).and_return({last: 4})
53
+ expect(@cli).to receive(:abort).with("Can't find the file you want")
54
+ @cli.exec
80
55
  end
81
56
  end
82
57
  end
@@ -16,4 +16,5 @@ Gem::Specification.new do |s|
16
16
  s.executables << 'tryrb'
17
17
  s.require_paths = ["lib"]
18
18
  s.homepage = 'https://github.com/tony612/tryrb'
19
+ s.add_dependency 'thor'
19
20
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tryrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tony Han
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-02 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2014-01-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  description: Try ruby code in a temporary file created.
14
28
  email: h.bing612@gmail.com
15
29
  executables: