Class: Effective::Generators::RouteGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Includes:
Helpers
Defined in:
lib/generators/effective/route_generator.rb

Instance Method Summary collapse

Instance Method Details

#create_routeObject



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
# File 'lib/generators/effective/route_generator.rb', line 26

def create_route
  blocks = []

  Effective::CodeWriter.new(resource.routes_file) do |w|
    resource.namespaces.each do |namespace|
      index = nil

      w.within(blocks.last) do
        index = w.first { |line, depth| depth == 1 && line == "namespace :#{namespace} do"}
      end

      index ? (blocks << index) : break
    end

    content = resource.namespaces[blocks.length..-1].map { |namespace| "namespace :#{namespace} do"} +
      [resources].flatten + (['end'] * (resource.namespaces.length - blocks.length))

    w.within(blocks.last) do
      if content.length == 1 && w.find { |line, depth| depth == 1 && line == content.first }
        say_status :identical, content.first, :blue
      else
        w.insert_after_last(content, content_depth: 0) { |line, depth| depth == 1 && line.start_with?('resources') } ||
        w.insert_before_last(content, content_depth: 0) { |line, depth| depth == 1 && line.start_with?('root') } ||
        w.insert_before_last(content, content_depth: 0) { |line, depth| line == 'end' }

        say_status :route, content.join("\n\t\t")
      end
    end
  end
end

#invoke_routeObject



22
23
24
# File 'lib/generators/effective/route_generator.rb', line 22

def invoke_route
  say_status :invoke, :route, :white
end

#validate_resourceObject



18
19
20
# File 'lib/generators/effective/route_generator.rb', line 18

def validate_resource
  exit unless resource_valid?
end