Class: Psychic::Runner::MagicTaskFactory
- Inherits:
-
Object
- Object
- Psychic::Runner::MagicTaskFactory
show all
- Includes:
- Logger
- Defined in:
- lib/psychic/runner/magic_task_factory.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Logger
#log_level=, #logger, #new_logger
Constructor Details
Returns a new instance of MagicTaskFactory.
44
45
46
47
48
49
50
51
|
# File 'lib/psychic/runner/magic_task_factory.rb', line 44
def initialize(opts = {})
@opts = opts
init_attr(:cwd) { Dir.pwd }
init_attr(:known_tasks) { self.class.known_tasks }
init_attr(:tasks) { self.class.tasks }
init_attr(:logger) { new_logger }
init_attr(:env) { ENV.to_hash }
end
|
Instance Attribute Details
#cwd ⇒ Object
Returns the value of attribute cwd.
6
7
8
|
# File 'lib/psychic/runner/magic_task_factory.rb', line 6
def cwd
@cwd
end
|
#env ⇒ Object
Returns the value of attribute env.
6
7
8
|
# File 'lib/psychic/runner/magic_task_factory.rb', line 6
def env
@env
end
|
#hints ⇒ Object
Returns the value of attribute hints.
6
7
8
|
# File 'lib/psychic/runner/magic_task_factory.rb', line 6
def hints
@hints
end
|
#known_tasks ⇒ Object
Returns the value of attribute known_tasks.
6
7
8
|
# File 'lib/psychic/runner/magic_task_factory.rb', line 6
def known_tasks
@known_tasks
end
|
#tasks ⇒ Object
Returns the value of attribute tasks.
6
7
8
|
# File 'lib/psychic/runner/magic_task_factory.rb', line 6
def tasks
@tasks
end
|
Class Method Details
.known_tasks ⇒ Object
29
30
31
|
# File 'lib/psychic/runner/magic_task_factory.rb', line 29
def known_tasks
@known_tasks ||= []
end
|
.magic_env_var(var) ⇒ Object
25
26
27
|
# File 'lib/psychic/runner/magic_task_factory.rb', line 25
def magic_env_var(var)
magic_env_vars << var
end
|
.magic_env_vars ⇒ Object
21
22
23
|
# File 'lib/psychic/runner/magic_task_factory.rb', line 21
def magic_env_vars
@magic_env_vars ||= []
end
|
.magic_file(pattern) ⇒ Object
rubocop:disable Style/TrivialAccessors
17
18
19
|
# File 'lib/psychic/runner/magic_task_factory.rb', line 17
def magic_file(pattern)
magic_file_patterns << pattern
end
|
.magic_file_patterns ⇒ Object
13
14
15
|
# File 'lib/psychic/runner/magic_task_factory.rb', line 13
def magic_file_patterns
@magic_file_patterns ||= []
end
|
.register_task_factory ⇒ Object
9
10
11
|
# File 'lib/psychic/runner/magic_task_factory.rb', line 9
def register_task_factory
Psychic::Runner::TaskFactoryRegistry.register(self)
end
|
.task(name, &block) ⇒ Object
37
38
39
40
41
|
# File 'lib/psychic/runner/magic_task_factory.rb', line 37
def task(name, &block)
name = name.to_s
tasks[name] = block
known_tasks << name
end
|
.tasks ⇒ Object
33
34
35
|
# File 'lib/psychic/runner/magic_task_factory.rb', line 33
def tasks
@tasks ||= {}
end
|
Instance Method Details
#active? ⇒ Boolean
65
66
67
68
69
70
71
72
73
|
# File 'lib/psychic/runner/magic_task_factory.rb', line 65
def active?
self.class.magic_file_patterns.each do | pattern |
return true unless Dir["#{@cwd}/#{pattern}"].empty?
end
self.class.magic_env_vars.each do | var |
return true if ENV[var]
end
false
end
|
#build_task(task_name, *_args) ⇒ Object
57
58
59
60
61
62
63
|
# File 'lib/psychic/runner/magic_task_factory.rb', line 57
def build_task(task_name, *_args)
task_name = task_name.to_s
task = task_for(task_name)
task = task.call if task.respond_to? :call
fail Psychic::Runner::TaskNotImplementedError, task_name if task.nil?
task
end
|
#known_task?(task_name) ⇒ Boolean
75
76
77
|
# File 'lib/psychic/runner/magic_task_factory.rb', line 75
def known_task?(task_name)
known_tasks.include?(task_name.to_s)
end
|
#task_for(task_name) ⇒ Object
53
54
55
|
# File 'lib/psychic/runner/magic_task_factory.rb', line 53
def task_for(task_name)
tasks[task_name] if tasks.include? task_name
end
|