ztk 1.0.0.rc.0 → 1.0.0.rc.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/ztk/dsl/core.rb CHANGED
@@ -1,4 +1,29 @@
1
+ ################################################################################
2
+ #
3
+ # Author: Zachary Patten <zachary@jovelabs.net>
4
+ # Copyright: Copyright (c) Jove Labs
5
+ # License: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+ ################################################################################
20
+
1
21
  module ZTK::DSL
22
+
23
+ # ZTK::DSL Core
24
+ #
25
+ # @author Zachary Patten <zachary@jovelabs.net>
26
+ # @api private
2
27
  module Core
3
28
  autoload :Attributes, "ztk/dsl/core/attributes"
4
29
  autoload :Actions, "ztk/dsl/core/actions"
@@ -21,6 +46,7 @@ module ZTK::DSL
21
46
  end
22
47
  end
23
48
 
49
+ # @author Zachary Patten <zachary@jovelabs.net>
24
50
  module DualMethods
25
51
 
26
52
  def logger
@@ -35,6 +61,7 @@ module ZTK::DSL
35
61
 
36
62
  end
37
63
 
64
+ # @author Zachary Patten <zachary@jovelabs.net>
38
65
  module ClassMethods
39
66
 
40
67
  def cattr_accessor(*args)
@@ -19,6 +19,9 @@
19
19
  ################################################################################
20
20
 
21
21
  module ZTK::DSL::Core
22
+
23
+ # @author Zachary Patten <zachary@jovelabs.net>
24
+ # @api private
22
25
  module Actions
23
26
  autoload :Find, "ztk/dsl/core/actions/find"
24
27
  autoload :Timestamps, "ztk/dsl/core/actions/timestamps"
@@ -19,6 +19,9 @@
19
19
  ################################################################################
20
20
 
21
21
  module ZTK::DSL::Core::Actions
22
+
23
+ # @author Zachary Patten <zachary@jovelabs.net>
24
+ # @api private
22
25
  module Find
23
26
 
24
27
  def self.included(base)
@@ -27,6 +30,7 @@ module ZTK::DSL::Core::Actions
27
30
  end
28
31
  end
29
32
 
33
+ # @author Zachary Patten <zachary@jovelabs.net>
30
34
  module ClassMethods
31
35
 
32
36
  def all
@@ -19,6 +19,9 @@
19
19
  ################################################################################
20
20
 
21
21
  module ZTK::DSL::Core::Actions
22
+
23
+ # @author Zachary Patten <zachary@jovelabs.net>
24
+ # @api private
22
25
  module Timestamps
23
26
 
24
27
  def self.included(base)
@@ -31,6 +34,7 @@ module ZTK::DSL::Core::Actions
31
34
  end
32
35
  end
33
36
 
37
+ # @author Zachary Patten <zachary@jovelabs.net>
34
38
  module ClassMethods
35
39
 
36
40
  def updated_at_timestamp
@@ -19,6 +19,9 @@
19
19
  ################################################################################
20
20
 
21
21
  module ZTK::DSL::Core
22
+
23
+ # @author Zachary Patten <zachary@jovelabs.net>
24
+ # @api private
22
25
  module Attributes
23
26
 
24
27
  def self.included(base)
@@ -31,6 +34,7 @@ module ZTK::DSL::Core
31
34
  @attributes ||= {}
32
35
  end
33
36
 
37
+ # @author Zachary Patten <zachary@jovelabs.net>
34
38
  module ClassMethods
35
39
 
36
40
  def attribute(key, options={})
@@ -19,6 +19,9 @@
19
19
  ################################################################################
20
20
 
21
21
  module ZTK::DSL::Core
22
+
23
+ # @author Zachary Patten <zachary@jovelabs.net>
24
+ # @api private
22
25
  module Dataset
23
26
 
24
27
  def self.included(base)
@@ -27,6 +30,7 @@ module ZTK::DSL::Core
27
30
  end
28
31
  end
29
32
 
33
+ # @author Zachary Patten <zachary@jovelabs.net>
30
34
  module ClassMethods
31
35
 
32
36
  def dataset
@@ -48,18 +52,6 @@ module ZTK::DSL::Core
48
52
  (@@id += 1)
49
53
  end
50
54
 
