temp_editor 0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in temp_editor.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 riverpuro
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.
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # TempEditor
2
+
3
+ Edit temporary file with ENV['EDITOR'].
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'temp_editor'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install temp_editor
18
+
19
+ ## Usage
20
+
21
+ 1. Initialize TempEditor with Tempfile constructor arguments.
22
+ 2. Register callback: before editing, after editing or both.
23
+ 3. Make enduser edit tempfile.
24
+
25
+ ```ruby
26
+ # Initialize TempEditor
27
+ editor = TempEditor.new(['prefix', '.extension'])
28
+
29
+ # Resister before editing callback:
30
+ editor.before do |tempfile|
31
+ tempfile.write("EDIT ME")
32
+ end
33
+
34
+ # Resister after editing callback:
35
+ editor.after do |tempfile|
36
+ puts tempfile.read
37
+ end
38
+
39
+ # launch editor
40
+ editor.edit
41
+ ```
42
+
43
+ See also: examples
44
+
45
+ ## Contributing
46
+
47
+ 1. Fork it
48
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
49
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
50
+ 4. Push to the branch (`git push origin my-new-feature`)
51
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task :default => :spec
@@ -0,0 +1,46 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
4
+ require 'temp_editor'
5
+
6
+ class RemoteResource
7
+ def initialize(resource_id)
8
+ @path = "http://example.com/diary/#{resource_id}"
9
+ end
10
+
11
+ def get
12
+ "# contents for #{@path}\n"
13
+ end
14
+
15
+ def put(content)
16
+ puts "update #{@path}"
17
+ puts "=" * 80
18
+ puts content
19
+ end
20
+ end
21
+
22
+ class RemoteEditor
23
+ def initialize(remote_resource_id)
24
+ @remote_resource = RemoteResource.new(remote_resource_id)
25
+
26
+ @editor = TempEditor.new(['remote', '.markdown'])
27
+
28
+ @editor.before do |file|
29
+ content = @remote_resource.get
30
+ file.write(content)
31
+ end
32
+
33
+ @editor.after do |file|
34
+ @remote_resource.put(file.read)
35
+ end
36
+ end
37
+
38
+ def edit
39
+ @editor.edit
40
+ end
41
+ end
42
+
43
+ if __FILE__ == $0
44
+ remote_editor = RemoteEditor.new(1)
45
+ remote_editor.edit
46
+ end
@@ -0,0 +1,49 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'tempfile'
3
+
4
+ class TempEditor
5
+ VERSION = '0.0.1'
6
+
7
+ class EditorConfigureError < StandardError; end
8
+
9
+ attr_reader :tempfile
10
+
11
+ def initialize(basename, *rest)
12
+ @tempfile = Tempfile.new(basename, *rest)
13
+ end
14
+
15
+ def before(&block)
16
+ @before_callback = build_callback(&block)
17
+ end
18
+
19
+ def after(&block)
20
+ @after_callback = build_callback(&block)
21
+ end
22
+
23
+ def edit
24
+ @before_callback.call if @before_callback
25
+ system "#{editor} #{@tempfile.path}"
26
+ @after_callback.call if @after_callback
27
+ end
28
+
29
+ def editor
30
+ @editor ||= ENV['EDITOR']
31
+ @editor or raise EditorConfigureError, "set your ENV['EDITOR']"
32
+ end
33
+
34
+ def editor=(editor_cmd)
35
+ @editor = editor_cmd
36
+ end
37
+
38
+ private
39
+ def build_callback(&block)
40
+ Proc.new do
41
+ begin
42
+ @tempfile.open
43
+ block.call(@tempfile)
44
+ ensure
45
+ @tempfile.close
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,16 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
4
+ require 'temp_editor'
5
+
6
+ RSpec.configure do |config|
7
+ config.treat_symbols_as_metadata_keys_with_true_values = true
8
+ config.run_all_when_everything_filtered = true
9
+ config.filter_run :focus
10
+
11
+ # Run specs in random order to surface order dependencies. If you find an
12
+ # order dependency and want to debug it, you can fix the order by providing
13
+ # the seed, which is printed after each run.
14
+ # --seed 1234
15
+ config.order = 'random'
16
+ end
@@ -0,0 +1,141 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ describe TempEditor do
5
+ shared_context "setup common @temp_editor instance" do
6
+ before do
7
+ @temp_editor = TempEditor.new('tempfile_prefix')
8
+
9
+ @editor_inputs = 'my editor inputs'
10
+
11
+ mock_editor_command = "echo '#{@editor_inputs}' >"
12
+ @temp_editor.editor = mock_editor_command
13
+ end
14
+ end
15
+
16
+
17
+ describe "#edit" do
18
+ include_context "setup common @temp_editor instance"
19
+
20
+ it "should exec editor" do
21
+ begin
22
+ @temp_editor.edit
23
+
24
+ @temp_editor.tempfile.open
25
+ tempfile_contents = @temp_editor.tempfile.read.chomp
26
+ tempfile_contents.should == @editor_inputs
27
+ ensure
28
+ @temp_editor.tempfile.close
29
+ end
30
+ end
31
+ end
32
+
33
+
34
+ describe "#before" do
35
+ include_context "setup common @temp_editor instance"
36
+
37
+ it "should be called with opened tempfile" do
38
+ @temp_editor.before do |file|
39
+ file.should_not be_closed
40
+ end
41
+
42
+ @temp_editor.edit
43
+ end
44
+
45
+ it "should be called before edit" do
46
+ @temp_editor.before do |file|
47
+ file_contents = file.read
48
+ file_contents.should be_empty
49
+ end
50
+
51
+ @temp_editor.edit
52
+ end
53
+ end
54
+
55
+
56
+ describe "#after" do
57
+ include_context "setup common @temp_editor instance"
58
+
59
+ it "should be called with opened tempfile" do
60
+ @temp_editor.after do |file|
61
+ file.should_not be_closed
62
+ end
63
+
64
+ @temp_editor.edit
65
+ end
66
+
67
+ it "should be called after edit" do
68
+ @temp_editor.after do |file|
69
+ file_contents = file.read.chomp
70
+ file_contents.should == @editor_inputs
71
+ end
72
+
73
+ @temp_editor.edit
74
+ end
75
+ end
76
+
77
+
78
+ describe "#editor", "#editor=" do
79
+ context "specify editor command" do
80
+ before do
81
+ @temp_editor = TempEditor.new('tempfile_prefix')
82
+
83
+ @specified_editor_command_inputs = "specified editor inputs"
84
+
85
+ @temp_editor.editor = "echo '#{@specified_editor_command_inputs}' >"
86
+ end
87
+
88
+ it "should use specified editor command" do
89
+ begin
90
+ @temp_editor.edit
91
+
92
+ @temp_editor.tempfile.open
93
+ tempfile_contents = @temp_editor.tempfile.read.chomp
94
+ tempfile_contents.should == @specified_editor_command_inputs
95
+ ensure
96
+ @temp_editor.tempfile.close
97
+ end
98
+ end
99
+ end
100
+
101
+ context "not specify editor command" do
102
+ before do
103
+ @temp_editor = TempEditor.new('tempfile_prefix')
104
+ end
105
+
106
+ it "should use ENV['EDITOR'] as default" do
107
+ begin
108
+ orignal_env_editor = ENV['EDITOR']
109
+ editor_inputs = "original editor inputs"
110
+ ENV['EDITOR'] = "echo '#{editor_inputs}' > "
111
+
112
+ @temp_editor.edit
113
+
114
+ @temp_editor.tempfile.open
115
+ tempfile_contents = @temp_editor.tempfile.read.chomp
116
+ tempfile_contents.should == editor_inputs
117
+ ensure
118
+ ENV['EDITOR'] = orignal_env_editor
119
+ @temp_editor.tempfile.close
120
+ end
121
+ end
122
+ end
123
+
124
+ context "when blank ENV['EDITOR']" do
125
+ before do
126
+ @temp_editor = TempEditor.new('tempfile_prefix')
127
+ end
128
+
129
+ it "should use ENV['EDITOR'] as default" do
130
+ begin
131
+ orignal_env_editor = ENV.delete('EDITOR')
132
+ expect {
133
+ @temp_editor.edit
134
+ }.to raise_error(TempEditor::EditorConfigureError)
135
+ ensure
136
+ ENV['EDITOR'] = orignal_env_editor
137
+ end
138
+ end
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'temp_editor'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "temp_editor"
8
+ gem.version = TempEditor::VERSION
9
+ gem.authors = ["riverpuro"]
10
+ gem.email = ["riverpuro@gmail.com"]
11
+ gem.description = %q{Edit temporary file with ENV['EDITOR']}
12
+ gem.summary = %q{Edit temporary file with ENV['EDITOR']}
13
+ gem.homepage = "https://github.com/riverpuro/temp_editor"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_development_dependency('rspec')
21
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: temp_editor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - riverpuro
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: Edit temporary file with ENV['EDITOR']
31
+ email:
32
+ - riverpuro@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - .rspec
39
+ - Gemfile
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - examples/remote_editor.rb
44
+ - lib/temp_editor.rb
45
+ - spec/spec_helper.rb
46
+ - spec/temp_editor_spec.rb
47
+ - temp_editor.gemspec
48
+ homepage: https://github.com/riverpuro/temp_editor
49
+ licenses: []
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 1.8.24
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: Edit temporary file with ENV['EDITOR']
72
+ test_files:
73
+ - spec/spec_helper.rb
74
+ - spec/temp_editor_spec.rb