smart_seeds 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1df4a9fffe7d597b387d516eeaf31ffb4d40ce59
4
- data.tar.gz: a8ca6dcf4effbe2e6a11b1eba2e83c8130b5f2c9
3
+ metadata.gz: ca1778f758a9eab74bd9fb47cb40f4862f2238b5
4
+ data.tar.gz: 0a42feefde292e962be94b6271cab72d29330f36
5
5
  SHA512:
6
- metadata.gz: c688c984175cd36ddaf7f7cec366c785e732e5c059576cabdef5d066f7715809aabf5e6dd8279d589ee96bb3060444da4d72cf6788da786f20642332598278fd
7
- data.tar.gz: 14d30ceec3e842c336b7c526bffb37bc114072dcabb3d1f4232d7a8cf7c8c3d8f12777c67ad6e686a9777a0fe3630fe3789642e600a5990211097e1bfbbfe600
6
+ metadata.gz: ce4e9b0a682050a6bf1826d7aa12de549fd6b5e0daecf0e5a07e39728e1f9b1fb961dacad9dc752c3b2be2842ba6721522aa4cd2b708ff21533dca476b35f368
7
+ data.tar.gz: 324d62e05060eae5440e81945071973303c3dee7ede347de149cd6131680c005dd3b08b860c52ee348842ff3bdcfd8951575f618c4fc2fc147829de5b2e04eaf
data/README.md CHANGED
@@ -1,7 +1,9 @@
1
1
  # SmartSeeds
2
- Gem for auto-smart generating the seeds in your ruby app. Compatible only with ActiveRecord.
2
+ Gem for smart auto generating of new records with random values for your ruby app.
3
3
 
4
- It determines types of fields(attributes) in an AR model and generates random values for each of these attributes.
4
+ It determines types of fields(attributes) of an ActiveRecord model and creates records with random values for each of these attributes.
5
+
6
+ The gem has simple integration with Faker and supports foreign_keys, enums, etc.
5
7
 
6
8
  ## Usage
7
9
 
@@ -9,29 +11,21 @@ For example `Entity` model has `name`, `age`, `is_human` attributes.
9
11
 
10
12
  All you need to do is run this command: `SmartSeeds.plant(Entity)`
11
13
  ```
12
- > SmartSeeds.plant(Entity)
14
+ > SmartSeeds.plant(Entity)
13
15
  (0.1ms) begin transaction
14
- 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]]
15
- (1.8ms) commit transaction
16
- => true
17
-
18
- ```
19
- And you've got an object in a database:
20
- ```
21
- > Entity.last
22
- Entity Load (0.2ms) SELECT "entities".* FROM "entities" ORDER BY "entities"."id" DESC LIMIT ? [["LIMIT", 1]]
23
- => #<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">
24
-
16
+ SQL (0.5ms) INSERT INTO "entities" ("name", "age", "is_human", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?) [["name", "Numquam"], ["age", 60246], ["is_human", true], ["created_at", 2017-01-30 15:10:00 UTC], ["updated_at", 2017-01-30 15:10:00 UTC]]
17
+ (0.7ms) commit transaction
18
+ => #<Entity id: 1, name: "Numquam", age: 1488, is_human: false, created_at: "2017-01-30 15:10:00", updated_at: "2017-01-30 15:10:00">
25
19
  ```
26
20
  ### Custom values
27
- If you want to override some default values, you can send it in a hash:
21
+ If you want to override some default random values, you can send them in a hash:
28
22
 
29
23
  ```
30
- > SmartSeeds.plant(Entity, {name: 'Aleah'})
31
- (0.1ms) begin transaction
32
- 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]]
33
- (1.6ms) commit transaction
34
- => true
24
+ > SmartSeeds.plant(Entity, {name: 'Aleah'})
25
+ (0.7ms) begin transaction
26
+ SQL (1.9ms) INSERT INTO "entities" ("name", "age", "is_human", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?) [["name", "Aleah"], ["age", 15166], ["is_human", false], ["created_at", 2017-01-30 15:11:16 UTC], ["updated_at", 2017-01-30 15:11:16 UTC]]
27
+ (0.7ms) commit transaction
28
+ => #<Entity id: 2, name: "Aleah", age: 39, is_human: true, created_at: "2017-01-30 15:11:16", updated_at: "2017-01-30 15:11:16">
35
29
  ```
36
30
 
37
31
  By the way, all attributes which has default values on AR side will be skipped by default.
@@ -52,11 +46,18 @@ For example: Model `Superhero` has `name` and `power` attributes, so the value w
52
46
 
53
47
  ```
54
48
 
49
+ ### Size
50
+ You can set a size and generate few records also
51
+ `SmartSeeds.plant(Entity, {}, 1000)`
52
+ ```
53
+ => "Done! 1000 entities was/were planted."
54
+ ```
55
+
55
56
  ## Installation
56
57
  Add this line to your application's Gemfile:
57
58
 
58
59
  ```ruby
59
- gem 'smart_seeds'
60
+ gem 'smart_seeds', '~> 0.1.7'
60
61
  ```
61
62
 
62
63
  And then execute:
@@ -1,4 +1,5 @@
1
1
  require 'faker'
2
+ require 'bulk_insert'
2
3
  require 'smart_seeds/performing'
3
4
  require 'smart_seeds/generator/base'
4
5
  require 'smart_seeds/generator/binary'
@@ -16,7 +17,7 @@ require 'smart_seeds/generator/integer/foreign_key'
16
17
  require 'smart_seeds/generator/faker'
17
18
 
18
19
  module SmartSeeds
19
- def self.plant(model, attrs={})
20
- SmartSeeds::Performing.new(model, attrs).start
20
+ def self.plant(model, attrs={}, size=1)
21
+ SmartSeeds::Performing.new(model, attrs, size).start
21
22
  end
22
23
  end
@@ -18,7 +18,7 @@ module SmartSeeds
18
18
  private
19
19
 
20
20
  def faker_classes_include_model_name?
21
- # Check if Faker has a Class which called as a AR Model
21
+ # Check if Faker has a Class which called as an AR Model
22
22
  faker_classes = ::Faker.constants.select {|c| ::Faker.const_get(c).is_a? Class}
23
23
  faker_classes.include? model.name.to_sym
24
24
  end
@@ -6,32 +6,17 @@ module SmartSeeds
6
6
  end
7
7
 
8
8
  def generate_value
9
- if is_an_enum?
10
- generate_enum
11
- elsif is_foreign_key?
12
- generate_foreign_key
9
+ enum = SmartSeeds::Generator::Enum.new(column, model)
10
+ foreign_key = SmartSeeds::Generator::ForeignKey.new(column, model)
11
+
12
+ if enum.enum?
13
+ enum.generate_value
14
+ elsif foreign_key.foreign_key?
15
+ foreign_key.generate_value
13
16
  else
14
17
  rand(1..66666)
15
18
  end
16
19
  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
20
  end
36
21
  end
37
22
  end
@@ -13,9 +13,9 @@ module SmartSeeds
13
13
  values_array.sample
14
14
  end
15
15
 
16
- def is_an_enum?
16
+ def enum?
17
17
  model.defined_enums.include?(column.name)
18
18
  end
19
19
  end
20
20
  end
21
- end
21
+ end
@@ -1,35 +1,55 @@
1
1
  module SmartSeeds
2
2
  module Generator
3
3
  class ForeignKey < SmartSeeds::Generator::Integer
4
+ include ActiveSupport::Inflector
5
+
4
6
  def initialize(column, model)
5
7
  super
6
8
  end
7
9
 
8
10
  def generate_value
9
- klass = convert_column_name.constantize
10
- klass.ids.sample
11
+ klass = determine_klass
12
+
13
+ if klass.ids.empty?
14
+ raise ActiveRecord::ActiveRecordError,
15
+ "There are no records in #{camelized_model_name}. You have to add those first. You can run SmartSeeds.plant(#{camelized_model_name})"
16
+ else
17
+ klass.ids.sample
18
+ end
11
19
  end
12
20
 
13
- def is_a_foreign_key?
14
- true if splitted_column_name.last == 'id'
21
+ def foreign_key?
22
+ true if associations_include_column?
15
23
  end
16
24
 
17
25
  private
18
26
 
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)
27
+ def determine_klass
28
+ assocation = model_associations.select { |obj| obj.name == column_name_without_id.to_sym }
29
+
30
+ # if a class_name has been overrided in a model's class
31
+ if assocation.first.options.key? :class_name
32
+ assocation.first.options[:class_name].constantize
26
33
  else
27
- splitted_column_name.first.capitalize
34
+ camelized_model_name
28
35
  end
29
36
  end
30
37
 
31
- def splitted_column_name
32
- column.name.split('_')
38
+ def model_associations
39
+ model.reflect_on_all_associations(:belongs_to)
40
+ end
41
+
42
+ def column_name_without_id
43
+ column.name.sub(/^*_id/, '')
44
+ end
45
+
46
+ def associations_include_column?
47
+ model_associations.map(&:name).include?(column_name_without_id.to_sym)
48
+ end
49
+
50
+ def camelized_model_name
51
+ # converts underscore words like big_category to BigCategory constant
52
+ camelize(column_name_without_id).constantize
33
53
  end
34
54
  end
35
55
  end
@@ -1,27 +1,32 @@
1
1
  module SmartSeeds
2
2
  class Performing
3
- def initialize(model, attrs)
3
+ include ActiveSupport::Inflector
4
+
5
+ def initialize(model, attrs, size)
4
6
  @attrs = attrs
5
7
  @model = model
6
8
  @object = model.new
7
- @skippable_attributes = %w(id created_at updated_at)
9
+ @size = size
10
+ @skippable_attributes = %w(id)
8
11
  end
9
12
 
10
13
  def start
11
- add_skippable_attributes
12
- set_default_values
14
+ size.times do
15
+ add_skippable_attributes
16
+ set_default_values
13
17
 
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?
18
+ # User can send custom values in a hash: SmartSeeds.plant(Entity, {name: 'Aleah'})
19
+ # This method overrides default values to custom('name' in the example above)
20
+ set_custom_values if attrs.any?
21
+ object.save
22
+ end
17
23
 
18
- object.save!
19
- object
24
+ return "Done! #{size} #{model.name.downcase.pluralize(size)} was/were planted."
20
25
  end
21
26
 
22
27
  private
23
28
 
24
- attr_reader :attrs, :model, :object, :skippable_attributes
29
+ attr_reader :attrs, :model, :object, :size, :skippable_attributes, :worker
25
30
 
26
31
  def add_skippable_attributes
27
32
  # All default attributes which defined in AR object should be skipped by default
@@ -56,4 +61,4 @@ module SmartSeeds
56
61
  skippable_attributes.include?(column_name)
57
62
  end
58
63
  end
59
- end
64
+ end
@@ -1,3 +1,3 @@
1
1
  module SmartSeeds
2
- VERSION = '0.1.6'
2
+ VERSION = '0.1.7'
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.6
4
+ version: 0.1.7
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-27 00:00:00.000000000 Z
11
+ date: 2017-06-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -66,7 +66,8 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
- description: Description of Autoseed.
69
+ description: It determines types of fields(attributes) of an ActiveRecord model and
70
+ creates records with random values for each of these attributes.
70
71
  email:
71
72
  - vitaliy.fry@gmail.com
72
73
  executables: []
@@ -113,9 +114,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
113
114
  version: '0'
114
115
  requirements: []
115
116
  rubyforge_project:
116
- rubygems_version: 2.5.1
117
+ rubygems_version: 2.6.8
117
118
  signing_key:
118
119
  specification_version: 4
119
- summary: Summary of Autoseed.
120
+ summary: Gem for smart auto generating of new records with random values for your
121
+ ruby app.
120
122
  test_files: []
121
- has_rdoc: