dirty_seed 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +4 -2
  3. data/lib/dirty_seed.rb +15 -11
  4. data/lib/dirty_seed/assigners/assigner.rb +73 -0
  5. data/lib/dirty_seed/assigners/base.rb +86 -0
  6. data/lib/dirty_seed/assigners/{dirty_boolean.rb → boolean.rb} +2 -2
  7. data/lib/dirty_seed/assigners/{dirty_date.rb → date.rb} +2 -2
  8. data/lib/dirty_seed/assigners/fakers.yml +64 -1
  9. data/lib/dirty_seed/assigners/float.rb +21 -0
  10. data/lib/dirty_seed/assigners/integer.rb +20 -0
  11. data/lib/dirty_seed/assigners/min_max_helper.rb +115 -0
  12. data/lib/dirty_seed/assigners/{dirty_number.rb → number.rb} +2 -2
  13. data/lib/dirty_seed/assigners/string.rb +39 -0
  14. data/lib/dirty_seed/assigners/{dirty_time.rb → time.rb} +2 -2
  15. data/lib/dirty_seed/{dirty_association.rb → association.rb} +6 -17
  16. data/lib/dirty_seed/attribute.rb +62 -0
  17. data/lib/dirty_seed/data_model.rb +17 -18
  18. data/lib/dirty_seed/method_missing_helper.rb +1 -1
  19. data/lib/dirty_seed/{dirty_model.rb → model.rb} +81 -33
  20. data/lib/dirty_seed/version.rb +1 -1
  21. data/spec/dummy/app/models/bravo.rb +2 -1
  22. data/spec/dummy/db/test.sqlite3 +0 -0
  23. data/spec/dummy/log/development.log +2 -0
  24. data/spec/dummy/log/test.log +62192 -0
  25. data/spec/lib/dirty_seed/assigners/assigner_spec.rb +19 -0
  26. data/spec/lib/dirty_seed/assigners/base_spec.rb +81 -0
  27. data/spec/lib/dirty_seed/assigners/boolean_spec.rb +13 -0
  28. data/spec/lib/dirty_seed/assigners/date_spec.rb +15 -0
  29. data/spec/lib/dirty_seed/assigners/float_spec.rb +43 -0
  30. data/spec/lib/dirty_seed/assigners/integer_spec.rb +43 -0
  31. data/spec/lib/dirty_seed/assigners/string_spec.rb +47 -0
  32. data/spec/lib/dirty_seed/assigners/time_spec.rb +15 -0
  33. data/spec/lib/dirty_seed/association_spec.rb +64 -0
  34. data/spec/lib/dirty_seed/attribute_spec.rb +49 -0
  35. data/spec/lib/dirty_seed/data_model_spec.rb +8 -36
  36. data/spec/lib/dirty_seed/{dirty_model_spec.rb → model_spec.rb} +13 -13
  37. data/spec/support/helpers.rb +5 -5
  38. metadata +37 -33
  39. data/lib/dirty_seed/assigners/dirty_assigner.rb +0 -95
  40. data/lib/dirty_seed/assigners/dirty_float.rb +0 -35
  41. data/lib/dirty_seed/assigners/dirty_integer.rb +0 -16
  42. data/lib/dirty_seed/assigners/dirty_string.rb +0 -74
  43. data/lib/dirty_seed/dirty_attribute.rb +0 -75
  44. data/spec/lib/dirty_seed/assigners/dirty_assigner_spec.rb +0 -68
  45. data/spec/lib/dirty_seed/assigners/dirty_boolean_spec.rb +0 -13
  46. data/spec/lib/dirty_seed/assigners/dirty_date_spec.rb +0 -15
  47. data/spec/lib/dirty_seed/assigners/dirty_float_spec.rb +0 -43
  48. data/spec/lib/dirty_seed/assigners/dirty_integer_spec.rb +0 -43
  49. data/spec/lib/dirty_seed/assigners/dirty_string_spec.rb +0 -53
  50. data/spec/lib/dirty_seed/assigners/dirty_time_spec.rb +0 -15
  51. data/spec/lib/dirty_seed/dirty_association_spec.rb +0 -64
  52. data/spec/lib/dirty_seed/dirty_attribute_spec.rb +0 -49
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3b389f96d3fa98b9ba404fc38fc0cca7979a8b1ce107a685cb77bbd77bea16ab
4
- data.tar.gz: bd5ed8df6a78e30d72178d41160f64bc5de7f4c2ebfb86a49126f5ba88720f68
3
+ metadata.gz: 2baeb1ee3088a3897bfee64a68299f83d45fe2ccb3799d71ca2ccb67d22f2400
4
+ data.tar.gz: 859d9c16b0f5283c9127697cb67e700aefda31e777e7ad0e9275fdae5b2c9abd
5
5
  SHA512:
