Class: CustomSeeds::SeedFile

Inherits:
Object
  • Object
show all
Defined in:
lib/custom_seeds/seed_file.rb

Constant Summary collapse

MAP =
{
  blue: 34,
  green: 32
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSeedFile

Returns a new instance of SeedFile.



19
20
21
22
23
24
25
# File 'lib/custom_seeds/seed_file.rb', line 19

def initialize
  @dry_run = self.class.options[:dry_run]
  @verbose = self.class.options[:verbose]
  @records = []
  @build_block = ->(_record) { raise 'No build block defined' }
  @log_block = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, **_args) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/custom_seeds/seed_file.rb', line 87

def method_missing(method_name, **_args)
  if @memoized&.key?(method_name)
    @memoized[method_name]
  else
    super
  end
end

Instance Attribute Details

#build_blockObject (readonly)

Returns the value of attribute build_block.



5
6
7
# File 'lib/custom_seeds/seed_file.rb', line 5

def build_block
  @build_block
end

#log_blockObject (readonly)

Returns the value of attribute log_block.



5
6
7
# File 'lib/custom_seeds/seed_file.rb', line 5

def log_block
  @log_block
end

Class Method Details

.define(&block) ⇒ Object



12
13
14
15
16
17
# File 'lib/custom_seeds/seed_file.rb', line 12

def self.define(&block)
  new.tap do |seed_file|
    seed_file.instance_eval(&block)
    seed_file.run
  end
end

.options(value = nil) ⇒ Object



27
28
29
30
31
# File 'lib/custom_seeds/seed_file.rb', line 27

def self.options(value = nil)
  return @options = value if value

  @options ||= {}
end

Instance Method Details

#after(&block) ⇒ Object



70
71
72
73
74
# File 'lib/custom_seeds/seed_file.rb', line 70

def after(&block)
  return @after_block if block.nil?

  @after_block = block
end

#before(&block) ⇒ Object



64
65
66
67
68
# File 'lib/custom_seeds/seed_file.rb', line 64

def before(&block)
  return @before_block if block.nil?

  @before_block = block
end

#colorize(string, color_code) ⇒ Object



41
42
43
# File 'lib/custom_seeds/seed_file.rb', line 41

def colorize(string, color_code)
  "\e[#{MAP[color_code]}m#{string}\e[0m"
end

#description(value) ⇒ Object



49
50
51
# File 'lib/custom_seeds/seed_file.rb', line 49

def description(value)
  puts colorize(value, :blue)
end

#dry_run?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/custom_seeds/seed_file.rb', line 33

def dry_run?
  @dry_run
end

#each_record(&block) ⇒ Object



58
59
60
61
62
# File 'lib/custom_seeds/seed_file.rb', line 58

def each_record(&block)
  return @build_block if block.nil?

  @build_block = block
end

#let(name, &block) ⇒ Object

memoize block



77
78
79
80
81
# File 'lib/custom_seeds/seed_file.rb', line 77

def let(name, &block)
  @memoized ||= {}

  @memoized[name] ||= block.call
end

#log_each(&block) ⇒ Object



95
96
97
98
99
# File 'lib/custom_seeds/seed_file.rb', line 95

def log_each(&block)
  return @log_block if block.nil?

  @log_block = block
end

#log_sql_statements(&block) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/custom_seeds/seed_file.rb', line 101

def log_sql_statements(&block)
  sql_statements = []
  return block.call unless verbose?

  subscriber = ActiveSupport::Notifications.subscribe('sql.active_record') do |_, _, _, _, details|
    sql_statements << details[:sql] unless details[:name] == 'SCHEMA'
  end

  block.call
ensure
  ActiveSupport::Notifications.unsubscribe(subscriber)
  sql_statements.each { |sql| puts sql }
end

#records(&block) ⇒ Object



53
54
55
56
# File 'lib/custom_seeds/seed_file.rb', line 53

def records(&block)
  return @records if block.nil?
  @records = block.call
end

#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/custom_seeds/seed_file.rb', line 83

def respond_to_missing?(method_name, _include_private = false)
  @memoized&.key?(method_name) || super
end

#runObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/custom_seeds/seed_file.rb', line 127

def run
  log_sql_statements do
    transaction do
      before&.call
      progress_bar = ProgressBar.new(records.size)

      records.each do |record|
        build_block.call(record)

        if log_block
          puts colorize(log_block.call(record), :blue)
        else
          progress_bar.increment!
        end
      end

      after&.call
      puts colorize('✅ Seeding completed', :green)
    end
  end
end

#title(value) ⇒ Object



45
46
47
# File 'lib/custom_seeds/seed_file.rb', line 45

def title(value)
  puts colorize("🌱 #{value}", :green)
end

#transaction(&block) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/custom_seeds/seed_file.rb', line 115

def transaction(&block)
  puts colorize('🚧 Dry Run. No changes will be applied.', :green) if dry_run?

  ActiveRecord::Base.transaction do
    block.call

    raise ActiveRecord::Rollback if dry_run?
  end

  puts colorize('🚧 Dry run complete', :green) if dry_run?
end

#verbose?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/custom_seeds/seed_file.rb', line 37

def verbose?
  @verbose
end