Class: INat::App::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/inat/app/task.rb,
lib/inat/app/task/dsl.rb

Defined Under Namespace

Modules: DSL Classes: Context

Constant Summary collapse

CHECK_LIST =
%w[ . .inat .rb ]
FILE_CHECK_LIST =
%w[ .inat .iNat .INat .INAT .rb .RB ]

Instance Method Summary collapse

Constructor Details

#initialize(application, source) ⇒ Task

Returns a new instance of Task.

Raises:

  • (ArgumentError)


63
64
65
66
67
68
# File 'lib/inat/app/task.rb', line 63

def initialize application, source
  @application = application
  @basename, @path = get_names source
  raise ArgumentError, "File not found: #{source}!" if @path.nil?
  @context = Context::new self, @basename, @path
end

Instance Method Details

#configObject



47
48
49
# File 'lib/inat/app/task.rb', line 47

def config
  @application.config
end

#done?Boolean

Returns:



59
60
61
# File 'lib/inat/app/task.rb', line 59

def done?
  @context&.done?
end

#executeObject



70
71
72
73
# File 'lib/inat/app/task.rb', line 70

def execute
  G.current_task = self
  @context.execute
end

#existing(path) ⇒ Object (private)



13
14
15
16
17
18
19
# File 'lib/inat/app/task.rb', line 13

private def existing path
  if File.exist?(path)
    path
  else
    nil
  end
end

#get_names(source) ⇒ Object (private)

TODO: переписать более внятно



38
39
40
41
42
43
44
45
# File 'lib/inat/app/task.rb', line 38

private def get_names source
  path = File.expand_path source
  basename = File.basename(source, '.*')
  return [ basename, existing(path) ] if name_complete?(source)
  base = path.split('/')[..-2].join('/') + '/' + basename
  name = try_extensions base, *FILE_CHECK_LIST
  return [ basename, name ]
end

#loggerObject



51
52
53
# File 'lib/inat/app/task.rb', line 51

def logger
  @application.logger
end

#nameObject



55
56
57
# File 'lib/inat/app/task.rb', line 55

def name
  @context&.name
end

#name_complete?(source) ⇒ Boolean (private)

Returns:



29
30
31
32
33
34
35
# File 'lib/inat/app/task.rb', line 29

private def name_complete? source
  s = source.downcase
  CHECK_LIST.each do |check|
    return true if s.end_with?(check)
  end
  return false
end

#try_extensions(base, *extensions) ⇒ Object (private)



21
22
23
24
25
26
27
# File 'lib/inat/app/task.rb', line 21

private def try_extensions base, *extensions
  FILE_CHECK_LIST.each do |exception|
    path = base + exception
    return path if File.exist?(path)
  end
  return nil
end