Class: Roda::Project::Bin::Generators::Migration::ActionDetector
- Inherits:
-
Object
- Object
- Roda::Project::Bin::Generators::Migration::ActionDetector
- Includes:
- Helpers::Inflections
- Defined in:
- lib/roda/project/bin/generators/migration/action_detector.rb
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #detect ⇒ Object
-
#initialize(name, args: []) ⇒ ActionDetector
constructor
A new instance of ActionDetector.
Methods included from Helpers::Inflections
camelize, plural?, pluralize, singular?, singularize, underscore
Constructor Details
#initialize(name, args: []) ⇒ ActionDetector
Returns a new instance of ActionDetector.
13 14 15 16 |
# File 'lib/roda/project/bin/generators/migration/action_detector.rb', line 13 def initialize(name, args: []) @name = name.to_s @args = args || [] end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
11 12 13 |
# File 'lib/roda/project/bin/generators/migration/action_detector.rb', line 11 def args @args end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
11 12 13 |
# File 'lib/roda/project/bin/generators/migration/action_detector.rb', line 11 def name @name end |
Instance Method Details
#detect ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/roda/project/bin/generators/migration/action_detector.rb', line 18 def detect camel_name = camelize(name) if (m = camel_name.match(/^CreateJoinTable(?<t1>[A-Z][a-zA-Z0-9]*?)(?<t2>[A-Z][a-zA-Z0-9]*)$/)) || (m = camel_name.match(/JoinTable(?<t1>[A-Z][a-zA-Z0-9]*?)(?<t2>[A-Z][a-zA-Z0-9]*)$/)) || camel_name.start_with?("CreateJoinTable") || camel_name.include?("JoinTable") tables = parse_join_tables(m) {action: :create_join_table, tables: tables} elsif (m = camel_name.match(/^Create(?<table_name>[A-Z0-9].*)$/)) {action: :create_table, table_name: pluralize(underscore(m[:table_name]))} elsif (m = camel_name.match(/^Add.+To(?<table_name>[A-Z0-9].*)$/)) {action: :add_columns, table_name: pluralize(underscore(m[:table_name]))} elsif (m = camel_name.match(/^Remove.+From(?<table_name>[A-Z0-9].*)$/)) {action: :drop_columns, table_name: pluralize(underscore(m[:table_name]))} else {action: :generic, table_name: nil} end end |