51
- # def foreign_keys(target)
52
- # puts("==> foreign_keys(#{target.inspect})")
53
-
54
- # target = singularize(underscore(target).to_sym)
55
- # source = singularize(underscore(self).to_sym)
56
-
57
- # splat = [source, target].sort
58
-
59
- # key = [ splat[0], "references", splat[1] ].join(":")
60
- # puts("key=#{key}")
61
- # end
62
-
63
55
  end
64
56
 
65
57
  end
@@ -19,6 +19,9 @@
19
19
  ################################################################################
20
20
 
21
21
  module ZTK::DSL::Core
22
+
23
+ # @author Zachary Patten <zachary@jovelabs.net>
24
+ # @api private
22
25
  module IO
23
26
 
24
27
  def self.included(base)
@@ -27,6 +30,7 @@ module ZTK::DSL::Core
27
30
  end
28
31
  end
29
32
 
33
+ # @author Zachary Patten <zachary@jovelabs.net>
30
34
  module ClassMethods
31
35
 
32
36
  def load(rb_file)
@@ -19,6 +19,9 @@
19
19
  ################################################################################
20
20
 
21
21
  module ZTK::DSL::Core
22
+
23
+ # @author Zachary Patten <zachary@jovelabs.net>
24
+ # @api private
22
25
  module Relations
23
26
  autoload :BelongsTo, "ztk/dsl/core/relations/belongs_to"
24
27
  autoload :HasMany, "ztk/dsl/core/relations/has_many"
@@ -31,6 +34,7 @@ module ZTK::DSL::Core
31
34
  end
32
35
  end
33
36
 
37
+ # @author Zachary Patten <zachary@jovelabs.net>
34
38
  module ClassMethods
35
39
 
36
40
  def add_relation(key)
@@ -19,6 +19,9 @@
19
19
  ################################################################################
20
20
 
21
21
  module ZTK::DSL::Core::Relations
22
+
23
+ # @author Zachary Patten <zachary@jovelabs.net>
24
+ # @api private
22
25
  module BelongsTo
23
26
 
24
27
  def self.included(base)
@@ -69,6 +72,7 @@ module ZTK::DSL::Core::Relations
69
72
  end
70
73
  end
71
74
 
75
+ # @author Zachary Patten <zachary@jovelabs.net>
72
76
  module ClassMethods
73
77
 
74
78
  def belongs_to(key, options={})
@@ -19,6 +19,9 @@
19
19
  ################################################################################
20
20
 
21
21
  module ZTK::DSL::Core::Relations
22
+
23
+ # @author Zachary Patten <zachary@jovelabs.net>
24
+ # @api private
22
25
  module HasMany
23
26
 
24
27
  def self.included(base)
@@ -62,6 +65,7 @@ module ZTK::DSL::Core::Relations
62
65
  end
63
66
  end
64
67
 
68
+ # @author Zachary Patten <zachary@jovelabs.net>
65
69
  module ClassMethods
66
70
 
67
71
  def has_many(key, options={})
data/lib/ztk/parallel.rb CHANGED
@@ -30,6 +30,13 @@ module ZTK
30
30
  #
31
31
  # This class can be used to easily run iterative and linear processes in a parallel manner.
32
32
  #
33
+ # The before fork callback is called once in the parent process.
34
+ #
35
+ # The after fork callback is called twice, once in the parent process and once
36
+ # in the child process.
37
+ #
38
+ # *example code*:
39
+ #
33
40
  # a_callback = Proc.new do |pid|
34
41
  # puts "Hello from After Callback - PID #{pid}"
35
42
  # end
@@ -44,6 +51,8 @@ module ZTK
44
51
  # config.after_fork = a_callback
45
52
  # end
46
53
  #
54
+ # puts Process.pid.inspect
55
+ #
47
56
  # 3.times do |x|
48
57
  # parallel.process do
49
58
  # x
@@ -51,12 +60,57 @@ module ZTK
51
60
  # end
52
61
  #
53
62
  # parallel.waitall
54
- # parallel.results
63
+ # puts parallel.results.inspect
55
64
  #
56
- # The before fork callback is called once in the parent process.
65
+ # *pry output*:
57
66
  #
