Class: PgCron::Definition Private

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_cron/definition.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Resolves a job name and version to its definition file.

Modelled on F(x)'s Fx::Definition: versioned SQL files, looked up first alongside any engine's migration paths and then in the app's own db/.

SQL rather than the YAML this gem used to read. A job's body is a SQL statement either way, so YAML meant a SQL string embedded in a YAML scalar — nothing could syntax-check it, dollar-quoting had to survive two levels of escaping, and the schema dumper could not round-trip it. A .sql file holds the cron.schedule() call verbatim.

Constant Summary collapse

JOB =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

"job"
DIRECTORY =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

db/cron, singular. F(x) derives its directory from the type ("function" -> db/functions) because it manages two kinds of object; this gem manages one, and "db/jobs" would read like Active Job's queues rather than the database's schedule.

"cron"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, version:, type: JOB) ⇒ Definition

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Definition.



29
30
31
32
33
# File 'lib/pg_cron/definition.rb', line 29

def initialize(name:, version:, type: JOB)
  @name = name
  @version_number = version.to_i
  @type = type
end

Class Method Details

.job(name:, version:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



25
26
27
# File 'lib/pg_cron/definition.rb', line 25

def self.job(name:, version:)
  new(name: name, version: version, type: JOB)
end

Instance Method Details

#full_path ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



42
43
44
# File 'lib/pg_cron/definition.rb', line 42

def full_path
  Rails.root.join(path)
end

#path ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



46
47
48
# File 'lib/pg_cron/definition.rb', line 46

def path
  @_path ||= File.join("db", DIRECTORY, filename)
end

#to_sql ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



35
36
37
38
39
40
# File 'lib/pg_cron/definition.rb', line 35

def to_sql
  content = File.read(find_file || full_path)
  raise "Define #{type} in #{path} before migrating." if content.empty?

  content
end

#version ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



50
51
52
# File 'lib/pg_cron/definition.rb', line 50

def version
  version_number.to_s.rjust(2, "0")
end