kitcat 1.0.0 → 1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f6c930e9f26d08132fc15fa533107ebd5d8f9c43
4
- data.tar.gz: 898f7a3fa58d3b38cf85692aee414b0b9dab82d3
3
+ metadata.gz: f897bc2d5d9d7171d62f024338a59076cda9cd4e
4
+ data.tar.gz: b2a8c4e3a483b5e82dd46a5d45dad08feb28f3b8
5
5
  SHA512:
6
- metadata.gz: 16cec3058a3b816289dfc6b2cf7adc8bea0b210a07103596c4096e66b436012056cfee8d8ae55d5019a27966d1e7951510d7f764e557a5bfc952235d5a21f1db
7
- data.tar.gz: d3af7d89498d41509cd026c0ee3f72c1294584d28a2f18e5e8daf19fafa1834ff8777b8ee775072bae413499dcf61b5c4336942a87aa6a3fb5bbcf7a08ac5a16
6
+ metadata.gz: 42ffa3a76b786a4c30e940f9fc397641d568f297df6398295e8696d66b18c245b84b102fe3e9bfd4b7abeeef4da67652c32e3c0947cdca503afc47b585837519
7
+ data.tar.gz: 41fad141fa2dbecfb79ebd1d17b8beacdba3dbf17d2632b79ee4742e368121f160d945640fe739b2d5d533bf42f0a3461dc7f28724ce723f8b49bf7c30cba079
@@ -1,13 +1,20 @@
1
1
  require 'ruby-progressbar'
2
2
  require 'active_model'
3
3
  require 'active_support/core_ext'
4
+ require 'kitcat/logging'
4
5
 
5
6
  module KitCat
6
7
  class Framework
7
8
  attr_reader :last_item_processed,
8
- :migration_name,
9
9
  :number_of_items_processed,
10
- :migration_strategy
10
+ :migration_strategy,
11
+ :logging
12
+
13
+ delegate :log_file_path,
14
+ :start_logging, :end_logging,
15
+ :log_success, :log_failure,
16
+ :log_interrupt_callback_start,
17
+ :log_interrupt_callback_finish, to: :logging
11
18
 
12
19
  # @params migration_strategy {Object}
13
20
  # Instance implementing the methods of +KitCat::Callbacks+
@@ -34,42 +41,24 @@ module KitCat
34
41
  progress_bar: true,
35
42
  progress_bar_output: STDOUT)
36
43
  @migration_strategy = migration_strategy
37
- @migration_name = build_migration_name(migration_name)
38
44
  @number_of_items_to_process = number_of_items_to_process
39
45
  @last_item_processed = nil
40
46
  @progress_bar = initialize_progress_bar(progress_bar, progress_bar_output)
47
+ @logging = KitCat::Logging.new(migration_strategy, migration_name)
41
48
  end
42
49
 
43
50
  def execute
44
- @interrupted = false
45
- Signal.trap('TERM') { @interrupted = true }
46
- Signal.trap('INT') { @interrupted = true }
51
+ trap_signals
52
+
47
53
  start_logging
48
54
 
49
55
  @number_of_items_processed = 0
50
56
 
51
57
  items.each do |item|
52
- if migration_strategy.process(item)
53
-
54
- log_success(item)
55
-
56
- @number_of_items_processed += 1
57
-
58
- increment_progress_bar
59
-
60
- @last_item_processed = item
61
-
62
- break unless process_more?
63
- else
64
- log_failure(item)
65
-
66
- break
67
- end
68
- if @interrupted
69
- handle_user_interrupt
70
- break
71
- end
58
+ break unless execute_for(item)
72
59
  end
60
+
61
+ ensure
73
62
  end_logging
74
63
  end
75
64
 
@@ -77,10 +66,6 @@ module KitCat
77
66
  @number_of_items_to_process ||= migration_strategy.criteria.count
78
67
  end
79
68
 
80
- def log_file_path
81
- @log_file_path ||= File.join(log_dir, build_log_file_name)
82
- end
83
-
84
69
  def progress_bar?
85
70
  !@progress_bar.nil?
86
71
  end
@@ -92,31 +77,58 @@ module KitCat
92
77
 
93
78
  private
94
79
 
95
- def items
96
- return enum_for(:items) unless block_given?
80
+ def execute_for(item)
81
+ begin
82
+ if migration_strategy.process(item)
97
83
 
98
- enum = migration_strategy.criteria.each
84
+ commit_success(item)
99
85
 
100
- loop do
101
- yield enum.next
86
+ return false unless process_more?
87
+ else
88
+ commit_failure(item)
89
+
90
+ return false
91
+ end
92
+ if @interrupted
93
+ handle_user_interrupt
94
+ return false
95
+ end
96
+ rescue StandardError
97
+ commit_failure(item)
98
+ raise
102
99
  end
100
+
101
+ true
103
102
  end
104
103
 
105
- def log_dir
106
- @log_dir ||= FileUtils.mkdir_p(File.join(Dir.pwd, 'log'))
104
+ def commit_success(item)
105
+ log_success(item)
106
+
107
+ @number_of_items_processed += 1
108
+
109
+ increment_progress_bar
110
+
111
+ @last_item_processed = item
107
112
  end
