undo-manager 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d689b917106bd7744e60bbc2c299545d257f3ee5
4
+ data.tar.gz: 9f07646e5571bc48a5534ea02594ab5e18a42b08
5
+ SHA512:
6
+ metadata.gz: 59b19e312c039de63218569ac570f22f1b9788e87ba3df5b66969a2e0cbf577f6630fc6908cae45923aeea8065a6a4897550293c6b334c3b22764378c54b6374
7
+ data.tar.gz: d4edc564a2d11c0ef31489872a21b95ebdb99b3c0350ed0592f167294b3b07882a6f35109add706a1c9a70c8b8a594f7b85cabbf29665467c72d251aadf1a02b
@@ -0,0 +1,11 @@
1
+ *.gem
2
+ .bundle
3
+ .DS_Store
4
+ .ruby-version
5
+ .ruby-gemset
6
+ pkg/*
7
+ tmp
8
+
9
+ Gemfile.lock
10
+ gemfiles/*.lock
11
+ doc/scratchpad.md
@@ -0,0 +1,3 @@
1
+ ### 0.0.1
2
+
3
+ * Initial release
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ group :development, :test do
6
+ gem 'gem-release'
7
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2015 Jo Hund
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,38 @@
1
+ # undo-manager
2
+
3
+ undo-manager makes it easy to add undo/redo functionality to a Ruby (on Rails) app. It uses the command pattern to do so. undo-manager provides the handling of undo and redo. It’s up to you to implement the `#do` and `#undo` methods for each of your commands.
4
+
5
+
6
+ ## Installation
7
+
8
+ `gem install undo-manager`
9
+
10
+ or with bundler in your Gemfile:
11
+
12
+ `gem 'undo-manager'`
13
+
14
+
15
+ ## Dependencies
16
+
17
+ * Ruby >= 1.9.3
18
+
19
+
20
+ ## Resources
21
+
22
+ * [API documentation](http://www.rubydoc.info/gems/undo-manager/)
23
+ * [Changelog](https://github.com/jhund/undo-manager/blob/master/CHANGELOG.md)
24
+ * [Source code (github)](https://github.com/jhund/undo-manager)
25
+ * [Issues](https://github.com/jhund/undo-manager/issues)
26
+ * [Rubygems.org](http://rubygems.org/gems/undo-manager)
27
+
28
+ [![Build Status](https://travis-ci.org/jhund/undo-manager.svg?branch=master)](https://travis-ci.org/jhund/undo-manager)
29
+
30
+ [![Code Climate](https://codeclimate.com/github/jhund/undo-manager.png)](https://codeclimate.com/github/jhund/undo-manager)
31
+
32
+ ### License
33
+
34
+ [MIT licensed](https://github.com/jhund/undo-manager/blob/master/MIT-LICENSE).
35
+
36
+ ### Copyright
37
+
38
+ Copyright (c) 2015 Jo Hund. See [(MIT) LICENSE](https://github.com/jhund/undo-manager/blob/master/MIT-LICENSE) for details.
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+ begin
3
+ require 'bundler'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ Bundler::GemHelper.install_tasks
8
+
9
+ require 'rake/testtask'
10
+
11
+ Rake::TestTask.new do |t|
12
+ t.libs.push "lib"
13
+ t.libs.push "spec"
14
+ t.pattern = "spec/**/*_spec.rb"
15
+ t.verbose = true
16
+ end
17
+
18
+ task :default => ['test']
@@ -0,0 +1,19 @@
1
+ # Workflow to Maintain This Gem
2
+
3
+ I use the gem-release gem
4
+
5
+ For more info see: https://github.com/svenfuchs/gem-release#usage
6
+
7
+ ## Steps for an update
8
+
9
+ 1. Update code and commit it.
10
+ 2. Add entry to CHANGELOG
11
+ * h1 for major release (1.0.0)
12
+ * h2 for minor release (0.1.0)
13
+ * h3 for patch release (0.0.1)
14
+ 3. Update version in .gemspec
15
+ 4. Commit CHANGELOG and .gemspec
16
+ 5. Release it.
17
+ * `gem release`
18
+ 6. Create a git tag and push to origin.
19
+ `gem tag`
@@ -0,0 +1,2 @@
1
+ require 'undo_manager'
2
+ require 'undo_manager/command'
@@ -0,0 +1,59 @@
1
+ # Responsibilities
2
+ # * Manage undo stack
3
+ # * Manage redo stack
4
+ # * clear redo stack when new undo command is added
5
+ # Collaborators
6
+ # * Command
7
+ class UndoManager
8
+
9
+ attr_reader :redo_stack, :undo_stack
10
+
11
+ # Initializes a new UndoManager.
12
+ # @param undo_stack [Array, optional]
13
+ def initialize(undo_stack = [])
14
+ @undo_stack = undo_stack
15
+ @redo_stack = []
16
+ @total_ops_counter = 0
17
+ end
18
+
19
+ # Returns true if there is at least one command to be redone.
20
+ def can_redo?
21
+ @redo_stack.any?
22
+ end
23
+
24
+ # Returns true if there is at least one command to be undone.
25
+ def can_undo?
26
+ @undo_stack.any?
27
+ end
28
+
29
+ # Pushes a new command onto the undo_stack.
30
+ # @param command [#do, #undo] a command object that responds to #do and #undo
31
+ # @return [Integer] number of total commands
32
+ def record_new_command(command)
33
+ @redo_stack = [] # clear redo stack
34
+ @undo_stack.push(command)
35
+ end
36
+
37
+ # Redoes command at top of redo_stack.
38
+ # @return [Command, nil] the redone command or nil if nothing was redone
39
+ def redo_command
40
+ o = @redo_stack.pop
41
+ if o
42
+ o.do # redo the command
43
+ @undo_stack.push(o)
44
+ end
45
+ o
46
+ end
47
+
48
+ # Undoes command at top of undo_stack.
49
+ # @return [Command, nil] the undone command or nil if nothing was undone
50
+ def undo_command
51
+ o = @undo_stack.pop
52
+ if o
53
+ o.undo # undo the command
54
+ @redo_stack.push(o)
55
+ end
56
+ o
57
+ end
58
+
59
+ end
@@ -0,0 +1,13 @@
1
+ class UndoManager
2
+ class Command
3
+
4
+ def do
5
+ raise "Implement me in subclass"
6
+ end
7
+
8
+ def undo
9
+ raise "Implement me in subclass"
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ # dependencies
2
+ require 'rubygems'
3
+ require 'minitest'
4
+ require 'minitest/autorun'
5
+
6
+ # Gem under test
7
+ require 'undo-manager'
@@ -0,0 +1,15 @@
1
+ require_relative '../spec_helper'
2
+
3
+ class UndoManager
4
+ describe Command do
5
+
6
+ it 'responds to #do' do
7
+ Command.new.must_respond_to(:do)
8
+ end
9
+
10
+ it 'responds to #undo' do
11
+ Command.new.must_respond_to(:undo)
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,173 @@
1
+ require_relative 'spec_helper'
2
+
3
+ describe UndoManager do
4
+
5
+ class TestCommand < UndoManager::Command
6
+
7
+ attr_reader :do_count, :undo_count
8
+
9
+ def initialize
10
+ @do_count = 0
11
+ @undo_count = 0
12
+ end
13
+
14
+ def do
15
+ @do_count += 1
16
+ end
17
+
18
+ def undo
19
+ @undo_count += 1
20
+ end
21
+
22
+ end
23
+
24
+ describe 'A new UndoManager' do
25
+
26
+ let(:undo_manager){ UndoManager.new([]) }
27
+
28
+ it 'has an empty undo_stack' do
29
+ undo_manager.undo_stack.must_be_empty
30
+ end
31
+
32
+ it 'cannot undo' do
33
+ undo_manager.can_undo?.must_equal false
34
+ end
35
+
36
+ it 'has an empty redo_stack' do
37
+ undo_manager.redo_stack.must_be_empty
38
+ end
39
+
40
+ it 'cannot redo' do
41
+ undo_manager.can_redo?.must_equal false
42
+ end
43
+
44
+ it 'uses the undo_stack passed as argument' do
45
+ us = [1,2,3]
46
+ UndoManager.new(us).undo_stack.must_equal [1,2,3]
47
+ end
48
+
49
+ end
50
+
51
+ describe 'UndoManager with commands in the undo_stack' do
52
+
53
+ let(:undo_manager){ UndoManager.new([1,2,3]) }
54
+
55
+ it 'has commands in the undo_stack' do
56
+ undo_manager.undo_stack.wont_be_empty
57
+ end
58
+
59
+ it 'has an empty redo_stack' do
60
+ undo_manager.redo_stack.must_be_empty
61
+ end
62
+
63
+ it 'can_undo' do
64
+ undo_manager.can_undo?.must_equal true
65
+ end
66
+
67
+ end
68
+
69
+ describe '#can_redo?' do
70
+
71
+ it 'returns false for empty redo_stack' do
72
+ UndoManager.new.can_redo?.must_equal false
73
+ end
74
+
75
+ it 'returns true for non-empty redo_stack' do
76
+ undo_manager = UndoManager.new([TestCommand.new])
77
+ undo_manager.undo_command
78
+ undo_manager.can_redo?.must_equal true
79
+ end
80
+
81
+ end
82
+
83
+ describe '#can_undo?' do
84
+
85
+ it 'returns false for empty undo_stack' do
86
+ UndoManager.new.can_undo?.must_equal false
87
+ end
88
+
89
+ it 'returns true for non-empty undo_stack' do
90
+ undo_manager = UndoManager.new([TestCommand.new])
91
+ undo_manager.can_undo?.must_equal true
92
+ end
93
+
94
+ end
95
+
96
+
97
+ describe '#record_new_command' do
98
+
99
+ let(:undo_manager){ UndoManager.new([]) }
100
+
101
+ it 'starts with an empty undo_stack' do
102
+ undo_manager.undo_stack.must_be_empty
103
+ end
104
+
105
+ it 'records a new command' do
106
+ undo_manager.must_respond_to(:record_new_command)
107
+ end
108
+
109
+ it 'adds new command to undo_stack' do
110
+ undo_manager.record_new_command(1)
111
+ undo_manager.undo_stack.must_equal [1]
112
+ end
113
+
114
+ it 'clears the redo_stack' do
115
+ o = TestCommand.new
116
+ undo_manager.record_new_command(o)
117
+ undo_manager.undo_command
118
+ undo_manager.redo_stack.must_equal [o]
119
+ undo_manager.record_new_command(1)
120
+ undo_manager.redo_stack.must_be_empty
121
+ end
122
+
123
+ end
124
+
125
+ describe 'redo_command' do
126
+
127
+ let(:command1){ TestCommand.new }
128
+ let(:command2){ TestCommand.new }
129
+ let(:undo_manager){
130
+ um = UndoManager.new([command1, command2])
131
+ um.undo_command
132
+ um
133
+ }
134
+
135
+ it 'redoes and returns the most recently undone command' do
136
+ undo_manager.redo_command.must_equal command2
137
+ end
138
+
139
+ it 'calls #do on the redone command' do
140
+ undo_manager.redo_command
141
+ command2.do_count.must_equal 1
142
+ end
143
+
144
+ it 'pushes the redone command onto the undo_stack' do
145
+ undo_manager.redo_command
146
+ undo_manager.undo_stack.last.must_equal command2
147
+ end
148
+
149
+ end
150
+
151
+ describe 'undo_command' do
152
+
153
+ let(:command1){ TestCommand.new }
154
+ let(:command2){ TestCommand.new }
155
+ let(:undo_manager){ UndoManager.new([command1, command2]) }
156
+
157
+ it 'undoes and returns the most recent command' do
158
+ undo_manager.undo_command.must_equal command2
159
+ end
160
+
161
+ it 'calls #undo on the undone command' do
162
+ undo_manager.undo_command
163
+ command2.undo_count.must_equal 1
164
+ end
165
+
166
+ it 'pushes the undone command onto the undo_stack' do
167
+ undo_manager.undo_command
168
+ undo_manager.redo_stack.last.must_equal command2
169
+ end
170
+
171
+ end
172
+
173
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.name = 'undo-manager'
6
+ gem.version = '0.0.1'
7
+ gem.platform = Gem::Platform::RUBY
8
+
9
+ gem.authors = ['Jo Hund']
10
+ gem.email = 'jhund@clearcove.ca'
11
+ gem.homepage = 'https://github.com/jhund/undo-manager'
12
+ gem.licenses = ['MIT']
13
+ gem.summary = 'undo-manager makes it easy to add undo/redo functionality to a Ruby (on Rails) app.'
14
+ gem.description = %(undo-manager makes it easy to add undo/redo functionality to a Ruby (on Rails) app.)
15
+
16
+ gem.files = `git ls-files`.split("\n")
17
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+
19
+ gem.add_development_dependency 'bundler', ['>= 1.0.0']
20
+ gem.add_development_dependency 'minitest'
21
+ gem.add_development_dependency 'minitest-spec-expect'
22
+ gem.add_development_dependency 'rake', ['>= 0']
23
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: undo-manager
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jo Hund
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest-spec-expect
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: undo-manager makes it easy to add undo/redo functionality to a Ruby (on
70
+ Rails) app.
71
+ email: jhund@clearcove.ca
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - CHANGELOG.md
78
+ - Gemfile
79
+ - MIT-LICENSE
80
+ - README.md
81
+ - Rakefile
82
+ - doc/how_to/release.md
83
+ - lib/undo-manager.rb
84
+ - lib/undo_manager.rb
85
+ - lib/undo_manager/command.rb
86
+ - spec/spec_helper.rb
87
+ - spec/undo_manager/command_spec.rb
88
+ - spec/undo_manager_spec.rb
89
+ - undo-manager.gemspec
90
+ homepage: https://github.com/jhund/undo-manager
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.4.5
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: undo-manager makes it easy to add undo/redo functionality to a Ruby (on Rails)
114
+ app.
115
+ test_files:
116
+ - spec/spec_helper.rb
117
+ - spec/undo_manager/command_spec.rb
118
+ - spec/undo_manager_spec.rb