Class: RustOnBackground::Generators::JobGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/job/job_generator.rb

Constant Summary collapse

TYPE_MAPPINGS =
{
  # Ruby/Rails types
  "string" => "String", "text" => "String", "citext" => "String",
  "integer" => "i64", "int" => "i64", "i64" => "i64", "bigint" => "i64",
  "i32" => "i32",
  "float" => "f64", "decimal" => "f64", "f64" => "f64",
  "boolean" => "bool", "bool" => "bool",
  "array" => "Vec<serde_json::Value>", "vec" => "Vec<serde_json::Value>",
  "set" => "Vec<serde_json::Value>",
  "hash" => "serde_json::Value", "object" => "serde_json::Value",
  "json" => "serde_json::Value", "jsonb" => "serde_json::Value",
  "date" => "String", "datetime" => "String", "time" => "String", "timestamp" => "String",
  "uuid" => "String", "binary" => "Vec<u8>", "blob" => "Vec<u8>"
}.freeze

Instance Method Summary collapse

Instance Method Details

#add_dispatch_routeObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/generators/job/job_generator.rb', line 42

def add_dispatch_route
  mod_file = "app/jobs/rust/src/jobs/mod.rs"
  content = File.read(mod_file)

  new_content = content.sub(/^([ \t]*)unknown\s*=>/m) do |_match|
    indent = $1
    "#{indent}\"#{file_name}\" => {\n" \
    "#{indent}    #{file_name}::run(&job.args, database_url).await\n" \
    "#{indent}}\n\n" \
    "#{indent}unknown =>"
  end

  File.write(mod_file, new_content)
end

#add_module_declarationObject



34
35
36
37
38
39
40
# File 'lib/generators/job/job_generator.rb', line 34

def add_module_declaration
  mod_file = "app/jobs/rust/src/jobs/mod.rs"

  inject_into_file mod_file, after: "use crate::worker::{Job, JobResult};\n" do
    "pub mod #{file_name};\n"
  end
end

#create_job_fileObject



29
30
31
32
# File 'lib/generators/job/job_generator.rb', line 29

def create_job_file
  @args = parsed_attributes
  template "job.rs.erb", "app/jobs/rust/src/jobs/#{file_name}.rs"
end

#show_post_generate_messageObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/generators/job/job_generator.rb', line 57

def show_post_generate_message
  say ""
  say "Job '#{file_name}' created!", :green
  say ""
  say "Files:"
  say "  app/jobs/rust/src/jobs/#{file_name}.rs"
  say ""
  say "Usage in Rails:"
  say "  RustOnBackground.perform(\"#{file_name}\"#{usage_example})"
  say ""
  say "Next steps:"
  say "  1. Edit the job file to add your logic"
  say "  2. Run: cd app/jobs/rust && cargo build"
  say ""
end