Module: Chusaku

Defined in:
lib/chusaku.rb,
lib/chusaku/cli.rb,
lib/chusaku/parser.rb,
lib/chusaku/routes.rb,
lib/chusaku/version.rb

Overview

Handles core functionality of annotating projects.

Defined Under Namespace

Modules: Parser Classes: CLI, Routes

Constant Summary collapse

DEFAULT_CONTROLLERS_PATTERN =
"**/*_controller.rb".freeze
VERSION =
"1.3.0"

Class Method Summary collapse

Class Method Details

.call(flags = {}) ⇒ Integer

The main method to run Chusaku. Annotate all actions in a Rails project as follows:

# @route GET /waterlilies/:id (waterlilies)
def show
  # ...
end

Parameters:

  • flags (Hash) (defaults to: {})

    CLI flags

Returns:

  • (Integer)

    0 on success, 1 on error



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/chusaku.rb', line 20

def call(flags = {})
  @flags = flags
  @routes = Chusaku::Routes.call
  @changed_files = []
  controllers_pattern = @flags[:controllers_pattern] || DEFAULT_CONTROLLERS_PATTERN
  controllers_paths = Dir.glob(Rails.root.join(controllers_pattern))

  @routes.each do |controller, actions|
    next unless controller

    controller_class = "#{controller.underscore.camelize}Controller".constantize
    action_method_name = actions.keys.first&.to_sym
    next unless !action_method_name.nil? && controller_class.method_defined?(action_method_name)

    source_path = controller_class.instance_method(action_method_name).source_location&.[](0)
    next unless controllers_paths.include?(source_path)

    annotate_file(path: source_path, actions: actions)
  end

  output_results
end

.load_tasksvoid

This method returns an undefined value.

Load Rake tasks for Chusaku. Should be called in your project’s ‘Rakefile`.



46
47
48
49
50
# File 'lib/chusaku.rb', line 46

def load_tasks
  Dir[File.join(File.dirname(__FILE__), "tasks", "**/*.rake")].each do |task|
    load(task)
  end
end