Class: RbsActiveHash::RakeTask

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = :"rbs:active_hash", &block) ⇒ RakeTask

Returns a new instance of RakeTask.



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

def initialize(name = :"rbs:active_hash", &block)
  super()

  @name = name

  block&.call(self)

  setup_signature_root_dir!

  define_clean_task
  define_generate_task
  define_setup_task
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/rbs_active_hash/rake_task.rb', line 9

def name
  @name
end

#signature_root_dirObject

Returns the value of attribute signature_root_dir.



9
10
11
# File 'lib/rbs_active_hash/rake_task.rb', line 9

def signature_root_dir
  @signature_root_dir
end

Instance Method Details

#define_clean_taskObject



32
33
34
35
36
37
# File 'lib/rbs_active_hash/rake_task.rb', line 32

def define_clean_task
  desc "Clean up generated RBS files"
  task("#{name}:clean": :environment) do
    sh "rm", "-rf", @signature_root_dir.to_s
  end
end

#define_generate_taskObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rbs_active_hash/rake_task.rb', line 39

def define_generate_task
  desc "Generate RBS files for ActiveHash models"
  task("#{name}:generate": :environment) do
    require "rbs_active_hash" # load RbsActiveHash lazily

    Rails.application.eager_load!

    ::ActiveHash::Base.descendants.each do |klass|
      next unless RbsActiveHash::ActiveHash.user_defined_model?(klass)

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

#define_setup_taskObject



25
26
27
28
29
30
# File 'lib/rbs_active_hash/rake_task.rb', line 25

def define_setup_task
  desc "Run all tasks of rbs_active_hash"

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