File: rails.rb

package info (click to toggle)
ruby-spring 2.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 428 kB
  • sloc: ruby: 3,373; sh: 9; makefile: 7
file content (112 lines) | stat: -rw-r--r-- 2,298 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
module Spring
  module Commands
    class Rails
      def call
        ARGV.unshift command_name
        load Dir.glob(::Rails.root.join("{bin,script}/rails")).first
      end

      def description
        nil
      end
    end

    class RailsConsole < Rails
      def env(args)
        return args.first if args.first && !args.first.index("-")

        environment = nil

        args.each.with_index do |arg, i|
          if arg =~ /--environment=(\w+)/
            environment = $1
          elsif i > 0 && args[i - 1] == "-e"
            environment = arg
          end
        end

        environment
      end

      def command_name
        "console"
      end
    end

    class RailsGenerate < Rails
      def command_name
        "generate"
      end
    end

    class RailsDestroy < Rails
      def command_name
        "destroy"
      end
    end

    class RailsRunner < Rails
      def call
        ARGV.replace extract_environment(ARGV).first
        super
      end

      def env(args)
        extract_environment(args).last
      end

      def command_name
        "runner"
      end

      def extract_environment(args)
        environment = nil

        args = args.select.with_index { |arg, i|
          case arg
          when "-e"
            false
          when /--environment=(\w+)/
            environment = $1
            false
          else
            if i > 0 && args[i - 1] == "-e"
              environment = arg
              false
            else
              true
            end
          end
        }

        [args, environment]
      end
    end

    class RailsTest < Rails
      def env(args)
        environment = "test"

        args.each.with_index do |arg, i|
          if arg =~ /--environment=(\w+)/
            environment = $1
          elsif i > 0 && args[i - 1] == "-e"
            environment = arg
          end
        end

        environment
      end

      def command_name
        "test"
      end
    end

    Spring.register_command "rails_console",  RailsConsole.new
    Spring.register_command "rails_generate", RailsGenerate.new
    Spring.register_command "rails_destroy",  RailsDestroy.new
    Spring.register_command "rails_runner",   RailsRunner.new
    Spring.register_command "rails_test",     RailsTest.new
  end
end