tryrb 1.0.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 312d018b8d11c953256d4d7635b14d025957f94a
4
+ data.tar.gz: fc8d1b1ce32cdd7892c1bedb5117dd9bbe167f47
5
+ SHA512:
6
+ metadata.gz: 70ee59a3790889c577a75c4e9f294e84abca90f03fbe803f53fbac129058975e5034df4f5e0ebdf8b3f2f5da49b234479dabc93c5d2592dbd93206ec0e9709f8
7
+ data.tar.gz: fbd48602e5d8f3ec9884fc82212be8dfbd164234ddc9777de4c501295f1f63c1bc59824da6c35feb5f3993b5b583493824a583bc920e99a21dfe6ef52fb69424
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Tony Han
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << "spec"
6
+ t.test_files = FileList['spec/*_spec.rb']
7
+ t.verbose = true
8
+ end
9
+
10
+ desc "Run tests"
11
+ task :default => :test
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'tryrb'
4
+
5
+ TryRb::CLI.start(ARGV)
@@ -0,0 +1,4 @@
1
+ require 'tryrb/cli'
2
+
3
+ module TryRb
4
+ end
@@ -0,0 +1,37 @@
1
+ require 'tryrb/config'
2
+ require 'fileutils'
3
+
4
+ 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]))
11
+ end
12
+
13
+ def fullpath(filename)
14
+ filename = get_filename(filename)
15
+ expanded_path = File.expand_path(@config.tmp_dir)
16
+ FileUtils.mkdir_p expanded_path unless Dir.exists?(expanded_path)
17
+ File.join @config.tmp_dir, filename
18
+ rescue Errno::EEXIST => e
19
+ abort "File #{e.message.split[-1]} exists. The tmp directory can't be created! Please delete the file first."
20
+ end
21
+
22
+ def filename_prefix
23
+ Time.now.strftime("%Y%m%d%H%M%S")
24
+ end
25
+
26
+ def filename_extname
27
+ '.rb'
28
+ end
29
+
30
+ def get_filename(name)
31
+ parts = [filename_prefix]
32
+ parts << name.gsub(/\.rb$/, '') if name
33
+ parts * '_' + filename_extname
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,40 @@
1
+ require 'singleton'
2
+
3
+ module TryRb
4
+ class Config
5
+ include Singleton
6
+ attr_reader :path
7
+ FILE_NAME = '.tryrbrc'
8
+
9
+ %w[tmp_dir editor].each do |key|
10
+ define_method key.to_sym do
11
+ @data[key]
12
+ end
13
+ end
14
+
15
+ def initialize
16
+ @path = File.join(File.expand_path('~'), FILE_NAME)
17
+ @data = load_file
18
+ end
19
+
20
+ def path=(path)
21
+ @path = path
22
+ @data = load_file
23
+ @path
24
+ end
25
+
26
+ def load_file
27
+ require 'yaml'
28
+ YAML.load_file(@path)
29
+ rescue Errno::ENOENT
30
+ default_config
31
+ end
32
+
33
+ private
34
+
35
+ def default_config
36
+ {'tmp_dir' => "~/tmp/tryrb",
37
+ 'editor' => 'mvim'}
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,15 @@
1
+ module TryRb
2
+ class Version
3
+ MAJOR = 1
4
+ MINOR = 0
5
+ PATCH = 0
6
+ PRE = nil
7
+
8
+ class << self
9
+ # @return [String]
10
+ def to_s
11
+ [MAJOR, MINOR, PATCH, PRE].compact.join('.')
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,82 @@
1
+ require 'helper'
2
+
3
+ describe TryRb::CLI do
4
+ before :each do
5
+ Time.stub(:now) { Date.parse('2014-1-1').to_time }
6
+ 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
+
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"])
18
+ end
19
+ end
20
+
21
+ describe '::fullpath' do
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')
25
+ end
26
+ 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
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,60 @@
1
+ require 'helper'
2
+
3
+ describe TryRb::Config do
4
+ after :each do
5
+ # TryRb::Config.instance.reset
6
+ end
7
+
8
+ it 'is a singleton' do
9
+ expect(TryRb::Config).to be_a Class
10
+ expect do
11
+ TryRb::Config.new
12
+ end.to raise_error(NoMethodError, /private method `new' called/)
13
+ end
14
+
15
+ describe '#tmp_dir' do
16
+ it "returns tmp dir" do
17
+ config = TryRb::Config.instance
18
+ config.path = fixture_file_path('tryrbrc')
19
+ expect(config.tmp_dir).to eq '~/tmp/foo/tryrb'
20
+ end
21
+ end
22
+
23
+ describe '#editor' do
24
+ it "returns editor" do
25
+ config = TryRb::Config.instance
26
+ config.path = fixture_file_path('tryrbrc')
27
+ expect(config.editor).to eq 'emacs'
28
+ end
29
+ end
30
+
31
+ describe '#path=' do
32
+ it 'overrides path' do
33
+ config = TryRb::Config.instance
34
+ config.path = fixture_file_path('foorc')
35
+ expect(config.path).to eq fixture_file_path('foorc')
36
+ end
37
+ it 'reloads data' do
38
+ config = TryRb::Config.instance
39
+ config.path = fixture_file_path('tryrbrc')
40
+ expect(config.load_file.keys).to eq %w[tmp_dir editor]
41
+ end
42
+ end
43
+
44
+ describe 'load_file' do
45
+ context 'when file exists at path' do
46
+ it 'loads data from the file' do
47
+ config = TryRb::Config.instance
48
+ config.path = fixture_file_path('tryrbrc')
49
+ expect(config.tmp_dir).to eq '~/tmp/foo/tryrb'
50
+ end
51
+ end
52
+ context 'when file does not exist' do
53
+ it 'loads default data' do
54
+ config = TryRb::Config.instance
55
+ config.path = fixture_file_path('foorc')
56
+ expect(config.tmp_dir).to eq '~/tmp/tryrb'
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,3 @@
1
+ ---
2
+ tmp_dir: ~/tmp/foo/tryrb
3
+ editor: emacs
@@ -0,0 +1,20 @@
1
+ require 'tryrb'
2
+ require 'rspec'
3
+
4
+ RSpec.configure do |config|
5
+ config.expect_with :rspec do |c|
6
+ c.syntax = :expect
7
+ end
8
+ end
9
+
10
+ def fixture_path
11
+ File.expand_path('../fixtures', __FILE__)
12
+ end
13
+
14
+ def fixture_file_path(file_name)
15
+ File.join fixture_path, file_name
16
+ end
17
+
18
+ def project_path
19
+ File.expand_path('../..', __FILE__)
20
+ end
@@ -0,0 +1,19 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'tryrb/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'tryrb'
7
+ s.version = TryRb::Version
8
+ s.licenses = %w[MIT]
9
+ s.summary = %q{Try ruby code in a temporary file created.}
10
+ s.description = s.summary
11
+ s.authors = ["Tony Han"]
12
+ s.email = 'h.bing612@gmail.com'
13
+ s.files = %w(LICENSE.md Rakefile tryrb.gemspec)
14
+ s.files += Dir.glob('lib/**/*.rb')
15
+ s.files += Dir.glob('spec/**/*')
16
+ s.executables << 'tryrb'
17
+ s.require_paths = ["lib"]
18
+ s.homepage = 'https://github.com/tony612/tryrb'
19
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tryrb
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Tony Han
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Try ruby code in a temporary file created.
14
+ email: h.bing612@gmail.com
15
+ executables:
16
+ - tryrb
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE.md
21
+ - Rakefile
22
+ - bin/tryrb
23
+ - lib/tryrb.rb
24
+ - lib/tryrb/cli.rb
25
+ - lib/tryrb/config.rb
26
+ - lib/tryrb/version.rb
27
+ - spec/cli_spec.rb
28
+ - spec/config_spec.rb
29
+ - spec/fixtures/tryrbrc
30
+ - spec/helper.rb
31
+ - tryrb.gemspec
32
+ homepage: https://github.com/tony612/tryrb
33
+ licenses:
34
+ - MIT
35
+ metadata: {}
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubyforge_project:
52
+ rubygems_version: 2.2.0.rc.1
53
+ signing_key:
54
+ specification_version: 4
55
+ summary: Try ruby code in a temporary file created.
56
+ test_files: []