Class: DayPlanner::Task

Inherits:
ActiveRecord::Base show all
Defined in:
app/models/day_planner/task.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ActiveRecord::Base

reset_table_sequence

Class Method Details

.schedule(options) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/models/day_planner/task.rb', line 7

def self.schedule(options)
	unless options[:every]
		raise ArgumentError, "Must specify task interval with :every, other scheduling methods are not yet implemented"
	end

	if options[:every].to_i < DayPlanner.interval.to_i
		raise ArgumentError, "Must specify a task interval at least as long as DayPlanner's check interval."
	end

	fields = {}
	fields[:name] = options.delete(:name).to_s if options[:name]
	fields[:interval] = options.delete(:every).to_i if options[:every]

	task = DayPlanner::Task.create(fields)
end

Instance Method Details

#blockObject



50
51
52
# File 'app/models/day_planner/task.rb', line 50

def block
	@block
end

#block=(block) ⇒ Object



54
55
56
# File 'app/models/day_planner/task.rb', line 54

def block=(block)
	@block = block
end

#check_nameObject



41
42
43
44
45
46
47
48
# File 'app/models/day_planner/task.rb', line 41

def check_name
	if name.present?
		tasks = Task.where(name: name)
		if tasks.count > 1 || (tasks.count == 1 && tasks.first.id != id)
			errors.add(:name, "must be unique if specified")
		end
	end
end

#log(last, now) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/models/day_planner/task.rb', line 23

def log(last, now)
	if ActiveRecord::Base.connection.table_exists?('day_planner_log')
		if !last.nil?
			deviation = -(last + interval - now)
		else
			deviation = 0
		end
		last_execution = DayPlanner::Log.where(name: self.name).last
		if !last_execution.nil?
			cumulative_deviation = last_execution.cumulative_deviation || 0
		else
			cumulative_deviation = 0
		end
		cumulative_deviation += deviation
		DayPlanner::Log.create(name: name, interval: interval, datetime: now, deviation: deviation, cumulative_deviation: cumulative_deviation)
	end
end