Created
February 10, 2017 15:28
-
-
Save wconrad/e37f0e25237008655f181bc5f7d7f304 to your computer and use it in GitHub Desktop.
Example of using polymorphism to replace switch statement
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
module PercyDbMigrate | |
module PrereqPolicy | |
# A prereq policy that migrates both selected classes and | |
# prerequesites. | |
class All | |
# Return the migration classes to run. | |
# | |
# @return [MigrationClasses] | |
def migration_classes(selected_migration_classes) | |
selected_migration_classes + selected_migration_classes.dependencies | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@prereq_policy_name = PrereqPolicy.default_name | |
... | |
op.on("-p S", PrereqPolicy.names, | |
"Prerequesite policy (default #{@prereq_policy})", | |
"One of #{PrereqPolicy.names.join(", ")}", | |
) do |v| | |
@prereq_policy_name = v | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create a policy based on its name given in a command-line switch | |
@prereq_policy = PrereqPolicy[@args.prereq_policy_name] | |
... | |
# use the policy to select which migrations to run | |
@migration_classes = @prereq_policy.migration_classes(selected_migration_classes) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
require "active_support" | |
module PercyDbMigrate | |
module PrereqPolicy | |
# A prereq policy that migrates no prerequisites. | |
class None | |
# Return the migration classes to run. | |
# | |
# @return [MigrationClasses] | |
def migration_classes(selected_migration_classes) | |
selected_migration_classes | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
require "active_support" | |
module PercyDbMigrate | |
module PrereqPolicy | |
# A prereq policy that migrates only prerequisites. | |
class Only | |
# Return the migration classes to run. | |
# | |
# @return [MigrationClasses] | |
def migration_classes(selected_migration_classes) | |
selected_migration_classes.dependencies | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
require "active_support" | |
module PercyDbMigrate | |
# Namespace and factory for various prerequesite policies. | |
# | |
# A prerequest policy determines what tables to fetch when | |
# migrations are being run. | |
module PrereqPolicy | |
extend ActiveSupport::Autoload | |
autoload :All | |
autoload :None | |
autoload :Only | |
# Map names to policies | |
MAP = { | |
"all" => All, | |
"none" => None, | |
"only" => Only, | |
} | |
private_constant :MAP | |
# Return the name of the default policy | |
def self.default_name | |
"all" | |
end | |
# Return the names of all policies | |
def self.names | |
MAP.keys | |
end | |
# Return the named policy. | |
def self.[](name) | |
MAP.fetch(name).new | |
end | |
# Return the default policy. | |
def self.default | |
self[default_name] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment