Class: CustomSeeds::SeedFile
- Inherits:
-
Object
- Object
- CustomSeeds::SeedFile
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
Returns a new instance of SeedFile.
19
20
21
22
23
|
# File 'lib/custom_seeds/seed_file.rb', line 19
def initialize
@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
65
66
67
68
69
70
71
|
# File 'lib/custom_seeds/seed_file.rb', line 65
def method_missing(method_name, **_args)
if @memoized&.key?(method_name)
@memoized[method_name]
else
super
end
end
|
Instance Attribute Details
#build_block ⇒ Object
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_block ⇒ Object
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
|
Instance Method Details
#before(&block) ⇒ Object
48
49
50
51
52
|
# File 'lib/custom_seeds/seed_file.rb', line 48
def before(&block)
return @before_block if block.nil?
@before_block = block
end
|
#colorize(string, color_code) ⇒ Object
25
26
27
|
# File 'lib/custom_seeds/seed_file.rb', line 25
def colorize(string, color_code)
"\e[#{MAP[color_code]}m#{string}\e[0m"
end
|
#description(value) ⇒ Object
33
34
35
|
# File 'lib/custom_seeds/seed_file.rb', line 33
def description(value)
puts colorize(value, :blue)
end
|
#each_record(&block) ⇒ Object
42
43
44
45
46
|
# File 'lib/custom_seeds/seed_file.rb', line 42
def each_record(&block)
return @build_block if block.nil?
@build_block = block
end
|
#let(name, &block) ⇒ Object
55
56
57
58
59
|
# File 'lib/custom_seeds/seed_file.rb', line 55
def let(name, &block)
@memoized ||= {}
@memoized[name] ||= block.call
end
|
#log_each(&block) ⇒ Object
73
74
75
76
77
|
# File 'lib/custom_seeds/seed_file.rb', line 73
def log_each(&block)
return @log_block if block.nil?
@log_block = block
end
|
#records(&block) ⇒ Object
37
38
39
40
|
# File 'lib/custom_seeds/seed_file.rb', line 37
def records(&block)
return @records if block.nil?
@records = block.call
end
|
#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean
61
62
63
|
# File 'lib/custom_seeds/seed_file.rb', line 61
def respond_to_missing?(method_name, _include_private = false)
@memoized&.key?(method_name) || super
end
|
#run ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/custom_seeds/seed_file.rb', line 79
def run
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
puts colorize('✅ Seeding completed', :green)
end
|
#title(value) ⇒ Object
29
30
31
|
# File 'lib/custom_seeds/seed_file.rb', line 29
def title(value)
puts colorize("🌱 #{value}", :green)
end
|