Class: ExclusiveArcGenerator

Inherits:
ActiveRecord::Generators::Base
  • Object
show all
Defined in:
lib/generators/exclusive_arc_generator.rb

Constant Summary collapse

Error =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ ExclusiveArcGenerator

Returns a new instance of ExclusiveArcGenerator.

Raises:



16
17
18
19
# File 'lib/generators/exclusive_arc_generator.rb', line 16

def initialize(*args)
  raise Error, "must supply a Model, arc, and at least two belong_tos" if args[0].size <= 3
  super
end

Instance Method Details

#create_exclusive_arc_migrationObject



21
22
23
24
25
26
# File 'lib/generators/exclusive_arc_generator.rb', line 21

def create_exclusive_arc_migration
  migration_template(
    "migration.rb.erb",
    "db/migrate/#{migration_file_name}"
  )
end

#inject_exclusive_arc_into_modelObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/generators/exclusive_arc_generator.rb', line 28

def inject_exclusive_arc_into_model
  indents = "  " * (class_name.scan("::").count + 1)
  inject_into_class(
    model_file_path,
    class_name.demodulize,
    <<~RB
      #{indents}include ExclusiveArc::Model
    RB
  )
  gsub_file(
    model_file_path,
    /has_exclusive_arc :#{arc}(.*)$/,
    ""
  )
  inject_into_file(
    model_file_path,
    <<~RB,
      #{indents}has_exclusive_arc #{model_exclusive_arcs}
    RB
    after: "include ExclusiveArc::Model\n"
  )
end