smart_seeds 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6b75ec3ecda89cc2bbdbbf178a340b784a4e0e35
4
- data.tar.gz: 37d14c7fde26f1f955e5fcd1ff442e9816d7c5c3
3
+ metadata.gz: 96bc7299e41923854e4e8c8ba348c17d5cb967c9
4
+ data.tar.gz: a5eb2cba7a94885343293847033e5981bddff985
5
5
  SHA512:
6
- metadata.gz: 2083733835fd4d75228bc3dd45899ccc86265deb198cfea93d74029591f7ce66acf318109e17c71eaba7ead536c36c38525dadc9b45cf21c1bab3e2bcd57a1c9
7
- data.tar.gz: f2624498873d672b76bb2ef43c3412d63e06bd2d99dc8ca711caae896f42b0fff57015af01df5d576049c25dc79fc19ef4bb6a00948eee4a062c16f12b836cf5
6
+ metadata.gz: 61c166b9b57cce3f760ae108e14dd69ede4c6328f0e7c3d9e9a0051ad521ec921c69dd0009edb519b73d94cf7d6bbd6feb2c403b99e84189c7d95bf10c2b8865
7
+ data.tar.gz: 41296cd2f9ade2890ffe6e79f08e0672e8634b1372c8f4f4128ec3f80b9ffe3d07e9e6d874f305b6cde11ec2287409de08dc1ef8db2aa64f2e7d0e6229fe8e29
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # SmartSeeds
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.
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 we've got an object in a database:
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 a AR model which called like a some Faker class and an attribute called like a some faker method in that class
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 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).
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
@@ -16,7 +16,6 @@ require 'smart_seeds/generator/faker'
16
16
 
17
17
  module SmartSeeds
18
18
  def self.plant(model, attrs={})
19
- perform = SmartSeeds::Performing.new(model, attrs)
20
- perform.call
19
+ SmartSeeds::Performing.new(model, attrs).start
21
20
  end
22
21
  end
@@ -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
- 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
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 generate_via_faker
21
+ def generate_faker_value
35
22
  SmartSeeds::Generator::Faker.new(column, model).generate_value
36
23
  end
37
24
  end
@@ -7,7 +7,7 @@ module SmartSeeds
7
7
  end
8
8
 
9
9
  def generate_value
10
- [true, false].sample
10
+ true
11
11
  end
12
12
  end
13
13
  end
@@ -1,3 +1,5 @@
1
+ require 'faker'
2
+
1
3
  module SmartSeeds
2
4
  module Generator
3
5
  class Faker < SmartSeeds::Generator::Base
@@ -6,12 +6,30 @@ module SmartSeeds
6
6
  end
7
7
 
8
8
  def generate_value
9
- klass = column.name.split('_').first.capitalize.constantize
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 column.name.split('_').last == 'id'
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
@@ -7,7 +7,11 @@ module SmartSeeds
7
7
  end
8
8
 
9
9
  def generate_value
10
- ::Faker::Lorem.word
10
+ if is_comatible_with_faker?
11
+ generate_faker_value
12
+ else
13
+ ::Faker::Lorem.word.capitalize
14
+ end
11
15
  end
12
16
  end
13
17
  end
@@ -7,7 +7,11 @@ module SmartSeeds
7
7
  end
8
8
 
9
9
  def generate_value
10
- ::Faker::Lorem.paragraph
10
+ if is_comatible_with_faker?
11
+ generate_faker_value
12
+ else
13
+ ::Faker::Lorem.paragraph
14
+ end
11
15
  end
12
16
  end
13
17
  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 call
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
- attr_reader :model
26
- attr_reader :object
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::Base.new(column, model).generate_value
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
- SmartSeeds::Performing::SKIPPABLE_COLUMN_NAMES.include?(column_name)
56
+ skippable_attributes.include?(column_name)
49
57
  end
50
58
  end
51
59
  end
@@ -1,3 +1,3 @@
1
1
  module SmartSeeds
2
- VERSION = '0.1.3'
2
+ VERSION = '0.1.4'
3
3
  end
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.3
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-23 00:00:00.000000000 Z
11
+ date: 2017-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails