playwhe 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Binary file
@@ -1,109 +0,0 @@
1
- require 'data_mapper'
2
- require 'date'
3
-
4
- require 'playwhe/storage/models'
5
-
6
- module PlayWhe
7
-
8
- module Storage
9
-
10
- def self.connect(path_to_db, log_level = :debug)
11
- DataMapper::Logger.new($stdout, log_level)
12
- DataMapper.setup(:default, "sqlite://#{File.expand_path(path_to_db)}")
13
- end
14
-
15
- def self.create(path = nil, log_level = :debug)
16
- setup(path, log_level)
17
-
18
- DataMapper.auto_migrate!
19
- end
20
-
21
- def self.update(path = nil, log_level = :debug)
22
- setup(path, log_level)
23
-
24
- (PlayWhe::BIRTHDAY.year..Date.today.year).each do |year|
25
- task = Task.first_or_create({ year: year },
26
- { month: year == PlayWhe::BIRTHDAY.year ? PlayWhe::BIRTHDAY.month : 1,
27
- status: 'pending' })
28
-
29
- unless task.completed?
30
- DataMapper.logger << "retrieving results for year #{year}..."
31
-
32
- before = Proc.new do |month|
33
- if month < task.month
34
- DataMapper.logger << " - skipping #{Date::MONTHNAMES[month]}"
35
- true # skip
36
- else
37
- DataMapper.logger << " + at #{Date::MONTHNAMES[month]}..."
38
- false # do not skip
39
- end
40
- end
41
-
42
- after = Proc.new do |month, flag, results|
43
- case flag
44
- when :ok
45
- Result.transaction do |t|
46
- begin
47
- results.each do |r|
48
- result = Result.first_or_new(draw: r[:draw], date: r[:date], period: r[:period], mark: r[:mark])
49
-
50
- unless result.save
51
- DataMapper.logger << " - invalid data: #{result.draw} #{result.date} #{result.period} #{result.mark}"
52
- Error.create(message: r.to_s)
53
- end
54
- end
55
-
56
- today = Date.today
57
- if Date.new(year, month) < Date.new(today.year, today.month)
58
- if month == 12
59
- task.status = 'completed'
60
- else
61
- task.month += 1
62
- end
63
-
64
- if task.save
65
- DataMapper.logger << " + completed #{Date::MONTHNAMES[month]}!"
66
- DataMapper.logger << "completed year #{year}!" if task.completed?
67
-
68
- true # continue
69
- else
70
- DataMapper.logger << ' - task not completed!'
71
-
72
- false # do not continue
73
- end
74
- else
75
- DataMapper.logger << 'All tasks completed!'
76
-
77
- false # do not continue
78
- end
79
- rescue
80
- t.rollback
81
-
82
- DataMapper.logger << 'Encountered an error during processing'
83
-
84
- false # do not continue
85
- end
86
- end
87
- when :error
88
- DataMapper.logger << 'Encountered an error during processing'
89
-
90
- false # do not continue
91
- end
92
- end
93
-
94
- PlayWhe.results_for_year(year, before: before, after: after)
95
- end
96
- end
97
- end
98
-
99
- private
100
-
101
- def self.setup(path, log_level)
102
- path ||= File.join(Dir.home, '.playwhe')
103
- path = File.expand_path(path)
104
- Dir.mkdir(path) unless File.directory?(path)
105
-
106
- self.connect("#{path}/playwhe.db", log_level)
107
- end
108
- end
109
- end
@@ -1,92 +0,0 @@
1
- require 'data_mapper'
2
-
3
- module PlayWhe
4
-
5
- module Storage
6
-
7
- DataMapper::Property.required(true)
8
-
9
- class Result
10
- include DataMapper::Resource
11
-
12
- storage_names[:default] = 'results'
13
-
14
- property :draw, Integer, key: true
15
- property :date, Date
16
- property :period, Integer
17
- property :mark, Integer
18
-
19
- validates_with_method :draw, method: :check_draw
20
- validates_within :period, set: 1..3
21
- validates_within :mark, set: PlayWhe::LOWEST_MARK..PlayWhe::HIGHEST_MARK
22
-
23
- def self.all_by_day(year, month, day)
24
- self.all(:date => Date.new(year, month, day))
25
- end
26
-
27
- def self.all_by_month(year, month)
28
- a = Date.new year, month, 1 # start of the month
29
- b = (a >> 1) - 1 # end of the month
30
-
31
- self.all(:date => a..b)
32
- end
33
-
34
- def self.all_by_year(year)
35
- a = Date.new year, 1, 1 # start of the year
36
- b = (a >> 12) - 1 # end of the year
37
-
38
- self.all(:date => a..b)
39
- end
40
-
41
- private
42
-
43
- def check_draw
44
- self.draw >= 1 ? true : [false, 'The draw must be a positive integer']
45
- end
46
- end
47
-
48
- class Task
49
- include DataMapper::Resource
50
-
51
- storage_names[:default] = 'tasks'
52
-
53
- property :id, Serial
54
- property :year, Integer, unique: true
55
- property :month, Integer
56
- property :status, String
57
- property :created_at, DateTime, required: false
58
- property :updated_at, DateTime, required: false
59
-
60
- validates_with_method :year, method: :check_year
61
- validates_within :month, set: 1..12
62
- validates_within :status, set: ['pending', 'completed']
63
-
64
- def completed?
65
- self.status == 'completed'
66
- end
67
-
68
- private
69
-
70
- def check_year
71
- if self.year >= PlayWhe::BIRTHDAY.year && self.year <= Date.today.year
72
- true
73
- else
74
- [false, 'The year is out of range']
75
- end
76
- end
77
- end
78
-
79
- class Error
80
- include DataMapper::Resource
81
-
82
- storage_names[:default] = 'errors'
83
-
84
- property :id, Serial
85
- property :message, Text
86
- property :created_at, DateTime, required: false
87
- property :updated_at, DateTime, required: false
88
- end
89
-
90
- DataMapper.finalize
91
- end
92
- end