Class: Roda::Project::Bin::Generators::Migration::ActionDetector

Inherits:
Object
  • Object
show all
Includes:
Helpers::Inflections
Defined in:
lib/roda/project/bin/generators/migration/action_detector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#argsObject (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

#nameObject (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

#detectObject



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