Module: MiniService::FileGenerator

Defined in:
lib/mini_service/file_generator.rb

Constant Summary collapse

DEFAULT_TEXT =
"private\n\ndef perform\n  # Put your code in here\n  return nil\nend\n"

Class Method Summary collapse

Class Method Details

.argument(arg) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/mini_service/file_generator.rb', line 52

def argument(arg)
  if arg[':']
    arg.gsub('=', ': ')
  else
    ":#{arg}"
  end
end

.arguments(args) ⇒ Object



48
49
50
# File 'lib/mini_service/file_generator.rb', line 48

def arguments(args)
  "arguments #{args.map(&method(:argument)).join(', ')}"
end

.asdf(args) ⇒ Object



44
45
46
# File 'lib/mini_service/file_generator.rb', line 44

def asdf(args)
  args.any? ? arguments(args) : nil
end

.class_end(name:, text: '', shift: 0) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/mini_service/file_generator.rb', line 75

def class_end(name:, text: '', shift: 0)
  [
    shift_text(text: "class #{name} < MiniService::Base", shift: shift),
    (text.size.zero? ? nil : shift_text(text: text, shift: shift + 2)),
    shift_text(text: 'end', shift: shift)
  ].compact.join("\n") + "\n"
end

.file(array, args = []) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/mini_service/file_generator.rb', line 28

def file(array, args = [])
  [
    '# frozen_string_literal: true',
    '',
    superduper(array, text_with_arguments(args))
  ].compact.join("\n")
end

.module_end(name:, text: '', shift: 0) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/mini_service/file_generator.rb', line 67

def module_end(name:, text: '', shift: 0)
  [
    "module #{name}".shift(shift: shift),
    shift_text(text: text, shift: shift + 2),
    shift_text(text: 'end', shift: shift)
  ].join("\n") + "\n"
end

.shift_text(text: '', separator: "\n", shift: 2) ⇒ Object



83
84
85
86
87
# File 'lib/mini_service/file_generator.rb', line 83

def shift_text(text: '', separator: "\n", shift: 2)
  text.split(separator).map do |line|
    (' ' * shift) + line unless line.size.zero?
  end.join(separator)
end

.superduper(array, text, mode = :class) ⇒ Object



60
61
62
63
64
65
# File 'lib/mini_service/file_generator.rb', line 60

def superduper(array, text, mode = :class)
  a = array.pop
  return text unless a

  superduper(array, super_text(a, mode, text), :module)
end

.text_with_arguments(args) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/mini_service/file_generator.rb', line 36

def text_with_arguments(args)
  [
    asdf(args) ? arguments(args) : nil,
    asdf(args) ? '' : nil,
    DEFAULT_TEXT
  ].compact.join("\n")
end