58
- # The after fork callback is called twice, once in the parent process and once
59
- # in the child process.
67
+ # [1] pry(main)> a_callback = Proc.new do |pid|
68
+ # [1] pry(main)* puts "Hello from After Callback - PID #{pid}"
69
+ # [1] pry(main)* end
70
+ # => #<Proc:0x000000015a8768@(pry):1>
71
+ # [2] pry(main)>
72
+ # [3] pry(main)> b_callback = Proc.new do |pid|
73
+ # [3] pry(main)* puts "Hello from Before Callback - PID #{pid}"
74
+ # [3] pry(main)* end
75
+ # => #<Proc:0x000000012910e8@(pry):4>
76
+ # [4] pry(main)>
77
+ # [5] pry(main)> parallel = ZTK::Parallel.new
78
+ # => #<ZTK::Parallel:0x000000015a9d48
79
+ # @config=
80
+ # #<OpenStruct stdout=#<IO:<STDOUT>>, stderr=#<IO:<STDERR>>, stdin=#<IO:<STDIN>>, logger=#<ZTK::Logger filename="/dev/null">, max_forks=12>,
81
+ # @forks=[],
82
+ # @results=[]>
83
+ # [6] pry(main)> parallel.config do |config|
84
+ # [6] pry(main)* config.before_fork = b_callback
85
+ # [6] pry(main)* config.after_fork = a_callback
86
+ # [6] pry(main)* end
87
+ # => #<Proc:0x000000015a8768@(pry):1>
88
+ # [7] pry(main)>
89
+ # [8] pry(main)> puts Process.pid.inspect
90
+ # 24761
91
+ # => nil
92
+ # [9] pry(main)>
93
+ # [10] pry(main)> 3.times do |x|
94
+ # [10] pry(main)* parallel.process do
95
+ # [10] pry(main)* x
96
+ # [10] pry(main)* end
97
+ # [10] pry(main)* end
98
+ # Hello from Before Callback - PID 24761
99
+ # Hello from After Callback - PID 24761
100
+ # Hello from Before Callback - PID 24761
101
+ # Hello from After Callback - PID 24776
102
+ # Hello from After Callback - PID 24761
103
+ # Hello from Before Callback - PID 24761
104
+ # Hello from After Callback - PID 24779
105
+ # Hello from After Callback - PID 24761
106
+ # Hello from After Callback - PID 24782
107
+ # => 3
108
+ # [11] pry(main)>
109
+ # [12] pry(main)> parallel.waitall
110
+ # => [0, 1, 2]
111
+ # [13] pry(main)> puts parallel.results.inspect
112
+ # [0, 1, 2]
113
+ # => nil
60
114
  #
61
115
  # @author Zachary Patten <zachary@jovelabs.net>
62
116
  class Parallel < ZTK::Base
@@ -80,7 +134,7 @@ module ZTK
80
134
  super({
81
135
  :max_forks => MAX_FORKS
82
136
  }.merge(configuration))
83
- config.logger.debug { "config=#{config.send(:table).inspect}" }
137
+ config.ui.logger.debug { "config=#{config.send(:table).inspect}" }
84
138
 
85
139
  (config.max_forks < 1) and log_and_raise(ParallelError, "max_forks must be equal to or greater than one!")
86
140
 
@@ -98,7 +152,7 @@ module ZTK
98
152
  def process(&block)
99
153
  !block_given? and log_and_raise(ParallelError, "You must supply a block to the process method!")
100
154
 
101
- config.logger.debug { "forks(#{@forks.inspect})" }
155
+ config.ui.logger.debug { "forks(#{@forks.inspect})" }
102
156
 
103
157
  while (@forks.count >= config.max_forks) do
104
158
  wait
@@ -115,7 +169,7 @@ module ZTK
115
169
  parent_reader.close
116
170
 
117
171
  if !(data = block.call).nil?
118
- config.logger.debug { "write(#{data.inspect})" }
172
+ config.ui.logger.debug { "write(#{data.inspect})" }
119
173
  child_writer.write(Base64.encode64(Marshal.dump(data)))
120
174
  end
121
175
 
@@ -143,12 +197,12 @@ module ZTK
143
197
  # status and data returned from the process block. If wait2() fails nil
144
198
  # is returned.
145
199
  def wait
146
- config.logger.debug { "wait" }
147
- config.logger.debug { "forks(#{@forks.inspect})" }
200
+ config.ui.logger.debug { "wait" }
201
+ config.ui.logger.debug { "forks(#{@forks.inspect})" }
148
202
  pid, status = (Process.wait2(-1) rescue nil)
