Class: Planter::Generators::AdapterGenerator

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

Overview

Rails generator that creates a custom adapter stub and configures Planter to use it.

Instance Method Summary collapse

Instance Method Details

#generate_adapterObject

Create a custom adapter file with the required adapter API.



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
78
79
80
# File 'lib/generators/planter/adapter_generator.rb', line 15

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

    module Planter
      module Adapters
        ##
        # Custom table-oriented adapter for Planter.
        class #{adapter_class_name}
          ##
          # Create a record unless one already exists.
          #
          # @param [Planter::SeedContext] context seeder configuration
          #
          # @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(context:, lookup_attributes:, create_attributes:)
            raise NotImplementedError, "\#{self.class} must implement #create_record"
          end

          ##
          # Return the parent ids to use when seeding child records.
          #
          # @param [Planter::SeedContext] context seeder configuration
          #
          # @return [Array]
          def parent_ids(context:)
            raise NotImplementedError, "\#{self.class} must implement #parent_ids"
          end

          ##
          # Return the foreign key used to assign a parent id on a child record.
          #
          # @param [Planter::SeedContext] context seeder configuration
          #
          # @return [String, Symbol]
          def foreign_key(context:)
            raise NotImplementedError, "\#{self.class} must implement #foreign_key"
          end

          ##
          # Return native columns or fields for the table being seeded.
          #
          # @param [Planter::SeedContext] context seeder configuration
          #
          # @return [Array<String>]
          def table_columns(context:)
            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

Update the Planter initializer to require and configure the new adapter.



84
85
86
87
88
89
90
# File 'lib/generators/planter/adapter_generator.rb', line 84

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