seedie 0.2.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +91 -9
- data/CHANGELOG.md +123 -0
- data/Gemfile +4 -1
- data/README.md +77 -3
- data/Rakefile +1 -1
- data/lib/generators/seedie/install_generator.rb +84 -37
- data/lib/generators/seedie/templates/blank_seedie.yml +22 -0
- data/lib/generators/seedie/templates/seedie_initializer.rb +8 -0
- data/lib/seedie/associations/base_association.rb +35 -31
- data/lib/seedie/associations/belongs_to.rb +12 -13
- data/lib/seedie/associations/has_and_belongs_to_many.rb +26 -0
- data/lib/seedie/associations/has_many.rb +6 -4
- data/lib/seedie/associations/has_one.rb +13 -13
- data/lib/seedie/configuration.rb +12 -0
- data/lib/seedie/field_values/custom_value.rb +14 -83
- data/lib/seedie/field_values/fake_value.rb +85 -17
- data/lib/seedie/field_values/faker_builder.rb +29 -35
- data/lib/seedie/field_values/value_template_validator.rb +91 -0
- data/lib/seedie/field_values_set.rb +21 -4
- data/lib/seedie/model/creator.rb +7 -5
- data/lib/seedie/model/id_generator.rb +10 -8
- data/lib/seedie/model/model_sorter.rb +14 -19
- data/lib/seedie/model_fields.rb +5 -3
- data/lib/seedie/model_seeder.rb +11 -22
- data/lib/seedie/polymorphic_association_helper.rb +20 -16
- data/lib/seedie/railtie.rb +3 -2
- data/lib/seedie/reporters/base_reporter.rb +75 -68
- data/lib/seedie/reporters/console_reporter.rb +16 -12
- data/lib/seedie/reporters/reportable.rb +14 -10
- data/lib/seedie/seeder.rb +5 -3
- data/lib/seedie/version.rb +1 -1
- data/lib/seedie.rb +21 -27
- data/lib/tasks/seedie.rake +4 -2
- metadata +32 -13
@@ -1,44 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Seedie
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
DEFAULT_COUNT = 1
|
6
|
-
INDEX = 0
|
4
|
+
module Associations
|
5
|
+
class BaseAssociation
|
6
|
+
include Reporters::Reportable
|
7
7
|
|
8
|
-
|
8
|
+
DEFAULT_COUNT = 1
|
9
|
+
INDEX = 0
|
9
10
|
|
10
|
-
|
11
|
-
@record = record
|
12
|
-
@model = model
|
13
|
-
@association_config = association_config
|
14
|
-
@reporters = reporters
|
11
|
+
attr_reader :record, :model, :association_config, :reporters
|
15
12
|
|
16
|
-
|
17
|
-
|
13
|
+
def initialize(record, model, association_config, reporters = [])
|
14
|
+
@record = record
|
15
|
+
@model = model
|
16
|
+
@association_config = association_config
|
17
|
+
@reporters = reporters
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
end
|
19
|
+
add_observers(@reporters)
|
20
|
+
end
|
22
21
|
|
23
|
-
|
24
|
-
|
25
|
-
|
22
|
+
def generate_associations
|
23
|
+
raise NotImplementedError
|
24
|
+
end
|
26
25
|
|
27
|
-
|
26
|
+
def generate_association
|
27
|
+
raise NotImplementedError
|
28
|
+
end
|
28
29
|
|
29
|
-
|
30
|
-
return config if only_count_given?(config)
|
31
|
-
return config["count"] if config["count"].present?
|
30
|
+
private
|
32
31
|
|
33
|
-
|
34
|
-
|
32
|
+
def get_association_count(config)
|
33
|
+
return config if only_count_given?(config)
|
34
|
+
return config["count"] if config["count"].present?
|
35
35
|
|
36
|
-
|
37
|
-
|
38
|
-
|
36
|
+
DEFAULT_COUNT
|
37
|
+
end
|
38
|
+
|
39
|
+
def only_count_given?(config)
|
40
|
+
config.is_a?(Numeric) || config.is_a?(String)
|
41
|
+
end
|
39
42
|
|
40
|
-
|
41
|
-
|
43
|
+
def generate_associated_field(id, association_name)
|
44
|
+
{ association_name.to_s => id }
|
45
|
+
end
|
42
46
|
end
|
43
47
|
end
|
44
|
-
end
|
48
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Seedie
|
2
4
|
module Associations
|
3
5
|
class BelongsTo < BaseAssociation
|
@@ -11,7 +13,7 @@ module Seedie
|
|
11
13
|
|
12
14
|
def generate_associations
|
13
15
|
return if association_config["belongs_to"].nil?
|
14
|
-
|
16
|
+
|
15
17
|
report(:belongs_to_start)
|
16
18
|
|
17
19
|
association_config["belongs_to"].each do |association_name, association_config|
|
@@ -29,7 +31,7 @@ module Seedie
|
|
29
31
|
|
30
32
|
private
|
31
33
|
|
32
|
-
def handle_association_config_type(reflection,
|
34
|
+
def handle_association_config_type(reflection, _association_name, association_config)
|
33
35
|
if reflection.polymorphic?
|
34
36
|
handle_polymorphic_config_type(reflection, association_config)
|
35
37
|
else
|
@@ -38,7 +40,6 @@ module Seedie
|
|
38
40
|
handle_strategy(klass, reflection, strategy)
|
39
41
|
end
|
40
42
|
end
|
41
|
-
|
42
43
|
|
43
44
|
def handle_polymorphic_config_type(reflection, association_config)
|
44
45
|
type_name = get_polymorphic_class_name(association_config["polymorphic"])
|
@@ -48,7 +49,7 @@ module Seedie
|
|
48
49
|
|
49
50
|
handle_strategy(klass, reflection, strategy)
|
50
51
|
end
|
51
|
-
|
52
|
+
|
52
53
|
# Handles the strategy for belongs_to associations
|
53
54
|
# For polymorphic reflection, we might not add a strategy
|
54
55
|
# so we need to default it to random
|
@@ -89,27 +90,25 @@ module Seedie
|
|
89
90
|
|
90
91
|
def handle_new_config_type(klass, reflection)
|
91
92
|
report(:belongs_to_associations, name: klass.to_s, parent_name: model.to_s)
|
92
|
-
|
93
|
+
|
93
94
|
new_associated_record = generate_association(klass, {}, INDEX)
|
94
95
|
associated_field_set.merge!(generate_associated_field(new_associated_record.id, reflection.foreign_key))
|
95
96
|
end
|
96
97
|
|
97
98
|
def handle_other_config_type(klass, reflection, association_config)
|
98
99
|
report(:belongs_to_associations, name: klass.to_s, parent_name: model.to_s)
|
99
|
-
|
100
|
+
|
100
101
|
new_associated_record = generate_association(klass, association_config, INDEX)
|
101
102
|
associated_field_set.merge!(generate_associated_field(new_associated_record.id, reflection.foreign_key))
|
102
103
|
end
|
103
104
|
|
104
105
|
def get_type(association_config)
|
105
|
-
if association_config.is_a?(String)
|
106
|
-
raise InvalidAssociationConfigError, "Invalid association config"
|
107
|
-
|
108
|
-
return association_config
|
109
|
-
else
|
110
|
-
association_config
|
106
|
+
if association_config.is_a?(String) && !%w[random new unique].include?(association_config)
|
107
|
+
raise InvalidAssociationConfigError, "Invalid association config"
|
111
108
|
end
|
109
|
+
|
110
|
+
association_config
|
112
111
|
end
|
113
112
|
end
|
114
113
|
end
|
115
|
-
end
|
114
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Seedie
|
4
|
+
module Associations
|
5
|
+
class HasAndBelongsToMany < BaseAssociation
|
6
|
+
def generate_associations
|
7
|
+
return if association_config["has_and_belongs_to_many"].nil?
|
8
|
+
|
9
|
+
report(:has_and_belongs_to_many_start)
|
10
|
+
association_config["has_and_belongs_to_many"].each do |association_name, association_config|
|
11
|
+
association_class = association_name.to_s.classify.constantize
|
12
|
+
count = get_association_count(association_config)
|
13
|
+
config = only_count_given?(association_config) ? {} : association_config
|
14
|
+
|
15
|
+
report(:associated_records, name: association_name, count: count, parent_name: model.to_s)
|
16
|
+
count.times do |index|
|
17
|
+
field_values_set = FieldValuesSet.new(association_class, config, index).generate_field_values_with_associations
|
18
|
+
record_creator = Model::Creator.new(record.send(association_name), reporters)
|
19
|
+
|
20
|
+
record_creator.create!(field_values_set)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -1,18 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Seedie
|
2
4
|
module Associations
|
3
5
|
class HasMany < BaseAssociation
|
4
6
|
def generate_associations
|
5
7
|
return if association_config["has_many"].nil?
|
6
|
-
|
8
|
+
|
7
9
|
report(:has_many_start)
|
8
10
|
association_config["has_many"].each do |association_name, association_config|
|
9
11
|
association_class = association_name.to_s.classify.constantize
|
10
12
|
count = get_association_count(association_config)
|
11
13
|
config = only_count_given?(association_config) ? {} : association_config
|
12
|
-
|
14
|
+
|
13
15
|
report(:associated_records, name: association_name, count: count, parent_name: model.to_s)
|
14
16
|
count.times do |index|
|
15
|
-
field_values_set = FieldValuesSet.new(association_class, config, index).
|
17
|
+
field_values_set = FieldValuesSet.new(association_class, config, index).generate_field_values_with_associations
|
16
18
|
record_creator = Model::Creator.new(record.send(association_name), reporters)
|
17
19
|
|
18
20
|
record_creator.create!(field_values_set)
|
@@ -21,4 +23,4 @@ module Seedie
|
|
21
23
|
end
|
22
24
|
end
|
23
25
|
end
|
24
|
-
end
|
26
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Seedie
|
2
4
|
module Associations
|
3
5
|
class HasOne < BaseAssociation
|
@@ -5,25 +7,23 @@ module Seedie
|
|
5
7
|
return if association_config["has_one"].nil?
|
6
8
|
|
7
9
|
report(:has_one_start)
|
8
|
-
|
10
|
+
|
9
11
|
association_config["has_one"].each do |association_name, association_config|
|
10
12
|
reflection = model.reflect_on_association(association_name)
|
11
13
|
association_class = reflection.klass
|
12
14
|
count = get_association_count(association_config)
|
13
|
-
|
15
|
+
|
14
16
|
report(:associated_records, count: count, name: association_name, parent_name: model.to_s)
|
15
|
-
if count > 1
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
record_creator.create!(field_values_set.merge!(parent_field_set))
|
24
|
-
end
|
17
|
+
raise InvalidAssociationConfigError, "has_one association cannot be more than 1" if count > 1
|
18
|
+
|
19
|
+
config = only_count_given?(association_config) ? {} : association_config
|
20
|
+
field_values_set = FieldValuesSet.new(association_class, config, INDEX).generate_field_values
|
21
|
+
parent_field_set = generate_associated_field(record.id, reflection.foreign_key)
|
22
|
+
|
23
|
+
record_creator = Model::Creator.new(association_class, reporters)
|
24
|
+
record_creator.create!(field_values_set.merge!(parent_field_set))
|
25
25
|
end
|
26
26
|
end
|
27
27
|
end
|
28
28
|
end
|
29
|
-
end
|
29
|
+
end
|
@@ -1,9 +1,8 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Seedie
|
2
4
|
module FieldValues
|
3
5
|
class CustomValue
|
4
|
-
VALID_KEYS = ["values", "value", "options"].freeze
|
5
|
-
PICK_STRATEGIES = ["random", "sequential"].freeze
|
6
|
-
|
7
6
|
attr_reader :name, :parsed_value
|
8
7
|
|
9
8
|
def initialize(name, value_template, index)
|
@@ -12,7 +11,7 @@ module Seedie
|
|
12
11
|
@index = index
|
13
12
|
@parsed_value = ""
|
14
13
|
|
15
|
-
|
14
|
+
ValueTemplateValidator.new(@value_template, @index, @name).validate
|
16
15
|
end
|
17
16
|
|
18
17
|
def generate_custom_field_value
|
@@ -27,89 +26,21 @@ module Seedie
|
|
27
26
|
|
28
27
|
private
|
29
28
|
|
30
|
-
def validate_value_template
|
31
|
-
return unless @value_template.is_a?(Hash)
|
32
|
-
|
33
|
-
validate_keys
|
34
|
-
validate_values if @value_template.key?("values")
|
35
|
-
validate_options if @value_template.key?("options")
|
36
|
-
end
|
37
|
-
|
38
|
-
def validate_values
|
39
|
-
values = @value_template["values"]
|
40
|
-
options = @value_template["options"]
|
41
|
-
|
42
|
-
if values.is_a?(Array) || values.is_a?(Hash)
|
43
|
-
validate_sequential_values_length
|
44
|
-
else
|
45
|
-
raise InvalidCustomFieldValuesError, "The values key for #{@name} must be an array or a hash with start and end keys."
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
def validate_options
|
50
|
-
options = @value_template["options"]
|
51
|
-
pick_strategy = options["pick_strategy"]
|
52
|
-
|
53
|
-
if pick_strategy.present? && !PICK_STRATEGIES.include?(pick_strategy)
|
54
|
-
raise InvalidCustomFieldOptionsError,
|
55
|
-
"The pick_strategy for #{@name} must be either 'sequential' or 'random'."
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
## If pick strategy is sequential, we need to ensure there is a value for each index
|
60
|
-
# If there isn't sufficient values, we raise an error
|
61
|
-
def validate_sequential_values_length
|
62
|
-
return unless @value_template.key?("options")
|
63
|
-
return unless @value_template["options"]["pick_strategy"] == "sequential"
|
64
|
-
|
65
|
-
values = @value_template["values"]
|
66
|
-
|
67
|
-
if values.is_a?(Hash) && values.keys.sort == ["end", "start"]
|
68
|
-
# Assuming the values are an inclusive range
|
69
|
-
values_length = values["end"] - values["start"] + 1
|
70
|
-
else
|
71
|
-
values_length = values.length
|
72
|
-
end
|
73
|
-
|
74
|
-
if values_length < @index + 1
|
75
|
-
raise CustomFieldNotEnoughValuesError,
|
76
|
-
"There are not enough values for #{@name}. Please add more values."
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
def validate_keys
|
81
|
-
invalid_keys = @value_template.keys - VALID_KEYS
|
82
|
-
|
83
|
-
if invalid_keys.present?
|
84
|
-
raise InvalidCustomFieldKeysError,
|
85
|
-
"Invalid keys for #{@name}: #{invalid_keys.join(", ")}. Only #{VALID_KEYS} are allowed."
|
86
|
-
end
|
87
|
-
|
88
|
-
if @value_template.key?("values")
|
89
|
-
if @value_template.key?("value")
|
90
|
-
raise InvalidCustomFieldKeysError,
|
91
|
-
"Invalid keys for #{@name}: values and value cannot be used together."
|
92
|
-
end
|
93
|
-
|
94
|
-
if @value_template["values"].is_a?(Hash)
|
95
|
-
if !@value_template["values"].key?("start") || !@value_template["values"].key?("end")
|
96
|
-
raise InvalidCustomFieldValuesError,
|
97
|
-
"The values key for #{@name} must be an array or a hash with start and end keys."
|
98
|
-
end
|
99
|
-
end
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
29
|
def generate_custom_value_from_string
|
104
30
|
@parsed_value = @value_template.gsub("{{index}}", @index.to_s)
|
105
31
|
|
106
32
|
@parsed_value.gsub!(/\{\{(.+?)\}\}/) do
|
107
|
-
method_string =
|
33
|
+
method_string = ::Regexp.last_match(1)
|
108
34
|
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
35
|
+
raise InvalidFakerMethodError, "Invalid method: #{method_string}" unless method_string.start_with?("Faker::")
|
36
|
+
|
37
|
+
method_chain = method_string.split(".")
|
38
|
+
# Faker::Name will be shifted off the array
|
39
|
+
faker_class = method_chain.shift.constantize
|
40
|
+
|
41
|
+
# For Faker::Internet.unique.email, there will be two methods in the array
|
42
|
+
method_chain.reduce(faker_class) do |current_class_or_value, method|
|
43
|
+
current_class_or_value.public_send(method)
|
113
44
|
end
|
114
45
|
end
|
115
46
|
end
|
@@ -123,7 +54,7 @@ module Seedie
|
|
123
54
|
generate_custom_values_from_range(@value_template["values"]["start"], @value_template["values"]["end"])
|
124
55
|
end
|
125
56
|
options = @value_template["options"]
|
126
|
-
|
57
|
+
|
127
58
|
if options.present? && options["pick_strategy"] == "sequential"
|
128
59
|
@parsed_value = values[@index]
|
129
60
|
else
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Seedie
|
2
4
|
module FieldValues
|
3
5
|
class FakeValue
|
@@ -9,41 +11,107 @@ module Seedie
|
|
9
11
|
def generate_fake_value
|
10
12
|
case @column.type
|
11
13
|
when :string, :text, :citext
|
12
|
-
|
14
|
+
generate_string
|
13
15
|
when :uuid
|
14
|
-
|
16
|
+
generate_uuid
|
15
17
|
when :integer, :bigint, :smallint
|
16
|
-
|
18
|
+
generate_integer
|
17
19
|
when :decimal, :float, :real
|
18
|
-
|
20
|
+
generate_decimal
|
19
21
|
when :datetime, :timestamp, :timestamptz
|
20
|
-
|
22
|
+
generate_datetime
|
21
23
|
when :date
|
22
|
-
|
24
|
+
generate_date
|
23
25
|
when :time, :timetz
|
24
|
-
|
26
|
+
generate_time
|
25
27
|
when :boolean
|
26
|
-
|
28
|
+
generate_boolean
|
27
29
|
when :json, :jsonb
|
28
|
-
|
30
|
+
generate_json
|
29
31
|
when :inet
|
30
|
-
|
32
|
+
generate_inet
|
31
33
|
when :cidr, :macaddr
|
32
|
-
|
34
|
+
generate_macaddr
|
33
35
|
when :bytea
|
34
|
-
|
36
|
+
generate_bytea
|
35
37
|
when :bit, :bit_varying
|
36
|
-
|
38
|
+
generate_bit
|
37
39
|
when :money
|
38
|
-
|
40
|
+
generate_money
|
39
41
|
when :hstore
|
40
|
-
|
42
|
+
generate_hstore
|
41
43
|
when :year
|
42
|
-
|
44
|
+
generate_year
|
43
45
|
else
|
44
46
|
raise UnknownColumnTypeError, "Unknown column type: #{@column.type}"
|
45
47
|
end
|
46
48
|
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def generate_string
|
53
|
+
Faker::Lorem.word
|
54
|
+
end
|
55
|
+
|
56
|
+
def generate_uuid
|
57
|
+
SecureRandom.uuid
|
58
|
+
end
|
59
|
+
|
60
|
+
def generate_integer
|
61
|
+
Faker::Number.number(digits: 5)
|
62
|
+
end
|
63
|
+
|
64
|
+
def generate_decimal
|
65
|
+
Faker::Number.decimal(l_digits: 2, r_digits: 2)
|
66
|
+
end
|
67
|
+
|
68
|
+
def generate_datetime
|
69
|
+
Faker::Time.between(from: DateTime.now - 1, to: DateTime.now)
|
70
|
+
end
|
71
|
+
|
72
|
+
def generate_date
|
73
|
+
Faker::Date.between(from: Date.today - 2, to: Date.today)
|
74
|
+
end
|
75
|
+
|
76
|
+
def generate_time
|
77
|
+
Faker::Time.forward(days: 23, period: :morning)
|
78
|
+
end
|
79
|
+
|
80
|
+
def generate_boolean
|
81
|
+
Faker::Boolean.boolean
|
82
|
+
end
|
83
|
+
|
84
|
+
def generate_json
|
85
|
+
{ "value" => { "key1" => Faker::Lorem.word, "key2" => Faker::Number.number(digits: 2) } }
|
86
|
+
end
|
87
|
+
|
88
|
+
def generate_inet
|
89
|
+
Faker::Internet.ip_v4_address
|
90
|
+
end
|
91
|
+
|
92
|
+
def generate_macaddr
|
93
|
+
Faker::Internet.mac_address
|
94
|
+
end
|
95
|
+
|
96
|
+
def generate_bytea
|
97
|
+
Faker::Internet.password
|
98
|
+
end
|
99
|
+
|
100
|
+
def generate_bit
|
101
|
+
%w[0 1].sample
|
102
|
+
end
|
103
|
+
|
104
|
+
def generate_money
|
105
|
+
Faker::Commerce.price.to_s
|
106
|
+
end
|
107
|
+
|
108
|
+
def generate_hstore
|
109
|
+
{ "value" => { "key1" => Faker::Lorem.word, "key2" => Faker::Number.number(digits: 2) } }
|
110
|
+
end
|
111
|
+
|
112
|
+
def generate_year
|
113
|
+
rand(1901..2155)
|
114
|
+
end
|
47
115
|
end
|
48
116
|
end
|
49
|
-
end
|
117
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Seedie
|
2
4
|
module FieldValues
|
3
5
|
class FakerBuilder
|
@@ -10,15 +12,18 @@ module Seedie
|
|
10
12
|
@class_prefix = ""
|
11
13
|
@method_prefix = ""
|
12
14
|
@options = ""
|
15
|
+
@seedie_config_custom_attributes = Seedie.configuration.custom_attributes
|
13
16
|
end
|
14
17
|
|
15
18
|
def build_faker_constant
|
19
|
+
return @seedie_config_custom_attributes[@name.to_sym] if @seedie_config_custom_attributes.key?(@name.to_sym)
|
20
|
+
|
16
21
|
@unique_prefix = "unique." if has_validation?(:uniqueness)
|
17
|
-
|
22
|
+
|
18
23
|
add_faker_class_and_method(@column.type)
|
19
|
-
|
24
|
+
|
20
25
|
if has_validation?(:inclusion)
|
21
|
-
handle_inclusion_validation
|
26
|
+
handle_inclusion_validation
|
22
27
|
else
|
23
28
|
@options += handle_numericality_validation if has_validation?(:numericality)
|
24
29
|
@options += handle_length_validation if has_validation?(:length)
|
@@ -37,58 +42,46 @@ module Seedie
|
|
37
42
|
def add_faker_class_and_method(type)
|
38
43
|
case type
|
39
44
|
when :string, :text, :citext
|
40
|
-
|
41
|
-
@method_prefix = "word"
|
45
|
+
set_faker("Lorem.", "word")
|
42
46
|
when :uuid
|
43
|
-
|
44
|
-
@method_prefix = "uuid"
|
47
|
+
set_faker("Internet.", "uuid")
|
45
48
|
when :integer, :bigint, :smallint
|
46
|
-
|
47
|
-
@method_prefix = "number"
|
48
|
-
@options = "(digits: 5)"
|
49
|
+
set_faker("Number.", "number", "(digits: 5)")
|
49
50
|
when :decimal, :float, :real
|
50
|
-
|
51
|
-
@method_prefix = "decimal"
|
52
|
-
@options = "(l_digits: 2, r_digits: 2)"
|
51
|
+
set_faker("Number.", "decimal", "(l_digits: 2, r_digits: 2)")
|
53
52
|
when :datetime, :timestamp, :timestamptz, :time, :timetz
|
54
|
-
|
55
|
-
@method_prefix = "between"
|
56
|
-
@options = "(from: DateTime.now - 1, to: DateTime.now)"
|
53
|
+
set_faker("Time.", "between", "(from: DateTime.now - 1, to: DateTime.now)")
|
57
54
|
when :date
|
58
|
-
|
59
|
-
@method_prefix = "between"
|
60
|
-
@options = "(from: Date.today - 1, to: Date.today)"
|
55
|
+
set_faker("Date.", "between", "(from: Date.today - 1, to: Date.today)")
|
61
56
|
when :boolean
|
62
|
-
|
63
|
-
@method_prefix = "boolean"
|
57
|
+
set_faker("Boolean.", "boolean")
|
64
58
|
when :json, :jsonb
|
65
59
|
@faker_expression = { "value" => "Json.shallow_json(width: 3, options: { key: 'Name.first_name', value: 'Number.number(digits: 2)' })" }
|
66
60
|
when :inet
|
67
|
-
|
68
|
-
@method_prefix = "ip_v4_address"
|
61
|
+
set_faker("Internet.", "ip_v4_address")
|
69
62
|
when :cidr, :macaddr
|
70
|
-
|
71
|
-
@method_prefix = "mac_address"
|
63
|
+
set_faker("Internet.", "mac_address")
|
72
64
|
when :bytea
|
73
|
-
|
74
|
-
@method_prefix = "password"
|
65
|
+
set_faker("Internet.", "password")
|
75
66
|
when :bit, :bit_varying
|
76
|
-
|
77
|
-
@method_prefix = "password"
|
67
|
+
set_faker("Internet.", "password")
|
78
68
|
when :money
|
79
|
-
|
80
|
-
@method_prefix = "price.to_s"
|
69
|
+
set_faker("Commerce.", "price.to_s")
|
81
70
|
when :hstore
|
82
71
|
@faker_expression = { "value" => "Json.shallow_json(width: 3, options: { key: 'Name.first_name', value: 'Number.number(digits: 2)' })" }
|
83
72
|
when :year
|
84
|
-
|
85
|
-
@method_prefix = "number"
|
86
|
-
@options = "(digits: 4)"
|
73
|
+
set_faker("Number.", "number", "(digits: 4)")
|
87
74
|
else
|
88
75
|
raise UnknownColumnTypeError, "Unknown column type: #{type}"
|
89
76
|
end
|
90
77
|
end
|
91
78
|
|
79
|
+
def set_faker(class_prefix, method_prefix, options = "")
|
80
|
+
@class_prefix = class_prefix
|
81
|
+
@method_prefix = method_prefix
|
82
|
+
@options = options
|
83
|
+
end
|
84
|
+
|
92
85
|
def has_validation?(kind)
|
93
86
|
@validations.any? { |validation| validation.kind == kind }
|
94
87
|
end
|
@@ -129,7 +122,8 @@ module Seedie
|
|
129
122
|
@method_prefix = ""
|
130
123
|
@options = ""
|
131
124
|
if options[:in].is_a?(Range)
|
132
|
-
@faker_expression = { "values" => { "start" => options[:in].first, "end" => options[:in].last },
|
125
|
+
@faker_expression = { "values" => { "start" => options[:in].first, "end" => options[:in].last },
|
126
|
+
"options" => { "pick_strategy" => "random" } }
|
133
127
|
else
|
134
128
|
@faker_expression = { "values" => options[:in], "options" => { "pick_strategy" => "random" } }
|
135
129
|
end
|