6
- metadata.gz: 9d3dab43a6ecd4e38f6fadb3f6f283fdfc137a6c7d48882735c66d3cda97a547eb6f126e1d8d81571f991bb34c23ed26894a53e1422e546199f33805af86e391
7
- data.tar.gz: 6717f972b7753d24475174c17f94fbea72af36a4ce32ca9b58f0e807f04212671b74e4d126c1c1fd9877240438badbcf7ab084f692ad54c86400e0ff27ecf568
6
+ metadata.gz: ecef53529af9c66e515e121cbd89ad053555b0f4a554d6a5d331939d8c4238ac7e194d6f3f806cee560b95deef03c5769bf73fffc3a5c7d329c732f3d8af8e00
7
+ data.tar.gz: b649f5b9c119bc63c95a635b74692b3b83e3d86115193fb329c94c3f52b9aef1fffcaab697ec8facc790687721444fd8446eeff7c6712cf75513713e63f93051
data/README.md CHANGED
@@ -7,7 +7,7 @@
7
7
  Add this line to your application's Gemfile:
8
8
 
9
9
  ```ruby
10
- gem 'dirty_seed', '~> 0.1.7'
10
+ gem 'dirty_seed', '~> 0.1.8'
11
11
  ```
12
12
 
13
13
  And then execute:
@@ -244,7 +244,10 @@ The current attribute names treated this way are:
244
244
 
245
245
  - `address`
246
246
  - `city`
247
+ - `color`
248
+ - `colour`
247
249
  - `country`
250
+ - `currency`
248
251
  - `description`
249
252
  - `email`
250
253
  - `first_name`
