Class: OpencBot::BaseIncrementer
- Inherits:
-
Object
- Object
- OpencBot::BaseIncrementer
show all
- Defined in:
- lib/openc_bot/incrementers/base.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(name, opts = {}) ⇒ BaseIncrementer
Returns a new instance of BaseIncrementer.
6
7
8
9
10
11
12
13
14
15
|
# File 'lib/openc_bot/incrementers/base.rb', line 6
def initialize(name, opts={})
@name = name
@expected_count = opts[:expected_count]
@count = 0
@app_path = opts[:app_path]
@show_progress = opts[:show_progress] || (opts[:show_progress].nil? && true)
@reset_iterator = opts[:reset_iterator]
@max_iterations = opts[:max_iterations]
@opts = opts
end
|
Class Method Details
.new(*args) ⇒ Object
17
18
19
20
21
22
23
|
# File 'lib/openc_bot/incrementers/base.rb', line 17
def self.new(*args)
path, = caller[0].partition(":")
path = File.expand_path(File.join(File.dirname(path), ".."))
args << {} if args.count == 1
args[1][:app_path] = path if !args[1][:app_path]
super(*args)
end
|
Instance Method Details
#db_name ⇒ Object
75
76
77
|
# File 'lib/openc_bot/incrementers/base.rb', line 75
def db_name
@name
end
|
#each ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/openc_bot/incrementers/base.rb', line 33
def each
Enumerator.new do |yielder|
increment_yielder do |result|
if result.is_a? Hash
formatted_result = result.to_json
else
formatted_result = result
end
write_current(formatted_result)
yielder.yield(result)
@count += 1
log_progress(progress_percent)
end
reset_current
end.lazy
end
|
#log_progress(percent) ⇒ Object
25
26
27
|
# File 'lib/openc_bot/incrementers/base.rb', line 25
def log_progress(percent)
puts "Iterator #{@name} progress: " + (percent.to_s + "%") if @show_progress
end
|
#position_file_name ⇒ Object
71
72
73
|
# File 'lib/openc_bot/incrementers/base.rb', line 71
def position_file_name
"#{@app_path}/db/#{db_name}-iterator-position.txt"
end
|
#progress_percent ⇒ Object
29
30
31
|
# File 'lib/openc_bot/incrementers/base.rb', line 29
def progress_percent
(@count.to_f / @expected_count * 100).round(2) if @expected_count
end
|
#read_current ⇒ Object
92
93
94
95
96
97
98
99
100
|
# File 'lib/openc_bot/incrementers/base.rb', line 92
def read_current
begin
File.open(position_file_name, "r") do |f|
f.read
end
rescue Errno::ENOENT
nil
end
end
|
#reset_current ⇒ Object
this is done with a file, rather than SQL, for speed reasons
80
81
82
83
84
|
# File 'lib/openc_bot/incrementers/base.rb', line 80
def reset_current
File.open(position_file_name, "w") do |f|
f.write("")
end
end
|
#resumable ⇒ Object
50
51
52
53
54
55
|
# File 'lib/openc_bot/incrementers/base.rb', line 50
def resumable
enum = each
enum = resuming_enum(enum) unless @reset_iterator
enum = enum.take(@max_iterations) if @max_iterations
enum
end
|
#resuming_enum(enum) ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/openc_bot/incrementers/base.rb', line 57
def resuming_enum(enum)
start_from = read_current
preset_show_progress = @show_progress
@show_progress = false
if start_from && start_from != ""
enum = enum.drop_while do |x|
found_start_point = (x.to_s == start_from)
@show_progress = preset_show_progress && found_start_point
!found_start_point
end
end
enum
end
|
#write_current(val) ⇒ Object
86
87
88
89
90
|
# File 'lib/openc_bot/incrementers/base.rb', line 86
def write_current(val)
File.open(position_file_name, "w") do |f|
f.write(val.to_s)
end
end
|