Module: DayPlanner

Defined in:
lib/day_planner/base.rb,
lib/day_planner/engine.rb,
lib/day_planner/version.rb,
app/models/day_planner/log.rb,
app/models/day_planner/task.rb,
lib/day_planner/generators/day_planner_generator.rb

Defined Under Namespace

Modules: Generators Classes: Log, Railtie, Task

Constant Summary collapse

MAJOR =
0
MINOR =
1
TINY =
0
PRE =
nil
BUILD =
nil
VERSION =
[MAJOR, MINOR, TINY, PRE, BUILD].compact.join(".")
@@status =
"stopped"
@@tasks =
[]

Class Method Summary collapse

Class Method Details

.activateObject


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/day_planner/base.rb', line 82

def activate
	@@master.kill if defined?(@@master)
	@@status = "running"

	if defined?(Rails) && Rails.logger
		Rails.logger.info("DayPlanner activated at #{Time.now.inspect}.")
	else
		puts "DayPlanner activated at #{Time.now.inspect}."
	end

	if ActiveRecord::Base.connection.table_exists?('day_planner_log')
		DayPlanner::Log.create(name: "Activating DayPlanner", interval: @@interval, datetime: Time.now)
	end

	@@master = Thread.new do
		begin
			while true
				check_schedule
				sleep(interval)
			end
		ensure
			Rails.logger.flush
		end
	end
end

.cancel(task) ⇒ Object

Raises:

  • (ArgumentError)

61
62
63
64
65
66
67
68
69
70
# File 'lib/day_planner/base.rb', line 61

def cancel(task)
	task = DayPlanner::Task.find_by_name(task) if task.is_a?(String) || task.is_a?(Symbol)
	task = DayPlanner::Task.find(task) if task.is_a?(Integer)

	raise ArgumentError, "DayPlanner couldn't find this task" if task.nil? || !task.is_a?(DayPlanner::Task)

	@@tasks.select!{ |t| t.id != task.id }

	task.destroy
end

.clear_logObject


41
42
43
44
# File 'lib/day_planner/base.rb', line 41

def clear_log
	ActiveRecord::Base.connection.execute("DELETE FROM #{DayPlanner::Log.table_name} WHERE true;")
	DayPlanner::Log.reset_table_sequence
end

.clear_tasksObject


35
36
37
38
39
# File 'lib/day_planner/base.rb', line 35

def clear_tasks
	@@tasks = []
	ActiveRecord::Base.connection.execute("DELETE FROM #{DayPlanner::Task.table_name} WHERE true;")
	DayPlanner::Task.reset_table_sequence
end

.deactivateObject


72
73
74
75
76
77
78
79
80
# File 'lib/day_planner/base.rb', line 72

def deactivate
	@@master.kill if defined?(@@master)
	@@status = "stopped"
	@@tasks = []

	clear_tasks

	true
end

.intervalObject


27
28
29
# File 'lib/day_planner/base.rb', line 27

def interval
	defined?(@@interval) ? @@interval : 60
end

.interval=(value) ⇒ Object


31
32
33
# File 'lib/day_planner/base.rb', line 31

def interval=(value)
	@@interval = value
end

.schedule(options, &block) ⇒ Object

Raises:

  • (ArgumentError)

46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/day_planner/base.rb', line 46

def schedule(options, &block)
	raise ArgumentError, "Failed to pass an options hash" unless options.is_a?(Hash)

	task = DayPlanner::Task.schedule(options)

	if !task.id.nil?
		task.block = block
		@@tasks.push(task)
	else
		raise ArgumentError, "Task creation failed. If you specified a name, was it unique?"
	end

	task
end

.statusObject


23
24
25
# File 'lib/day_planner/base.rb', line 23

def status
	@@status
end

.task(task) ⇒ Object


13
14
15
16
17
18
19
20
21
# File 'lib/day_planner/base.rb', line 13

def task(task)
	if task.is_a?(Integer)
		@@tasks.select{ |t| t.id == task }.first
	elsif task.is_a?(String)
		@@task.select{ |t| t.name == task }.first
	elsif task.is_a?(DayPlanner::Task)
		@@task.select{ |t| t.id == task.id }.first
	end
end

.tasksObject


9
10
11
# File 'lib/day_planner/base.rb', line 9

def tasks
	@@tasks
end