@@ -281,6 +284,5 @@ The gem is available as open source under the terms of the [MIT License](https:/
281
284
  * Detect more "special" attributes to ignore.
282
285
  * Use instance errors to adjust values and eventually match custom validations.
283
286
  * Add a configuration system to define how to seed: models to skip, default values, etc.
284
- * Increase performances.
285
287
 
286
288
 
@@ -1,24 +1,28 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'faker'
4
+
3
5
  require_relative 'dirty_seed/engine'
4
6
  require_relative 'dirty_seed/exceptions'
5
7
 
6
8
  module DirtySeed
9
+ autoload :Association, 'dirty_seed/association'
10
+ autoload :Attribute, 'dirty_seed/attribute'
7
11
  autoload :DataModel, 'dirty_seed/data_model'
8
- autoload :DirtyAssociation, 'dirty_seed/dirty_association'
9
- autoload :DirtyAttribute, 'dirty_seed/dirty_attribute'
10
- autoload :DirtyModel, 'dirty_seed/dirty_model'
12
+ autoload :Model, 'dirty_seed/model'
11
13
  autoload :MethodMissingHelper, 'dirty_seed/method_missing_helper'
12
14
  autoload :Sorter, 'dirty_seed/sorter'
13
15
 
14
16
  module Assigners
15
- autoload :DirtyAssigner, 'dirty_seed/assigners/dirty_assigner'
16
- autoload :DirtyBoolean, 'dirty_seed/assigners/dirty_boolean'
17
- autoload :DirtyDate, 'dirty_seed/assigners/dirty_date'
18
- autoload :DirtyFloat, 'dirty_seed/assigners/dirty_float'
19
- autoload :DirtyInteger, 'dirty_seed/assigners/dirty_integer'
20
- autoload :DirtyNumber, 'dirty_seed/assigners/dirty_number'
21
- autoload :DirtyString, 'dirty_seed/assigners/dirty_string'
22
- autoload :DirtyTime, 'dirty_seed/assigners/dirty_time'
17
+ autoload :Assigner, 'dirty_seed/assigners/assigner'
18
+ autoload :Base, 'dirty_seed/assigners/base'
19
+ autoload :Boolean, 'dirty_seed/assigners/boolean'
20
+ autoload :Date, 'dirty_seed/assigners/date'
21
+ autoload :Float, 'dirty_seed/assigners/float'
22
+ autoload :Integer, 'dirty_seed/assigners/integer'
23
+ autoload :MinMaxHelper, 'dirty_seed/assigners/min_max_helper'
24
+ autoload :Number, 'dirty_seed/assigners/number'
25
+ autoload :String, 'dirty_seed/assigners/string'
26
+ autoload :Time, 'dirty_seed/assigners/time'
23
27
  end
24
28
  end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'regexp-examples'
4
+
5
+ module DirtySeed
6
+ module Assigners
7
+ # Draws a value matching validators
8
+ class Assigner
9
+ attr_reader :attribute
10
+
11
+ delegate :name, :type, :validators, to: :attribute
12
+
13
+ # Initializes an instance
14
+ # @param attribute [DirtySeed::Attribute]
15
+ # @return [DirtySeed::Assigners::Assigner]
16
+ def initialize(attribute)
17
+ @attribute = attribute
18
+ end
19
+
20
+ private
21
+
22
+ # Returns true if the value should be unique
23
+ # @return [Boolean]
24
+ def unique?
25
+ @unique ||=
26
+ validators.any? do |validator|
27
+ validator.is_a? ActiveRecord::Validations::UniquenessValidator
28
+ end
29
+ end
30
+
31
+ # Returns the regex pattern if value should respect a format
32
+ # e.g. `validates :email, format: { with: /\w{10}@(hotmail|gmail)\.com/ }`
33
+ # @return [Array<>]
34
+ def regex
35
+ regex_validator =
36
+ validators.find do |validator|
37
+ validator.is_a? ActiveModel::Validations::FormatValidator
38
+ end
39
+ regex_validator&.options&.dig(:with)
40
+ end
41
+
42
+ # Returns a value matching the requirements
43
+ # @param category [Symbol] fake category
44
+ # @param method [Symbol] fake method
45
+ # @param options [Hash] options used by faker
46
+ # @return [Object]
47
+ def faker_value(category:, method:, options: nil)
48
+ if options
49
+ faker_method(category).public_send(method, options)
50
+ else
51
+ faker_method(category).public_send(method)
52
+ end
53
+ rescue Faker::UniqueGenerator::RetryLimitExceeded
54
+ nil
55
+ end
56
+
57
+ # Returns a faker method
58
+ # @param category [Symbol] fake category
59
+ # @return [Faker::?]
60
+ def faker_method(category)
61
+ @faker_method ||= define_faker_method(category)
62
+ end
63
+
64
+ # Returns a faker method
65
+ # @param category [Symbol] fake category
66
+ # @return [Faker::?]
67
+ def define_faker_method(category)
68
+ action = "::Faker::#{category}".constantize
69
+ unique? ? action.unique : action
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DirtySeed
4
+ module Assigners
5
+ # Draws a value matching validators
6
+ class Base < Assigner
7
+ SPECIFIC_TYPES = %i[float integer string].freeze
8
+ private_constant :SPECIFIC_TYPES
9
+
10
+ # Returns a value depending on validators and attribute type
11
+ # It firstly check if value can be set relying to validators
12
+ # If not, it delegates to an assigner adapted to the attribute type
13
+ # @return [<?>] depends on attribute
14
+ def value
15
+ return if absent?
16
+ return inclusion_options.sample if inclusion_options.is_a? Array
17
+ return rand(inclusion_options) if inclusion_options.is_a? Range
18
+
19
+ try_specific || adapted_assigner&.value
20
+ end
21
+
22
+ private
23
+
24
+ # Returns true if value should be absent
25
+ # Returns [Boolean]
26
+ def absent?
27
+ validators.any? do |validator|
28
+ validator.is_a? ActiveRecord::Validations::AbsenceValidator
29
+ end
30
+ end
31
+
32
+ # Returns the inclusion options if value should be included in options
33
+ # e.g. `validates :status, inclusion: { in: %w[todo done aborted] }`
34
+ # @return [Array<>]
35
+ def inclusion_options
36
+ inclusion_validator =
37
+ validators.find do |validator|
38
+ validator.is_a? ActiveModel::Validations::InclusionValidator
39
+ end
40
+ inclusion_validator&.options&.dig(:in)
41
+ end
42
+
43
+ # If attribute name is listed in meaningful attributes
44
+ # Then rely on faker
45
+ # And validate it matches type and other validations
46
+ # @return [<?>] depends on attribute
47
+ def try_specific
48
+ return unless specific_options
49
+ return unless specific_options[:types].include? type.to_s
50
+
51
+ try = faker_value(specific_options.except(:types))
52
+ return if regex && !regex.match?(try)
53
+
54
+ try
55
+ end
56
+
57
+ # Returns the specific options if the attribute name is specific
58
+ # @return [Hash, nil]
59
+ def specific_options
60
+ @specific_options ||= specific_attributes[name]
61
+ end
62
+
63
+ # Returns specific attributes
64
+ # @return [Hash]
65
+ # @example
66
+ # { address: { category: 'Address', method: 'full_address' } }
67
+ def specific_attributes
68
+ YAML.safe_load(
69
+ File.read(
70
+ DirtySeed::Engine.root.join('lib', 'dirty_seed', 'assigners', 'fakers.yml')
71
+ )
72
+ ).deep_symbolize_keys
73
+ end
74
+
75
+ # Returns the adapted assigner depending on type
76
+ # @return [DirtySeed::Assigners::<?>]
77
+ def adapted_assigner
78
+ @adapted_assigner ||=
79
+ "DirtySeed::Assigners::#{attribute.type.capitalize}".constantize.new(attribute)
80
+ # If attribute type is not currently handled (json, array...) return nil
81
+ rescue NameError
82
+ nil
83
+ end
84
+ end
85
+ end
86
+ end
@@ -3,10 +3,10 @@
3
3
  module DirtySeed
4
4
  module Assigners
5
5
  # Draws a value matching validators
6
- class DirtyBoolean < DirtyAssigner
6
+ class Boolean < Assigner
7
7
  # Returns a boolean
8
8
  # @return [Boolean]
9
- def define_value
9
+ def value
10
10
  [true, false].sample
11
11
  end
12
12
  end
@@ -3,10 +3,10 @@
3
3
  module DirtySeed
4
4
  module Assigners
5
5
  # Draws a value matching validators
6
- class DirtyDate < DirtyAssigner
6
+ class Date < Assigner
7
7
  # Returns a date matching all validators
8
8
  # @return [Date]
9
- def define_value
9
+ def value
10
10
  faker_value(
11
11
  category: :Date,
12
12
  method: :between,
@@ -1,80 +1,143 @@
1
1
  address:
2
2
  category: Address
3
3
  method: full_address
4
+ types:
5
+ - string
4
6
  city:
5
7
  category: Address
6
8
  method: city
9
+ types:
10
+ - string
11
+ color:
12
+ category: Color
13
+ method: hex_color
14
+ types:
15
+ - string
16
+ colour:
17
+ category: Color
18
+ method: hex_color
19
+ types:
20
+ - string
7
21
  country:
8
22
  category: Address
9
23
  method: country
24
+ types:
25
+ - string
26
+ currency:
27
+ category: currency
28
+ method: code
29
+ types:
30
+ - string
10
31
  description:
11
32
  category: Lorem
12
33
  method: paragraph
34
+ types:
35
+ - string
13
36
  options:
14
37
  sentence_count: 2
15
38
  random_sentences_to_add: 4
16
39
  email:
17
40
  category: Internet
18
41
  method: email
42
+ types:
43
+ - string
19
44
  options:
20
45
  domain: example
21
46
  first_name:
22
47
  category: Name
23
48
  method: first_name
49
+ types:
50
+ - string
24
51
  firstname:
25
52
  category: Name
26
53
  method: first_name
54
+ types:
55
+ - string
27
56
  last_name:
28
57
  category: Name
29
58
  method: last_name
59
+ types:
60
+ - string
30
61
  lastname:
31
62
  category: Name
32
63
  method: last_name
64
+ types:
65
+ - string
33
66
  lat:
34
67
  category: Address
35
68
  method: latitude
69
+ types:
70
+ - float
36
71
  latitude:
37
72
  category: Address
38
73
  method: latitude
74
+ types:
75
+ - float
39
76
  lng:
40
77
  category: Address
41
78
  method: longitude
79
+ types:
80
+ - float
42
81
  locale:
43
82
  category: Address
44
83
  method: country_code
84
+ types:
85
+ - string
45
86
  longitude:
46
87
  category: Address
47
88
  method: longitude
89
+ types:
90
+ - float
48
91
  middlename:
49
92
  category: Name
50
93
  method: middle_name
94
+ types:
95
+ - string
51
96
  middle_name:
52
97
  category: Name
53
98
  method: middle_name
99
+ types:
100
+ - string
54
101
  password:
55
102
  category: Internet
56
103
  method: password
104
+ types:
105
+ - string
57
106
  phone:
58
107
  category: PhoneNumber
59
108
  method: phone_number
109
+ types:
110
+ - string
60
111
  phone_number:
61
112
  category: PhoneNumber
62
113
  method: phone_number
114
+ types:
115
+ - string
63
116
  reference:
64
117
  category: Internet
65
118
  method: uuid
119
+ types:
120
+ - string
66
121
  title:
67
122
  category: Lorem
68
123
  method: sentence
124
+ types:
125
+ - string
69
126
  options:
70
127
  word_count: 3
71
128
  random_words_to_add: 4
72
129
  user_name:
73
130
  category: Internet
74
131
  method: username
132
+ types:
133
+ - string
75
134
  username:
76
135
  category: Internet
77
136
  method: username
137
+ types:
138
+ - string
78
139
  uuid:
79
140
  category: Internet
80
- method: uuid
141
+ method: uuid
142
+ types:
143
+ - string
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DirtySeed
4
+ module Assigners
5
+ # Draws an Float matching validators
6
+ class Float < Assigner
7
+ include MinMaxHelper
8
+
9
+ # Returns an value matching all validators
10
+ # @return [Float]
11
+ def value
12
+ # binding.pry
13
+ faker_value(
14
+ category: :Number,
15
+ method: :between,
16
+ options: { from: min, to: max }
17
+ )
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DirtySeed
4
+ module Assigners
5
+ # Draws an integer matching validators
6
+ class Integer < Assigner
7
+ include MinMaxHelper
8
+
9
+ # Returns an value matching all validators
10
+ # @return [Integer]
11
+ def value
12
+ faker_value(
13
+ category: :Number,
14
+ method: :between,
15
+ options: { from: min, to: max }
16
+ )
17
+ end
18
+ end
19
+ end
20
+ end