smart_seeds 0.1.0 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +47 -2
- data/lib/smart_seeds.rb +17 -4
- data/lib/smart_seeds/generator/base.rb +39 -0
- data/lib/smart_seeds/generator/binary.rb +14 -0
- data/lib/smart_seeds/generator/boolean.rb +14 -0
- data/lib/smart_seeds/generator/date.rb +14 -0
- data/lib/smart_seeds/generator/datetime.rb +14 -0
- data/lib/smart_seeds/generator/decimal.rb +14 -0
- data/lib/smart_seeds/generator/faker.rb +41 -0
- data/lib/smart_seeds/generator/float.rb +14 -0
- data/lib/smart_seeds/generator/integer.rb +37 -0
- data/lib/smart_seeds/generator/integer/enum.rb +21 -0
- data/lib/smart_seeds/generator/integer/foreign_key.rb +18 -0
- data/lib/smart_seeds/generator/string.rb +14 -0
- data/lib/smart_seeds/generator/text.rb +14 -0
- data/lib/smart_seeds/generator/time.rb +14 -0
- data/lib/smart_seeds/performing.rb +51 -0
- data/lib/smart_seeds/version.rb +1 -1
- metadata +31 -5
- data/lib/generating.rb +0 -41
- data/lib/performing.rb +0 -41
- data/lib/tasks/autoseed_tasks.rake +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4aca1c67df762de136d3c7bd6435220424253522
|
4
|
+
data.tar.gz: f3b0810b6ae0c9d7af2e6b2a1ce17d71ac9c2a6a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2c0356fcb1055780b580c6480f008449d2f673b2e21d1ec35850970d03e62ba9a66a57765912500537d9d434e8df0e163ab55fb1f1bcfe91a2aaadc1bdf6d824
|
7
|
+
data.tar.gz: b7f912df9d79e5136e0dd98b6d2e00ff45fc30c16e95daadf65f4754407220463f97a8af8943ed96cb6e012e3b879f213552d5b0cb94af53dcf468d3e6be3567
|
data/README.md
CHANGED
@@ -1,8 +1,53 @@
|
|
1
1
|
# SmartSeeds
|
2
|
-
|
2
|
+
Gem for an auto-smart generating the seeds in your Rails app.
|
3
|
+
It defines types columns in a model and generates random values for each of these fields.
|
3
4
|
|
4
5
|
## Usage
|
5
|
-
|
6
|
+
|
7
|
+
For example `Entity` model has `name`, `age`, `is_human` attributes.
|
8
|
+
|
9
|
+
All you need to do is run this command: `SmartSeeds.plant(Entity)`
|
10
|
+
```
|
11
|
+
> SmartSeeds.plant(Entity)
|
12
|
+
(0.1ms) begin transaction
|
13
|
+
SQL (0.4ms) INSERT INTO "entities" ("id", "name", "age", "is_human", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["id", 369], ["name", "Heil"], ["age", 344], ["is_human", false], ["created_at", 2017-01-15 16:10:50 UTC], ["updated_at", 2017-01-15 16:10:50 UTC]]
|
14
|
+
(1.8ms) commit transaction
|
15
|
+
=> true
|
16
|
+
|
17
|
+
```
|
18
|
+
And we've got an object in a database:
|
19
|
+
```
|
20
|
+
> Entity.last
|
21
|
+
Entity Load (0.2ms) SELECT "entities".* FROM "entities" ORDER BY "entities"."id" DESC LIMIT ? [["LIMIT", 1]]
|
22
|
+
=> #<Entity id: 1, name: "Heil", age: 344, is_human: false, created_at: "2017-01-15 16:10:50", updated_at: "2017-01-15 16:10:50">
|
23
|
+
|
24
|
+
```
|
25
|
+
### Custom values
|
26
|
+
If you want to override some default values, you can send it in a hash:
|
27
|
+
|
28
|
+
```
|
29
|
+
> SmartSeeds.plant(Entity, {name: 'Aleah'})
|
30
|
+
(0.1ms) begin transaction
|
31
|
+
SQL (0.4ms) INSERT INTO "entities" ("id", "name", "age", "is_human", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["id", 167], ["name", "Aleah"], ["age", 374], ["is_human", false], ["created_at", 2017-01-15 16:15:15 UTC], ["updated_at", 2017-01-15 16:15:15 UTC]]
|
32
|
+
(1.6ms) commit transaction
|
33
|
+
=> true
|
34
|
+
```
|
35
|
+
|
36
|
+
### Faker
|
37
|
+
The gem has a simple integration with [Faker](https://github.com/stympy/faker).
|
38
|
+
|
39
|
+
That means if you have a AR model which called like a some Faker class and an attribute called like a some faker method in that class
|
40
|
+
a value will be generated by Faker gem.
|
41
|
+
|
42
|
+
For example: Model `Superhero` has the `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
|
+
```
|
44
|
+
> SmartSeeds.plant(Superhero)
|
45
|
+
(0.7ms) begin transaction
|
46
|
+
SQL (2.8ms) INSERT INTO "superheros" ("name", "power", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "Cheetah Skull"], ["power", "Magic"], ["created_at", 2017-01-22 23:33:20 UTC], ["updated_at", 2017-01-22 23:33:20 UTC]]
|
47
|
+
(0.9ms) commit transaction
|
48
|
+
=> #<Superhero id: 8, name: "Ultra Firebird", power: "Magic", created_at: "2017-01-22 23:33:20", updated_at: "2017-01-22 23:33:20">
|
49
|
+
|
50
|
+
```
|
6
51
|
|
7
52
|
## Installation
|
8
53
|
Add this line to your application's Gemfile:
|
data/lib/smart_seeds.rb
CHANGED
@@ -1,9 +1,22 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
require 'smart_seeds/performing'
|
2
|
+
require 'smart_seeds/generator/base'
|
3
|
+
require 'smart_seeds/generator/binary'
|
4
|
+
require 'smart_seeds/generator/boolean'
|
5
|
+
require 'smart_seeds/generator/date'
|
6
|
+
require 'smart_seeds/generator/datetime'
|
7
|
+
require 'smart_seeds/generator/decimal'
|
8
|
+
require 'smart_seeds/generator/float'
|
9
|
+
require 'smart_seeds/generator/integer'
|
10
|
+
require 'smart_seeds/generator/string'
|
11
|
+
require 'smart_seeds/generator/text'
|
12
|
+
require 'smart_seeds/generator/time'
|
13
|
+
require 'smart_seeds/generator/integer/enum'
|
14
|
+
require 'smart_seeds/generator/integer/foreign_key'
|
15
|
+
require 'smart_seeds/generator/faker'
|
3
16
|
|
4
17
|
module SmartSeeds
|
5
|
-
def self.
|
6
|
-
perform = Performing.new(model, attrs)
|
18
|
+
def self.plant(model, attrs={})
|
19
|
+
perform = SmartSeeds::Performing.new(model, attrs)
|
7
20
|
perform.call
|
8
21
|
end
|
9
22
|
end
|
@@ -0,0 +1,39 @@
|
|
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
|
+
module SmartSeeds
|
5
|
+
module Generator
|
6
|
+
class Base
|
7
|
+
def initialize(column, model)
|
8
|
+
@column = column
|
9
|
+
@model = model
|
10
|
+
end
|
11
|
+
|
12
|
+
# Get class name of specific generator and execute generate value method
|
13
|
+
def generate_value
|
14
|
+
if is_comatible_with_faker?
|
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
|
24
|
+
end
|
25
|
+
|
26
|
+
protected
|
27
|
+
|
28
|
+
attr_reader :column, :model
|
29
|
+
|
30
|
+
def is_comatible_with_faker?
|
31
|
+
SmartSeeds::Generator::Faker.new(column, model).is_compatible?
|
32
|
+
end
|
33
|
+
|
34
|
+
def generate_via_faker
|
35
|
+
SmartSeeds::Generator::Faker.new(column, model).generate_value
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module SmartSeeds
|
2
|
+
module Generator
|
3
|
+
class Faker < SmartSeeds::Generator::Base
|
4
|
+
def initialize(column, model)
|
5
|
+
super
|
6
|
+
end
|
7
|
+
|
8
|
+
def generate_value
|
9
|
+
faker_class.send(column.name.to_sym)
|
10
|
+
end
|
11
|
+
|
12
|
+
def is_compatible?
|
13
|
+
true if faker_classes_include_model_name? && faker_methods_include_column_name?
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def faker_classes_include_model_name?
|
19
|
+
# Check if Faker has a Class which called as a AR Model
|
20
|
+
faker_classes = ::Faker.constants.select {|c| ::Faker.const_get(c).is_a? Class}
|
21
|
+
faker_classes.include? model.name.to_sym
|
22
|
+
end
|
23
|
+
|
24
|
+
def faker_methods_include_column_name?
|
25
|
+
# Support methods are included in Faker generate class
|
26
|
+
support_singleton_methods = %i(method_missing fetch translate parse with_locale unique numerify letterify bothify regexify fetch_all flexible rand_in_range yaml_tag)
|
27
|
+
|
28
|
+
klass = faker_class
|
29
|
+
|
30
|
+
# Here is should stay only methods for generating random values without support methods like:
|
31
|
+
# => [:name, :prefix, :suffix, :power, :descriptor]
|
32
|
+
faker_methods = klass.singleton_methods - support_singleton_methods
|
33
|
+
faker_methods.include? column.name.to_sym
|
34
|
+
end
|
35
|
+
|
36
|
+
def faker_class
|
37
|
+
"Faker::#{model.name.capitalize}".constantize
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module SmartSeeds
|
2
|
+
module Generator
|
3
|
+
class Integer < SmartSeeds::Generator::Base
|
4
|
+
def initialize(column, model)
|
5
|
+
super
|
6
|
+
end
|
7
|
+
|
8
|
+
def generate_value
|
9
|
+
if is_an_enum?
|
10
|
+
generate_enum
|
11
|
+
elsif is_foreign_key?
|
12
|
+
generate_foreign_key
|
13
|
+
else
|
14
|
+
rand(1..66666)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def is_an_enum?
|
21
|
+
SmartSeeds::Generator::Enum.new(column, model).is_an_enum?
|
22
|
+
end
|
23
|
+
|
24
|
+
def generate_enum
|
25
|
+
SmartSeeds::Generator::Enum.new(column, model).generate_value
|
26
|
+
end
|
27
|
+
|
28
|
+
def is_foreign_key?
|
29
|
+
SmartSeeds::Generator::ForeignKey.new(column, model).is_a_foreign_key?
|
30
|
+
end
|
31
|
+
|
32
|
+
def generate_foreign_key
|
33
|
+
SmartSeeds::Generator::ForeignKey.new(column, model).generate_value
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module SmartSeeds
|
2
|
+
module Generator
|
3
|
+
class Enum < SmartSeeds::Generator::Integer
|
4
|
+
def initialize(column, model)
|
5
|
+
super
|
6
|
+
end
|
7
|
+
|
8
|
+
def generate_value
|
9
|
+
# Extract all values from enum hash
|
10
|
+
# {"status"=>{"active"=>0, "in_progress"=>1, "archived"=>2}}
|
11
|
+
# Get [0, 1, 2]
|
12
|
+
values_array = model.defined_enums[column.name].values
|
13
|
+
values_array.sample
|
14
|
+
end
|
15
|
+
|
16
|
+
def is_an_enum?
|
17
|
+
model.defined_enums.include?(column.name)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module SmartSeeds
|
2
|
+
module Generator
|
3
|
+
class ForeignKey < SmartSeeds::Generator::Integer
|
4
|
+
def initialize(column, model)
|
5
|
+
super
|
6
|
+
end
|
7
|
+
|
8
|
+
def generate_value
|
9
|
+
klass = column.name.split('_').first.capitalize.constantize
|
10
|
+
klass.ids.sample
|
11
|
+
end
|
12
|
+
|
13
|
+
def is_a_foreign_key?
|
14
|
+
true if column.name.split('_').last == 'id'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module SmartSeeds
|
2
|
+
class Performing
|
3
|
+
SKIPPABLE_COLUMN_NAMES = %w(id created_at updated_at)
|
4
|
+
|
5
|
+
def initialize(model, attrs)
|
6
|
+
@attrs = attrs
|
7
|
+
@model = model
|
8
|
+
@object = model.new
|
9
|
+
end
|
10
|
+
|
11
|
+
def call
|
12
|
+
set_default_values
|
13
|
+
|
14
|
+
# User can send custom values in a hash: SmartSeeds.(Entity, {name: 'Aleah'})
|
15
|
+
# This method overrides default values to custom('name' in the example above)
|
16
|
+
set_custom_values if attrs.any?
|
17
|
+
|
18
|
+
object.save!
|
19
|
+
object
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
attr_reader :attrs
|
25
|
+
attr_reader :model
|
26
|
+
attr_reader :object
|
27
|
+
|
28
|
+
def set_default_values
|
29
|
+
model.columns.each do |column|
|
30
|
+
next if is_column_must_be_skipped?(column.name)
|
31
|
+
object[column.name] = generate_value(column)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def set_custom_values
|
36
|
+
attrs.each do |attr|
|
37
|
+
key = attr.first
|
38
|
+
value = attr.last
|
39
|
+
object[key.to_s] = value
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def generate_value(column)
|
44
|
+
SmartSeeds::Generator::Base.new(column, model).generate_value
|
45
|
+
end
|
46
|
+
|
47
|
+
def is_column_must_be_skipped?(column_name)
|
48
|
+
SmartSeeds::Performing::SKIPPABLE_COLUMN_NAMES.include?(column_name)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
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.2
|
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-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: faker
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
description: Description of Autoseed.
|
42
56
|
email:
|
43
57
|
- vitaliy.fry@gmail.com
|
@@ -48,11 +62,23 @@ files:
|
|
48
62
|
- MIT-LICENSE
|
49
63
|
- README.md
|
50
64
|
- Rakefile
|
51
|
-
- lib/generating.rb
|
52
|
-
- lib/performing.rb
|
53
65
|
- lib/smart_seeds.rb
|
66
|
+
- lib/smart_seeds/generator/base.rb
|
67
|
+
- lib/smart_seeds/generator/binary.rb
|
68
|
+
- lib/smart_seeds/generator/boolean.rb
|
69
|
+
- lib/smart_seeds/generator/date.rb
|
70
|
+
- lib/smart_seeds/generator/datetime.rb
|
71
|
+
- lib/smart_seeds/generator/decimal.rb
|
72
|
+
- lib/smart_seeds/generator/faker.rb
|
73
|
+
- lib/smart_seeds/generator/float.rb
|
74
|
+
- lib/smart_seeds/generator/integer.rb
|
75
|
+
- lib/smart_seeds/generator/integer/enum.rb
|
76
|
+
- lib/smart_seeds/generator/integer/foreign_key.rb
|
77
|
+
- lib/smart_seeds/generator/string.rb
|
78
|
+
- lib/smart_seeds/generator/text.rb
|
79
|
+
- lib/smart_seeds/generator/time.rb
|
80
|
+
- lib/smart_seeds/performing.rb
|
54
81
|
- lib/smart_seeds/version.rb
|
55
|
-
- lib/tasks/autoseed_tasks.rake
|
56
82
|
homepage: http://frylock.me
|
57
83
|
licenses:
|
58
84
|
- MIT
|
data/lib/generating.rb
DELETED
@@ -1,41 +0,0 @@
|
|
1
|
-
module Generating
|
2
|
-
AVAILABLE_TYPES = %i(
|
3
|
-
binary
|
4
|
-
boolean
|
5
|
-
date
|
6
|
-
datetime
|
7
|
-
decimal
|
8
|
-
float
|
9
|
-
integer
|
10
|
-
string
|
11
|
-
text
|
12
|
-
time
|
13
|
-
)
|
14
|
-
|
15
|
-
def self.generate_value(column_type)
|
16
|
-
raise ArgumentError, "There is no column type #{column_type}" unless Generating::AVAILABLE_TYPES.include?(column_type)
|
17
|
-
|
18
|
-
case column_type
|
19
|
-
when :binary
|
20
|
-
'0b100'
|
21
|
-
when :boolean
|
22
|
-
[true, false].sample
|
23
|
-
when :date
|
24
|
-
DateTime.now.to_date
|
25
|
-
when :datetime
|
26
|
-
DateTime.now
|
27
|
-
when :decimal
|
28
|
-
rand(6.6...666.0).round(2)
|
29
|
-
when :float
|
30
|
-
rand(6.6...666.0).round(2)
|
31
|
-
when :integer
|
32
|
-
rand(1...666)
|
33
|
-
when :string
|
34
|
-
'Heil'
|
35
|
-
when :text
|
36
|
-
'BIG text'
|
37
|
-
when :time
|
38
|
-
DateTime.now.to_time
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
data/lib/performing.rb
DELETED
@@ -1,41 +0,0 @@
|
|
1
|
-
class Performing
|
2
|
-
def initialize(model, attrs)
|
3
|
-
@attrs = attrs
|
4
|
-
@model = model
|
5
|
-
@object = model.new
|
6
|
-
end
|
7
|
-
|
8
|
-
def call
|
9
|
-
set_default_values
|
10
|
-
set_custom_values if attrs.any?
|
11
|
-
|
12
|
-
object.save!
|
13
|
-
end
|
14
|
-
|
15
|
-
private
|
16
|
-
|
17
|
-
attr_reader :attrs
|
18
|
-
attr_reader :model
|
19
|
-
attr_reader :object
|
20
|
-
|
21
|
-
def set_default_values
|
22
|
-
model.columns.each do |column|
|
23
|
-
column_name = column.name
|
24
|
-
column_type = column.type
|
25
|
-
|
26
|
-
object[column_name] = generate_value(column_type)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
def set_custom_values
|
31
|
-
attrs.each do |attr|
|
32
|
-
key = attr.first
|
33
|
-
value = attr.last
|
34
|
-
object[key.to_s] = value
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
def generate_value(column_type)
|
39
|
-
Generating.generate_value(column_type)
|
40
|
-
end
|
41
|
-
end
|