Class: MakeTestCommand

Inherits:
MoreUtils show all
Defined in:
lib/commands/make_test.rb

Class Method Summary collapse

Methods inherited from MoreUtils

flag_lookup, gem_version, get_args, get_file_str, get_flags, root, this_dir, versions, write_file, wrong_version_error

Class Method Details

.run(*args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/commands/make_test.rb', line 7

def run *args
    lookup = flag_lookup(args)
    arguments = get_args(args)

    namespace_regex = '{{ NAMESPACE }}'
    model_regex = '{{ MODEL }}'
    routes_regex = '{{ ROUTE }}'
    namespace_snakecase_regex = '{{ NAMESPACE_SNAKECASE }}'

    api_route = arguments[0]
    namespace_array = api_route.split('/')
    model = namespace_array.pop
    namespace_array = namespace_array.reject { |e| e == '' }
    namespace = namespace_array.map { |e| e.downcase.capitalize }.join('::') + '::' + model.camelcase
    namespace_snake = (namespace_array.join('_') + '_' + model).underscore

    system("mkdir -p #{root}/test/controllers/#{namespace_array.join('/')}")
    test_template = get_file_str("#{this_dir}/../templates/mini_test_controller.txt")
    test_template = test_template.gsub(namespace_regex, namespace)
    test_template = test_template.gsub(model_regex, model)
    test_template = test_template.gsub(namespace_snakecase_regex, namespace_snake)
    test_template = test_template.gsub(routes_regex, api_route)
    write_file("#{root}/test/controllers#{api_route}_controller_test.rb", test_template)

    puts "Genreated your unit for api route: #{api_route}"
end