piko_transaction 1.0.1 → 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: 12be8dac1f3bdbf43a1578b6bf2d40de99db6a6d
4
- data.tar.gz: be08565903cc98a3901b3beee57c8d2d9835376f
3
+ metadata.gz: 22c96f391920e4d74555e59e51ea8ed6281f4acc
4
+ data.tar.gz: 70f19c75b100b5e5e89f72d6cacfb7b041052180
5
5
  SHA512:
6
- metadata.gz: a4a9b7ff9fc9107f015a0f2e35bceed5e41ea356ede169e36a3f02ab61c73bd3a8fc7ae6f49e7287b0aeb9b32d6fa4b92409164de37a7d39913a1aa356a40a63
7
- data.tar.gz: 2adf4e855f2159135032b6d6b8544089759f67589594ec25caed1cccbc8bcefd31f942ee597601dc342db85837730af22e49861ae5e454e7ce01cb8975e7a99a
6
+ metadata.gz: 10153b9ef8b9d66691b9ce926570c2b24029c147546140ab58b8ec83c80ccc54b5e2e5eac6d53ee073e3b0d4cc3bd86d6883481c0567bad5fa67ee622f52796b
7
+ data.tar.gz: fa98740f073eda60cd2a98b3564f033629c7b8bb72fdc7993fa6c8f464048b3ddf5a5caea23584d243d6f7d496e03c571dd39fb2ec2ca91df19b80fb1ab0c0a0
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ### 1.1.0 (2017.06.08)
2
+
3
+ * add custom command
4
+
1
5
  ### 1.0.1 (2017.06.06)
2
6
 
3
7
  * add struct graph
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright (C) 2017 Szymon Kopciewski
4
+ #
5
+ # This file is part of PikoTransaction.
6
+ #
7
+ # This program is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # This program is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
+
20
+ require "piko_transaction/logger"
21
+
22
+ module PikoTransaction
23
+ class CustomCommand
24
+ include Logger
25
+
26
+ def initialize(do_action = nil, undo_action = nil, &alternative_do_action)
27
+ @do_action = do_action
28
+ @undo_action = undo_action
29
+ @alternative_do_action = alternative_do_action
30
+ @done = false
31
+ end
32
+
33
+ def do
34
+ return terminate("Command already done") unless can_do?
35
+ return terminate("Can not execute 'do' action") unless execute_do_action
36
+ mark_as_done
37
+ true
38
+ end
39
+
40
+ def undo
41
+ return terminate("Can not undo command") unless can_undo?
42
+ return terminate("Can not execute 'undo' action") unless execute_undo_action
43
+ mark_as_undone
44
+ true
45
+ end
46
+
47
+ private
48
+
49
+ def terminate(msg)
50
+ logger.warn { msg }
51
+ false
52
+ end
53
+
54
+ def mark_as_done
55
+ @done = true
56
+ end
57
+
58
+ def mark_as_undone
59
+ @done = false
60
+ end
61
+
62
+ def can_do?
63
+ !@done
64
+ end
65
+
66
+ def can_undo?
67
+ @done
68
+ end
69
+
70
+ def execute_do_action
71
+ action = choose_do_action
72
+ return terminate("Bad 'do' action") unless action.respond_to? :call
73
+ logger.debug { "Executing do action" }
74
+ action.call
75
+ end
76
+
77
+ def choose_do_action
78
+ @do_action || @alternative_do_action
79
+ end
80
+
81
+ def execute_undo_action
82
+ return true unless @undo_action.respond_to? :call
83
+ logger.debug { "Executing undo action" }
84
+ @undo_action.call
85
+ end
86
+ end
87
+ end
@@ -18,5 +18,5 @@
18
18
  # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
19
 
20
20
  module PikoTransaction
21
- VERSION = "1.0.1"
21
+ VERSION = "1.1.0"
22
22
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: piko_transaction
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Szymon Kopciewski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-06 00:00:00.000000000 Z
11
+ date: 2017-06-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: logger2r
@@ -120,6 +120,7 @@ files:
120
120
  - LICENSE
121
121
  - README.md
122
122
  - lib/piko_transaction.rb
123
+ - lib/piko_transaction/custom_command.rb
123
124
  - lib/piko_transaction/delete_command.rb
124
125
  - lib/piko_transaction/insert_command.rb
125
126
  - lib/piko_transaction/logger.rb