class Fx::Generators::FunctionGenerator

@api private

Public Class Methods

next_migration_number(dir) click to toggle source
# File lib/generators/fx/function/function_generator.rb, line 42
def self.next_migration_number(dir)
  ::ActiveRecord::Generators::Base.next_migration_number(dir)
end

Public Instance Methods

activerecord_migration_class() click to toggle source
# File lib/generators/fx/function/function_generator.rb, line 65
def activerecord_migration_class
  if ActiveRecord::Migration.respond_to?(:current_version)
    "ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]"
  else
    "ActiveRecord::Migration"
  end
end
create_function_definition() click to toggle source
# File lib/generators/fx/function/function_generator.rb, line 19
def create_function_definition
  if creating_new_function?
    create_file definition.path
  else
    copy_file previous_definition.full_path, definition.full_path
  end
end
create_functions_directory() click to toggle source
# File lib/generators/fx/function/function_generator.rb, line 13
def create_functions_directory
  unless function_definition_path.exist?
    empty_directory(function_definition_path)
  end
end
create_migration_file() click to toggle source
# File lib/generators/fx/function/function_generator.rb, line 27
def create_migration_file
  return if skip_migration_creation?
  if updating_existing_function?
    migration_template(
      "db/migrate/update_function.erb",
      "db/migrate/update_function_#{file_name}_to_version_#{version}.rb"
    )
  else
    migration_template(
      "db/migrate/create_function.erb",
      "db/migrate/create_function_#{file_name}.rb"
    )
  end
end
formatted_name() click to toggle source
# File lib/generators/fx/function/function_generator.rb, line 73
def formatted_name
  if singular_name.include?(".")
    "\"#{singular_name}\""
  else
    ":#{singular_name}"
  end
end
migration_class_name() click to toggle source
Calls superclass method
# File lib/generators/fx/function/function_generator.rb, line 57
def migration_class_name
  if updating_existing_function?
    "UpdateFunction#{class_name}ToVersion#{version}"
  else
    super
  end
end
previous_version() click to toggle source
# File lib/generators/fx/function/function_generator.rb, line 47
def previous_version
  @_previous_version ||= Dir.entries(function_definition_path)
    .map { |name| version_regex.match(name).try(:[], "version").to_i }
    .max
end
version() click to toggle source
# File lib/generators/fx/function/function_generator.rb, line 53
def version
  @_version ||= previous_version.next
end

Private Instance Methods

creating_new_function?() click to toggle source
# File lib/generators/fx/function/function_generator.rb, line 96
def creating_new_function?
  previous_version == 0
end
definition() click to toggle source
# File lib/generators/fx/function/function_generator.rb, line 100
def definition
  Fx::Definition.new(name: file_name, version: version)
end
function_definition_path() click to toggle source
# File lib/generators/fx/function/function_generator.rb, line 84
def function_definition_path
  @_function_definition_path ||= Rails.root.join(*%w[db functions])
end
migration() click to toggle source

True unless explicitly false

# File lib/generators/fx/function/function_generator.rb, line 115
def migration
  options[:migration] != false
end
previous_definition() click to toggle source
# File lib/generators/fx/function/function_generator.rb, line 104
def previous_definition
  Fx::Definition.new(name: file_name, version: previous_version)
end
skip_migration_creation?() click to toggle source

Skip creating migration file if:

- migrations option is nil or false
# File lib/generators/fx/function/function_generator.rb, line 110
def skip_migration_creation?
  !migration
end
updating_existing_function?() click to toggle source
# File lib/generators/fx/function/function_generator.rb, line 92
def updating_existing_function?
  previous_version > 0
end
version_regex() click to toggle source
# File lib/generators/fx/function/function_generator.rb, line 88
def version_regex
  /\A#{file_name}_v(?<version>\d+)\.sql\z/
end