149
203
  if !pid.nil? && !status.nil? && !(fork = @forks.select{ |f| f[:pid] == pid }.first).nil?
150
204
  data = (Marshal.load(Base64.decode64(fork[:reader].read.to_s)) rescue nil)
151
- config.logger.debug { "read(#{data.inspect})" }
205
+ config.ui.logger.debug { "read(#{data.inspect})" }
152
206
  !data.nil? and @results.push(data)
153
207
  fork[:reader].close
154
208
  fork[:writer].close
@@ -163,7 +217,7 @@ module ZTK
163
217
  #
164
218
  # @return [Array<Object>] The results from all of the *process* blocks.
165
219
  def waitall
166
- config.logger.debug { "waitall" }
220
+ config.ui.logger.debug { "waitall" }
167
221
  while @forks.count > 0
168
222
  self.wait
169
223
  end
@@ -174,7 +228,7 @@ module ZTK
174
228
  #
175
229
  # @return [Integer] Current number of active forks.
176
230
  def count
177
- config.logger.debug { "count(#{@forks.count})" }
231
+ config.ui.logger.debug { "count(#{@forks.count})" }
178
232
  @forks.count
179
233
  end
180
234
 
data/lib/ztk/report.rb CHANGED
@@ -43,7 +43,7 @@ module ZTK
43
43
  def initialize(configuration={})
44
44
  super({
45
45
  }.merge(configuration))
46
- config.logger.debug { "config=#{config.send(:table).inspect}" }
46
+ config.ui.logger.debug { "config=#{config.send(:table).inspect}" }
47
47
  end
48
48
 
49
49
  # Displays data in a spreadsheet style.
@@ -84,19 +84,19 @@ module ZTK
84
84
  end
85
85
  header_line = format_row(header_line)
86
86
 
87
- config.stdout.puts(format_header(headers, max_lengths))
88
- config.stdout.puts(header_line)
89
- config.stdout.puts(format_header(headers, max_lengths))
87
+ config.ui.stdout.puts(format_header(headers, max_lengths))
88
+ config.ui.stdout.puts(header_line)
89
+ config.ui.stdout.puts(format_header(headers, max_lengths))
90
90
 
91
91
  rows.each do |row|
92
92
  row_line = headers.collect do |header|
93
93
  "%-#{max_lengths.send(header)}s" % row.send(header)
94
94
  end
95
95
  row_line = format_row(row_line)
96
- config.stdout.puts(row_line)
96
+ config.ui.stdout.puts(row_line)
97
97
  end
98
98
 
99
- config.stdout.puts(format_header(headers, max_lengths))
99
+ config.ui.stdout.puts(format_header(headers, max_lengths))
100
100
 
101
101
  width = (2 + max_lengths.send(:table).values.reduce(:+) + ((headers.count * 3) - 3) + 2)
102
102
 
@@ -141,13 +141,13 @@ module ZTK
141
141
  width = (max_key_length + max_value_length + 2 + 2 + 2)
142
142
 
143
143
  rows.each do |row|
144
- config.stdout.puts("+#{"-" * width}+")
144
+ config.ui.stdout.puts("+#{"-" * width}+")
145
145
  headers.each do |header|
146
146
  entry_line = format_entry(header, max_key_length, row.send(header), max_value_length)
147
- config.stdout.puts(entry_line)
147
+ config.ui.stdout.puts(entry_line)
148
148
  end
149
149
  end
150
- config.stdout.puts("+#{"-" * width}+")
150
+ config.ui.stdout.puts("+#{"-" * width}+")
151
151
 
152
152
  OpenStruct.new(:rows => rows, :max_key_length => max_key_length, :max_value_length => max_value_length, :width => width)
153
153
  end
@@ -33,17 +33,66 @@ module ZTK
33
33
  # The block is yielded and if a valid exception occurs the block will be
34
34
  # re-executed for the set number of attempts.
35
35
  #
36
- # For example, from the ZTK SSH class:
36
+ # *example code*:
37
37
  #
38
+ # counter = 0
38
39
  # ZTK::RescueRetry.try(:tries => 3, :on => EOFError) do
