Class: Ali::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/ali/task.rb

Direct Known Subclasses

Ali::Tasks::Help, Ali::Tasks::Version

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTask

Returns a new instance of Task.



54
55
56
# File 'lib/ali/task.rb', line 54

def initialize
  @template = Ali::Template.new(self)
end

Instance Attribute Details

#templateObject (readonly)

Returns the value of attribute template.



52
53
54
# File 'lib/ali/task.rb', line 52

def template
  @template
end

Class Method Details

.description(value = nil) ⇒ Object



16
17
18
19
# File 'lib/ali/task.rb', line 16

def description(value = nil)
  return @description.to_s if value.nil?
  @description = value
end

.inherited(subclass) ⇒ Object



5
6
7
# File 'lib/ali/task.rb', line 5

def inherited(subclass)
  Ali::Registry.instance.register subclass
end

.inspectObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/ali/task.rb', line 31

def inspect
  value = <<~INSPECT
    #<#{name}
      name=#{name(for: :task).inspect}
      monikers=#{monikers.inspect}
      description=#{description.to_s.squish.inspect}
      options=#{options.inspect}>
  INSPECT
  value.squish
end

.monikers(*values) ⇒ Object



21
22
23
24
# File 'lib/ali/task.rb', line 21

def monikers(*values)
  return @monikers.to_a if values.none?
  @monikers ||= Set.new(values.flatten.map(&:to_s))
end

.name(options = {for: :class}) ⇒ Object



9
10
11
12
13
14
# File 'lib/ali/task.rb', line 9

def name(options = {for: :class})
  return super() if options[:for] == :class
  super()
    .then { |it| it.delete_prefix("Ali::Tasks::") }
    .then { |it| it.split("::").map(&:parameterize).join(":") }
end

.options(*values) ⇒ Object



26
27
28
29
# File 'lib/ali/task.rb', line 26

def options(*values)
  return @options.to_a if values.none?
  @options ||= Set.new(values.flatten.select { |it| it.is_a?(Ali::Option) })
end

.to_hObject



42
43
44
45
46
47
48
49
# File 'lib/ali/task.rb', line 42

def to_h
  {
    name: name(for: :task),
    monikers: monikers,
    description: description
    # options: options
  }.stringify_keys
end

Instance Method Details

#perform(*args) ⇒ Object

Raises:

  • (NotImplementedError)


58
59
60
# File 'lib/ali/task.rb', line 58

def perform(*args)
  raise NotImplementedError, "#perform must be implemented by subclass"
end