Class: Crosstest::Psychic::MagicTaskFactory

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Core::Logger
Defined in:
lib/crosstest/psychic/magic_task_factory.rb

Constant Summary collapse

TASK_PRIORITY =
5

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(psychic, opts = {}) ⇒ MagicTaskFactory

Returns a new instance of MagicTaskFactory


48
49
50
51
52
53
54
# File 'lib/crosstest/psychic/magic_task_factory.rb', line 48

def initialize(psychic, opts = {})
  @psychic = psychic
  @opts = opts
  @priority = self.class::TASK_PRIORITY
  init_attr(:known_tasks) { self.class.known_tasks }
  init_attr(:tasks) { self.class.tasks }
end

Instance Attribute Details

#hintsObject (readonly)

Returns the value of attribute hints


10
11
12
# File 'lib/crosstest/psychic/magic_task_factory.rb', line 10

def hints
  @hints
end

#known_tasksObject (readonly)

Returns the value of attribute known_tasks


10
11
12
# File 'lib/crosstest/psychic/magic_task_factory.rb', line 10

def known_tasks
  @known_tasks
end

#priorityObject (readonly)

Returns the value of attribute priority


10
11
12
# File 'lib/crosstest/psychic/magic_task_factory.rb', line 10

def priority
  @priority
end

#psychicObject (readonly)

Returns the value of attribute psychic


10
11
12
# File 'lib/crosstest/psychic/magic_task_factory.rb', line 10

def psychic
  @psychic
end

#tasksObject (readonly)

Returns the value of attribute tasks


10
11
12
# File 'lib/crosstest/psychic/magic_task_factory.rb', line 10

def tasks
  @tasks
end

Class Method Details

.known_tasksObject


33
34
35
# File 'lib/crosstest/psychic/magic_task_factory.rb', line 33

def known_tasks
  @known_tasks ||= []
end

.magic_env_var(var) ⇒ Object


29
30
31
# File 'lib/crosstest/psychic/magic_task_factory.rb', line 29

def magic_env_var(var)
  magic_env_vars << var
end

.magic_env_varsObject


25
26
27
# File 'lib/crosstest/psychic/magic_task_factory.rb', line 25

def magic_env_vars
  @magic_env_vars ||= []
end

.magic_file(pattern) ⇒ Object

rubocop:disable Style/TrivialAccessors


21
22
23
# File 'lib/crosstest/psychic/magic_task_factory.rb', line 21

def magic_file(pattern) # rubocop:disable Style/TrivialAccessors
  magic_file_patterns << pattern
end

.magic_file_patternsObject


17
18
19
# File 'lib/crosstest/psychic/magic_task_factory.rb', line 17

def magic_file_patterns
  @magic_file_patterns ||= []
end

.register_task_factoryObject


13
14
15
# File 'lib/crosstest/psychic/magic_task_factory.rb', line 13

def register_task_factory
  Crosstest::Psychic::TaskFactoryManager.register_factory(self)
end

.task(name, &block) ⇒ Object


41
42
43
44
45
# File 'lib/crosstest/psychic/magic_task_factory.rb', line 41

def task(name, &block)
  name = name.to_s
  tasks[name] = block
  known_tasks << name
end

.tasksObject


37
38
39
# File 'lib/crosstest/psychic/magic_task_factory.rb', line 37

def tasks
  @tasks ||= {}
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)

64
65
66
67
68
69
70
71
72
# File 'lib/crosstest/psychic/magic_task_factory.rb', line 64

def active?
  self.class.magic_file_patterns.each do | pattern |
    return true unless Dir.glob("#{cwd}/#{pattern}", File::FNM_CASEFOLD).empty?
  end
  self.class.magic_env_vars.each do | var |
    return true if ENV[var]
  end
  false
end

#command_for_task(task_alias, *_args) ⇒ Object


56
57
58
59
60
61
62
# File 'lib/crosstest/psychic/magic_task_factory.rb', line 56

def command_for_task(task_alias, *_args)
  task_alias = task_alias.to_s
  task = tasks[task_alias] if tasks.include? task_alias
  task = task.call if task.respond_to? :call
  fail Crosstest::Psychic::TaskNotImplementedError, task_alias if task.nil?
  task
end

#known_task?(task_alias) ⇒ Boolean

Returns:

  • (Boolean)

74
75
76
# File 'lib/crosstest/psychic/magic_task_factory.rb', line 74

def known_task?(task_alias)
  known_tasks.include?(task_alias.to_s)
end

#priority_for_task(task_alias) ⇒ Object


78
79
80
# File 'lib/crosstest/psychic/magic_task_factory.rb', line 78

def priority_for_task(task_alias)
  priority if known_task? task_alias
end