smart_seeds 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +8 -5
- data/lib/smart_seeds.rb +1 -2
- data/lib/smart_seeds/generator/base.rb +2 -15
- data/lib/smart_seeds/generator/boolean.rb +1 -1
- data/lib/smart_seeds/generator/faker.rb +2 -0
- data/lib/smart_seeds/generator/integer/foreign_key.rb +20 -2
- data/lib/smart_seeds/generator/string.rb +5 -1
- data/lib/smart_seeds/generator/text.rb +5 -1
- data/lib/smart_seeds/performing.rb +16 -8
- data/lib/smart_seeds/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 96bc7299e41923854e4e8c8ba348c17d5cb967c9
|
4
|
+
data.tar.gz: a5eb2cba7a94885343293847033e5981bddff985
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 61c166b9b57cce3f760ae108e14dd69ede4c6328f0e7c3d9e9a0051ad521ec921c69dd0009edb519b73d94cf7d6bbd6feb2c403b99e84189c7d95bf10c2b8865
|
7
|
+
data.tar.gz: 41296cd2f9ade2890ffe6e79f08e0672e8634b1372c8f4f4128ec3f80b9ffe3d07e9e6d874f305b6cde11ec2287409de08dc1ef8db2aa64f2e7d0e6229fe8e29
|
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# SmartSeeds
|
2
|
-
Gem for
|
3
|
-
|
2
|
+
Gem for auto-smart generating the seeds in your ruby app. Compatible only with ActiveRecord.
|
3
|
+
|
4
|
+
It determines types of fields(attributes) in an AR model and generates random values for each of these attributes.
|
4
5
|
|
5
6
|
## Usage
|
6
7
|
|
@@ -15,7 +16,7 @@ All you need to do is run this command: `SmartSeeds.plant(Entity)`
|
|
15
16
|
=> true
|
16
17
|
|
17
18
|
```
|
18
|
-
And
|
19
|
+
And you've got an object in a database:
|
19
20
|
```
|
20
21
|
> Entity.last
|
21
22
|
Entity Load (0.2ms) SELECT "entities".* FROM "entities" ORDER BY "entities"."id" DESC LIMIT ? [["LIMIT", 1]]
|
@@ -33,13 +34,15 @@ If you want to override some default values, you can send it in a hash:
|
|
33
34
|
=> true
|
34
35
|
```
|
35
36
|
|
37
|
+
By the way, all attributes which has default values on AR side will be skipped by default.
|
38
|
+
|
36
39
|
### Faker
|
37
40
|
The gem has a simple integration with [Faker](https://github.com/stympy/faker).
|
38
41
|
|
39
|
-
That means if you have
|
42
|
+
That means if you have an AR model which called like a some Faker class and an attribute called like a some faker method in that class,
|
40
43
|
a value will be generated by Faker gem.
|
41
44
|
|
42
|
-
For example: Model `Superhero` has
|
45
|
+
For example: Model `Superhero` has `name` and `power` attributes, so the value will be generated by `Faker::Superhero.name` and `Faker::Superhero.power`. [Superhero doc](https://github.com/stympy/faker/blob/master/doc/superhero.md).
|
43
46
|
```
|
44
47
|
> SmartSeeds.plant(Superhero)
|
45
48
|
(0.7ms) begin transaction
|
data/lib/smart_seeds.rb
CHANGED
@@ -1,6 +1,3 @@
|
|
1
|
-
# Available types of AR model are: :binary, :boolean, :date, :datetime, :decimal, :float, :integer, :string, :text, :time
|
2
|
-
# There are generators for all of these types, for example SmartSeeds::Generator::Integer
|
3
|
-
|
4
1
|
module SmartSeeds
|
5
2
|
module Generator
|
6
3
|
class Base
|
@@ -9,18 +6,8 @@ module SmartSeeds
|
|
9
6
|
@model = model
|
10
7
|
end
|
11
8
|
|
12
|
-
# Get class name of specific generator and execute generate value method
|
13
9
|
def generate_value
|
14
|
-
|
15
|
-
generate_via_faker
|
16
|
-
else
|
17
|
-
begin
|
18
|
-
klass = "SmartSeeds::Generator::#{column.type.to_s.capitalize}".constantize
|
19
|
-
klass.new(column, model).generate_value
|
20
|
-
rescue Exception => e
|
21
|
-
puts e.inspect
|
22
|
-
end
|
23
|
-
end
|
10
|
+
raise 'Abstract method'
|
24
11
|
end
|
25
12
|
|
26
13
|
protected
|
@@ -31,7 +18,7 @@ module SmartSeeds
|
|
31
18
|
SmartSeeds::Generator::Faker.new(column, model).is_compatible?
|
32
19
|
end
|
33
20
|
|
34
|
-
def
|
21
|
+
def generate_faker_value
|
35
22
|
SmartSeeds::Generator::Faker.new(column, model).generate_value
|
36
23
|
end
|
37
24
|
end
|
@@ -6,12 +6,30 @@ module SmartSeeds
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def generate_value
|
9
|
-
klass =
|
9
|
+
klass = convert_column_name.constantize
|
10
10
|
klass.ids.sample
|
11
11
|
end
|
12
12
|
|
13
13
|
def is_a_foreign_key?
|
14
|
-
true if
|
14
|
+
true if splitted_column_name.last == 'id'
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def convert_column_name
|
20
|
+
# If a foreign key contains of two words splitted by dash like super_hero_id(SuperHero model)
|
21
|
+
if splitted_column_name.count > 2
|
22
|
+
# Pop the last element from an splitted array(_id)
|
23
|
+
arr = splitted_column_name.take(splitted_column_name.count - 1)
|
24
|
+
arr.map(&:capitalize).join
|
25
|
+
# Or it can be just a hero_id(Hero model)
|
26
|
+
else
|
27
|
+
splitted_column_name.first.capitalize
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def splitted_column_name
|
32
|
+
column.name.split('_')
|
15
33
|
end
|
16
34
|
end
|
17
35
|
end
|
@@ -1,14 +1,14 @@
|
|
1
1
|
module SmartSeeds
|
2
2
|
class Performing
|
3
|
-
SKIPPABLE_COLUMN_NAMES = %w(id created_at updated_at)
|
4
|
-
|
5
3
|
def initialize(model, attrs)
|
6
4
|
@attrs = attrs
|
7
5
|
@model = model
|
8
6
|
@object = model.new
|
7
|
+
@skippable_attributes = %w(id created_at updated_at)
|
9
8
|
end
|
10
9
|
|
11
|
-
def
|
10
|
+
def start
|
11
|
+
add_skippable_attributes
|
12
12
|
set_default_values
|
13
13
|
|
14
14
|
# User can send custom values in a hash: SmartSeeds.(Entity, {name: 'Aleah'})
|
@@ -21,13 +21,18 @@ module SmartSeeds
|
|
21
21
|
|
22
22
|
private
|
23
23
|
|
24
|
-
attr_reader :attrs
|
25
|
-
|
26
|
-
|
24
|
+
attr_reader :attrs, :model, :object, :skippable_attributes
|
25
|
+
|
26
|
+
def add_skippable_attributes
|
27
|
+
# All default attributes which defined in AR object should be skipped by default
|
28
|
+
keys_with_default_values = model.column_defaults.select{|key, value| value.present? }.keys
|
29
|
+
skippable_attributes.concat(keys_with_default_values)
|
30
|
+
end
|
27
31
|
|
28
32
|
def set_default_values
|
29
33
|
model.columns.each do |column|
|
30
34
|
next if is_column_must_be_skipped?(column.name)
|
35
|
+
|
31
36
|
object[column.name] = generate_value(column)
|
32
37
|
end
|
33
38
|
end
|
@@ -40,12 +45,15 @@ module SmartSeeds
|
|
40
45
|
end
|
41
46
|
end
|
42
47
|
|
48
|
+
# There are generator classes for each type of columns
|
49
|
+
# If model's type is :integer, method below delegates to SmartSeeds::Generator::Integer
|
43
50
|
def generate_value(column)
|
44
|
-
SmartSeeds::Generator
|
51
|
+
klass = "SmartSeeds::Generator::#{column.type.to_s.capitalize}".constantize
|
52
|
+
klass.new(column, model).generate_value
|
45
53
|
end
|
46
54
|
|
47
55
|
def is_column_must_be_skipped?(column_name)
|
48
|
-
|
56
|
+
skippable_attributes.include?(column_name)
|
49
57
|
end
|
50
58
|
end
|
51
59
|
end
|
data/lib/smart_seeds/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: smart_seeds
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vitaliy Fry
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-01-
|
11
|
+
date: 2017-01-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|