copier 1.1 → 1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +34 -4
  2. data/lib/copier.rb +58 -0
  3. data/spec/copier_spec.rb +49 -13
  4. metadata +2 -2
data/README.md CHANGED
@@ -4,21 +4,51 @@
4
4
 
5
5
  ## Usage
6
6
 
7
+ Usage 1: as a command
8
+
9
+ $ copier 'hello'
10
+ $ echo 'hello' | copier
11
+
12
+ Usage 2: as a library
13
+
7
14
  require 'copier'
8
15
  Copier('text')
9
16
 
10
17
  run the code and then you can paste `text` by Cmd-v on Mac OS X, Ctrl-v on Windows, and as well on other platforms.
11
18
 
19
+ there's no paster command or method intentionally.
20
+
12
21
  ## Platform
13
22
 
14
23
  This library supports the following platforms. I'm always welcome patches for supporting other platforms, including spec.
15
24
 
16
25
  * Mac OS X
17
26
  * Windows (cygwin)
27
+ * Linux
28
+
29
+ ## Licence
30
+
31
+ (The MIT License)
32
+
33
+ Copyright (c) 2010 - 2010:
34
+
35
+ * Tatsuhiro Ujihisa <http://ujihisa.blogspot.com>
18
36
 
19
- ## development
37
+ Permission is hereby granted, free of charge, to any person obtaining
38
+ a copy of this software and associated documentation files (the
39
+ 'Software'), to deal in the Software without restriction, including
40
+ without limitation the rights to use, copy, modify, merge, publish,
41
+ distribute, sublicense, and/or sell copies of the Software, and to
42
+ permit persons to whom the Software is furnished to do so, subject to
43
+ the following conditions:
20
44
 
21
- issues
45
+ The above copyright notice and this permission notice shall be
46
+ included in all copies or substantial portions of the Software.
22
47
 
23
- * no GNOME support yet
24
- * no KDE support yet
48
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
49
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
50
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
51
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
52
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
53
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
54
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/lib/copier.rb CHANGED
@@ -1,13 +1,71 @@
1
1
  module Copier
2
2
  class NotSupported < StandardError; end
3
+
4
+ class Config < Hash
5
+ def method_missing(name,*args)
6
+ if /=$/ =~ name.to_s
7
+ self[name.to_s.gsub(/=$/,"").to_sym] = args[0]
8
+ else
9
+ self[name]
10
+ end
11
+ end
12
+ end
13
+
14
+ class << self
15
+ def config_prepare
16
+ @config = Config.new()
17
+ end
18
+
19
+ def load_config(f = '~/.copier')
20
+ fn = File.expand_path(f)
21
+ self.config_prepare
22
+ config = @config
23
+ eval(File.read(fn)) rescue nil
24
+ @config = config.dup
25
+ @config_loaded = true
26
+ end
27
+
28
+ def method_missing(name, *args)
29
+ @config.__send__(name, *args)
30
+ end
31
+
32
+ def disable_config_file=(a)
33
+ @disable_config_file = a
34
+ if a
35
+ self.config_prepare
36
+ @config_loaded = false
37
+ end
38
+ end
39
+
40
+ def disable_config_file
41
+ @disable_config_file
42
+ end
43
+
44
+ def config_loaded
45
+ @config_loaded
46
+ end
47
+ end
3
48
  end
4
49
 
5
50
  def Copier(text)
51
+ Copier.load_config unless Copier.config_loaded || Copier.disable_config_file
52
+
53
+ if Copier.copy_method
54
+ Copier.copy_method.call(text)
55
+ return
56
+ end
57
+
58
+ if Copier.copy_file
59
+ File.open(Copier.copy_file, 'w') {|io| io.write text }
60
+ end
61
+
6
62
  case RUBY_PLATFORM
7
63
  when /darwin/
8
64
  IO.popen('pbcopy', 'w') {|io| io.write text }
9
65
  when /cygwin/
10
66
  File.open('/dev/clipboard', 'wb') {|io| io.write text.gsub("\x0A", "\n") }
67
+ when /linux/
68
+ IO.popen('xclip -selection clipboard', 'w') {|io| io.write text}
11
69
  else
12
70
  raise Copier::NotSupported, RUBY_PLATFORM + " is not supported yet by Copier."
13
71
  end
data/spec/copier_spec.rb CHANGED
@@ -5,22 +5,58 @@ unless respond_to? :require_relative
5
5
  end
6
6
 
7
7
  require_relative '../lib/copier'
8
+ require 'tempfile'
9
+ Copier.config_prepare
8
10
 
9
11
  describe 'Copier()' do
10
- it 'copies text on Mac OS X' do
11
- pending 'This platform is not Mac OS X, so you cannot run this case now' unless
12
- /darwin/ =~ RUBY_PLATFORM
13
- a = rand.to_s
14
- Copier(a)
15
- `pbpaste`.chomp.should == a
12
+ describe 'in default case' do
13
+ before do
14
+ Copier.disable_config_file = true
15
+ end
16
+
17
+ it 'copies text on Mac OS X' do
18
+ pending 'This platform is not Mac OS X, so you cannot run this case now' unless
19
+ /darwin/ =~ RUBY_PLATFORM
20
+ a = rand.to_s
21
+ Copier(a)
22
+ `pbpaste`.chomp.should == a
23
+ end
24
+
25
+ it 'copies text on Windows (cygwin)' do
26
+ pending 'This platform is not Windows (cygwin), so you cannot run this case now' unless
27
+ /cygwin/ =~ RUBY_PLATFORM
28
+ a = rand.to_s
29
+ Copier(a)
30
+ #`pbpaste`.chomp.should == a
31
+ pending 'Please fill this case... I dont know how to paste text'
32
+ end
33
+
34
+ it 'uses xclip on linux' do
35
+ pending 'This platform is not Linux, so you cannot run this case now' unless
36
+ /linux/ =~ RUBY_PLATFORM
37
+ a = rand.to_s
38
+ Copier(a)
39
+ `xclip -selection clipboard -o`.chomp.should == a
40
+ end
16
41
  end
17
42
 
18
- it 'copies text on Windows (cygwin)' do
19
- pending 'This platform is not Windows (cygwin), so you cannot run this case now' unless
20
- /cygwin/ =~ RUBY_PLATFORM
21
- a = rand.to_s
22
- Copier(a)
23
- #`pbpaste`.chomp.should == a
24
- pending 'Please fill this case... I dont know how to paste text'
43
+ describe 'with config' do
44
+ before do
45
+ Copier.disable_config_file = true
46
+ Copier.config_prepare
47
+ end
48
+
49
+ it '.copy_method can copy by block' do
50
+ Copier.copy_method = lambda do |a|
51
+ a.should == "Copier will be used over ssh"
52
+ end
53
+ Copier("Copier will be used over ssh")
54
+ end
55
+
56
+ it '.copy_file can copy to file' do
57
+ Copier.copy_file = Tempfile.new('copier_spec').path
58
+ Copier("fork it")
59
+ File.read(Copier.copy_file).should == "fork it"
60
+ end
25
61
  end
26
62
  end
metadata CHANGED
@@ -4,8 +4,8 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 1
7
- - 1
8
- version: "1.1"
7
+ - 2
8
+ version: "1.2"
9
9
  platform: ruby
10
10
  authors:
11
11
  - ujihisa