108
113
 
109
- def build_log_file_name
110
- "migration-#{migration_name}-#{timestamp}.log"
114
+ def commit_failure(item)
115
+ log_failure(item)
111
116
  end
112
117
 
113
- def timestamp
114
- Time.now.to_s(:number)
118
+ def trap_signals
119
+ @interrupted = false
120
+ Signal.trap('TERM') { @interrupted = true }
121
+ Signal.trap('INT') { @interrupted = true }
115
122
  end
116
123
 
117
- def build_migration_name(migration_name)
118
- result = migration_name || migration_strategy.class.name.delete(':').underscore.upcase
119
- result.gsub(/\W/, '').upcase
124
+ def items
125
+ return enum_for(:items) unless block_given?
126
+
127
+ enum = migration_strategy.criteria.each
128
+
129
+ loop do
130
+ yield enum.next
131
+ end
120
132
  end
121
133
 
122
134
  def initialize_progress_bar(progress_bar_flag, output)
@@ -132,39 +144,6 @@ module KitCat
132
144
  format: "%a %bᗧ%i %p%% %e")
133
145
  end
134
146
 
135
- def start_logging
136
- logger.info 'Start Processing...'
137
- end
138
-
139
- def end_logging
140
- logger.info '...end of processing'
141
- end
142
-
143
- def logger
144
- @logger ||= Logger.new(log_file_path)
145
- end
146
-
147
- def log_success(item)
148
- log_line(item) { |method| logger.info "...successfully processed item: #{item.try(method)}" }
149
- end
150
-
151
- def log_failure(item)
152
- log_line(item) { |method| logger.error "...error while processing item: #{item.try(method)}" }
153
- end
154
-
155
- def log_line(item)
156
- method = item.respond_to?(:to_log) ? :to_log : :to_s
157
- yield method
158
- end
159
-
160
- def log_interrupt_callback_start
161
- logger.info '...user interrupted, calling interrupt callback on migration strategy...'
162
- end
163
-
164
- def log_interrupt_callback_finish
165
- logger.info '......end of interrupt callback after user interruption'
166
- end
167
-
168
147
  def process_more?
169
148
  @number_of_items_to_process.nil? || @number_of_items_processed < @number_of_items_to_process
170
149
  end
@@ -0,0 +1,71 @@
1
+ module KitCat
2
+ # +KitCat::Logging+ is used to encapsulate the functionality of +Framework+ related to
3
+ # logging.
4
+ #
5
+ # It needs to be initialized with the +migration_name+ because this is used to build the
6
+ # relevant log filename.
7
+ #
8
+ class Logging
9
+ attr_reader :migration_name, :migration_strategy
10
+
11
+ def initialize(migration_strategy, migration_name)
12
+ @migration_name = build_migration_name(migration_name, migration_strategy)
13
+ end
14
+
15
+ def log_file_path
16
+ @log_file_path ||= File.join(log_dir, build_log_file_name)
17
+ end
18
+
19
+ def start_logging
20
+ logger.info 'Start Processing...'
21
+ end
22
+
23
+ def end_logging
24
+ logger.info '...end of processing'
25
+ end
26
+
27
+ def log_success(item)
28
+ log_line(item) { |method| logger.info "...successfully processed item: #{item.try(method)}" }
29
+ end
30
+
31
+ def log_failure(item)
32
+ log_line(item) { |method| logger.error "...error while processing item: #{item.try(method)}" }
33
+ end
34
+
35
+ def log_interrupt_callback_start
36
+ logger.info '...user interrupted, calling interrupt callback on migration strategy...'
37
+ end
38
+
39
+ def log_interrupt_callback_finish
40
+ logger.info '......end of interrupt callback after user interruption'
41
+ end
42
+
43
+ private
44
+
45
+ def build_migration_name(migration_name, migration_strategy)
46
+ result = migration_name || migration_strategy.class.name.delete(':').underscore.upcase
47
+ result.gsub(/\W/, '').upcase
48
+ end
49
+
50
+ def log_dir
51
+ @log_dir ||= FileUtils.mkdir_p(File.join(Dir.pwd, 'log'))
52
+ end
53
+
54
+ def build_log_file_name
55
+ "migration-#{migration_name}-#{timestamp}.log"
56
+ end
57
+
58
+ def timestamp
59
+ Time.now.to_s(:number)
60
+ end
61
+
62
+ def logger
63
+ @logger ||= Logger.new(log_file_path)
64
+ end
65
+
66
+ def log_line(item)
67
+ method = item.respond_to?(:to_log) ? :to_log : :to_s
68
+ yield method
69
+ end
70
+ end
71
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kitcat
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simply Business
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-04 00:00:00.000000000 Z
11
+ date: 2016-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-progressbar
@@ -132,6 +132,7 @@ extra_rdoc_files: []
132
132
  files:
133
133
  - Rakefile
134
134
  - lib/kitcat/framework.rb
135
+ - lib/kitcat/logging.rb
135
136
  - lib/kitcat/terminal_width_calculator.rb
136
137
  - lib/kitcat.rb
137
138
  - LICENSE