39
- # @ssh = Net::SSH.start(config.host_name, config.user, ssh_options)
40
- # ...
40
+ # counter += 1
41
+ # raise EOFError
41
42
  # end
43
+ # puts counter.inspect
42
44
  #
43
- # If an SSH connection drops on the other side and we go to write to it, we
44
- # will get this error. Wrapping the SSH code in *RescueRetry* allows us to
45
- # retry the connection in the event this happens. If we have no luck after
46
- # 3 attempts at executing the block, *RescueRetry* surfaces the exception.
45
+ # counter = 0
46
+ # ZTK::RescueRetry.try(:tries => 3) do
47
+ # counter += 1
48
+ # raise "OMGWTFBBQ"
49
+ # end
50
+ # puts counter.inspect
51
+ #
52
+ # counter = 0
53
+ # ZTK::RescueRetry.try(:tries => 3, :on => EOFError) do
54
+ # counter += 1
55
+ # raise "OMGWTFBBQ"
56
+ # end
57
+ # puts counter.inspect
58
+ #
59
+ # *pry output*:
60
+ #
61
+ # [1] pry(main)> counter = 0
62
+ # => 0
63
+ # [2] pry(main)> ZTK::RescueRetry.try(:tries => 3, :on => EOFError) do
64
+ # [2] pry(main)* counter += 1
65
+ # [2] pry(main)* raise EOFError
66
+ # [2] pry(main)* end
67
+ # EOFError: EOFError
68
+ # from (pry):4:in `block in <main>'
69
+ # [3] pry(main)> puts counter.inspect
70
+ # 3
71
+ # => nil
72
+ # [4] pry(main)>
73
+ # [5] pry(main)> counter = 0
74
+ # => 0
75
+ # [6] pry(main)> ZTK::RescueRetry.try(:tries => 3) do
76
+ # [6] pry(main)* counter += 1
77
+ # [6] pry(main)* raise "OMGWTFBBQ"
78
+ # [6] pry(main)* end
79
+ # RuntimeError: OMGWTFBBQ
80
+ # from (pry):10:in `block in <main>'
81
+ # [7] pry(main)> puts counter.inspect
82
+ # 3
83
+ # => nil
84
+ # [8] pry(main)>
85
+ # [9] pry(main)> counter = 0
86
+ # => 0
87
+ # [10] pry(main)> ZTK::RescueRetry.try(:tries => 3, :on => EOFError) do
88
+ # [10] pry(main)* counter += 1
89
+ # [10] pry(main)* raise "OMGWTFBBQ"
90
+ # [10] pry(main)* end
91
+ # RuntimeError: OMGWTFBBQ
92
+ # from (pry):16:in `block in <main>'
93
+ # [11] pry(main)> puts counter.inspect
94
+ # 1
95
+ # => nil
47
96
  #
48
97
  # @author Zachary Patten <zachary@jovelabs.net>
49
98
  class RescueRetry
@@ -75,19 +124,19 @@ module ZTK
75
124
  :on => Exception,
76
125
  :delay => 1
77
126
  }.merge(options))
78
- options.logger.debug { "options=#{options.send(:table).inspect}" }
127
+ options.ui.logger.debug { "options=#{options.send(:table).inspect}" }
79
128
 
80
- !block_given? and Base.log_and_raise(options.logger, RescueRetryError, "You must supply a block!")
129
+ !block_given? and Base.log_and_raise(options.ui.logger, RescueRetryError, "You must supply a block!")
81
130
 
82
131
  begin
83
132
  return block.call
84
133
  rescue options.on => e
85
134
  if ((options.tries -= 1) > 0)
86
- options.logger.warn { "Caught #{e.inspect}, we will give it #{options.tries} more tr#{options.tries > 1 ? 'ies' : 'y'}." }
135
+ options.ui.logger.warn { "Caught #{e.inspect}, we will give it #{options.tries} more tr#{options.tries > 1 ? 'ies' : 'y'}." }
87
136
  sleep(options.delay)
88
137
  retry
89
138
  else
90
- options.logger.fatal { "Caught #{e.inspect} and we have no more tries left, sorry, we have to give up now." }
139
+ options.ui.logger.fatal { "Caught #{e.inspect} and we have no more tries left, sorry, we have to give up now." }
91
140
  raise e
92
141
  end
93
142
  end