Class: Planter::Generators::AdapterGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/planter/adapter_generator.rb

Instance Method Summary collapse

Instance Method Details

#generate_adapterObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/generators/planter/adapter_generator.rb', line 8

def generate_adapter
  create_file adapter_path, <<~RUBY
    # frozen_string_literal: true

    module Planter
      module Adapters
        ##
        # Custom adapter for Planter.
        class #{adapter_class_name}
          ##
          # Create a record unless one already exists.
          #
          # @param [String] model_name the model being seeded
          #
          # @param [Hash] lookup_attributes attributes used to find the record
          #
          # @param [Hash] create_attributes additional attributes used only when
          #   creating a new record
          #
          # @return [Object]
          def create_record(model_name:, lookup_attributes:, create_attributes:)
            raise NotImplementedError, "\#{self.class} must implement #create_record"
          end

          ##
          # Return the parent ids to use when seeding child records.
          #
          # @param [String] model_name the model being seeded
          #
          # @param [String, Symbol] parent the parent association name
          #
          # @return [Array]
          def parent_ids(model_name:, parent:)
            raise NotImplementedError, "\#{self.class} must implement #parent_ids"
          end

          ##
          # Return the foreign key used to assign a parent id on a child record.
          #
          # @param [String] model_name the model being seeded
          #
          # @param [String, Symbol] parent the parent association name
          #
          # @return [String, Symbol]
          def foreign_key(model_name:, parent:)
            raise NotImplementedError, "\#{self.class} must implement #foreign_key"
          end

          ##
          # Return native columns or fields for the model being seeded.
          #
          # @param [String] model_name the model being seeded
          #
          # @return [Array<String>]
          def table_columns(model_name:)
            raise NotImplementedError, "\#{self.class} must implement #table_columns"
          end

          ##
          # Return table or collection names that can have seeders generated.
          #
          # @return [Array<String>]
          def table_names
            raise NotImplementedError, "\#{self.class} must implement #table_names"
          end
        end
      end
    end
  RUBY
end

#update_initializerObject



79
80
81
82
83
84
85
# File 'lib/generators/planter/adapter_generator.rb', line 79

def update_initializer
  contents = ::File.read(initializer_full_path)
  contents = replace_or_insert_adapter_require(contents)
  contents = replace_or_insert_adapter_config(contents)

  ::File.write(initializer_full_path, contents)
end