Class: RbsActiverecord::RakeTask

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/rbs_activerecord/rake_task.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = :'rbs:activerecord', &block) ⇒ RakeTask

Returns a new instance of RakeTask.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rbs_activerecord/rake_task.rb', line 11

def initialize(name = :'rbs:activerecord', &block) #: void
  super()

  @name = name
  @signature_root_dir = Rails.root / "sig/activerecord"

  block&.call(self)

  define_setup_task
  define_clean_task
  define_generate_task
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/rbs_activerecord/rake_task.rb', line 7

def name
  @name
end

#signature_root_dirObject

Returns the value of attribute signature_root_dir.



7
8
9
# File 'lib/rbs_activerecord/rake_task.rb', line 7

def signature_root_dir
  @signature_root_dir
end

Instance Method Details

#define_clean_taskObject

: void



31
32
33
34
35
36
# File 'lib/rbs_activerecord/rake_task.rb', line 31

def define_clean_task #: void
  desc "Clean RBS files for ActiveRecord models"
  task "#{name}:clean" do
    signature_root_dir.rmtree if signature_root_dir.exist?
  end
end

#define_generate_taskObject

: void # rubocop:disable Metrics/AbcSize



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rbs_activerecord/rake_task.rb', line 38

def define_generate_task #: void  # rubocop:disable Metrics/AbcSize
  desc "Generate RBS files for ActiveRecord models"
  task("#{name}:generate": :environment) do
    require "rbs_activerecord" # load RbsActiverecord lazily

    Rails.application.eager_load!

    signature_root_dir.mkpath

    ActiveRecord::Base.descendants.each do |klass|
      next if klass.abstract_class?
      next unless klass.connection.table_exists?(klass.table_name)

      rbs = Generator.new(klass).generate
      next unless rbs

      path = signature_root_dir / "app/models/#{klass.name.underscore}.rbs"
      path.dirname.mkpath
      path.write(rbs)
    end
  end
end

#define_setup_taskObject

: void



24
25
26
27
28
29
# File 'lib/rbs_activerecord/rake_task.rb', line 24

def define_setup_task #: void
  desc "Run all tasks of rbs_activerecord"

  deps = [:"#{name}:clean", :"#{name}:generate"]
  task("#{name}:setup" => deps)
end