lazy_fixtures 0.0.1 → 0.0.2

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: ee9e13f450b9456b2a3b44be2581ef9ee8052513
4
- data.tar.gz: 74a2bd5481e952ed8f0ccdb4cb294ccf046ab427
3
+ metadata.gz: d79e9b85c9bc3cfe4caf26ad1b1bb1cadaddded8
4
+ data.tar.gz: d040abd87d4291d35f1f8628e8851bcf7d952b89
5
5
  SHA512:
6
- metadata.gz: 21ac1dbc12c3fa363ae4161a56d13fc98a018bfaeb85c45a47f65ea6172def1226e071dd1b177b28e3fc621f38f30fa4f545ad79aafaf65bf7d0dd96f86ba893
7
- data.tar.gz: c3d01fabd019fdaa37d4c796992f48e6f14267896b6e9042394b8330aab61a476a41597e2415fb697d8e4920ddc2a697fc5a6b9de9aa160f6d2fbf3f622a7168
6
+ metadata.gz: b96f474825907f0f10f98d215aa6b6f165fffc19d0683f7ac063e35b3293f8768ef41fd53784ae5d3bd0966fd6d6cc4ac2c01c0a2426e77d5b99130ef8fbf20e
7
+ data.tar.gz: e0a4851d561cd868aaaa369afc647bf34dc91afcdc953051b125bd441fe3e924708356ebcfb25b8ec19620b32bf63d265e81881252f5c73b3fa112932a760446
data/.gitignore CHANGED
@@ -14,3 +14,4 @@
14
14
  .idea
15
15
  *.DS_Store
16
16
  mkmf.log
17
+ ./tmp/test
data/README.md CHANGED
@@ -3,6 +3,7 @@
3
3
 
4
4
  Generator to create factory girl fixtures, just with a simple ActiveRecord object
5
5
  from your current database.
6
+
6
7
  ## Installation
7
8
 
8
9
  Add this line to your application's Gemfile:
@@ -21,11 +22,13 @@ Or install it yourself as:
21
22
 
22
23
  ## Usage
23
24
 
24
- Inside your console simple type `LazyFixtures.generate()` inside the parenthesis place an ActiveRecord object, and this command will generate the corresponding file inside your project.
25
+ Inside your console simple type `LazyFixtures.generate(instance)` replace instance
26
+ with any ActiveRecord object form your current database, lazy_fixtures
27
+ will generate the corresponding file inside and fixture your project.
25
28
 
26
29
  ## Initialisation
27
30
 
28
- If you are using this gem inside a rails project you can use the initialise folder you can add a file to configure some attributes that will help the gem work properly.
31
+ Configuration of the gem goes inside an initilizer.
29
32
 
30
33
  ```ruby
31
34
  require 'factory_girl'
@@ -34,44 +37,47 @@ FactoryGirl.factories.clear
34
37
  FactoryGirl.find_definitions
35
38
 
36
39
  LazyFixtures.configure do |config|
37
- config.factory_directory = "#{specify which folder are your fixtures save}"
40
+ config.factory_directory = "spec/factories"
38
41
  config.factory_names = FactoryGirl.factories.map(&:name)
39
42
  end
40
43
  ```
41
44
 
42
- This lines will tell the gem where to store the files and it will give them a list with your actual Factory Girl fixtures, with this it will avoid factory girl exceptions.
45
+ This lines will tell the gem where to store the files and it will give them a list
46
+ with your actual Factory Girl fixtures, with this it will avoid factory girl exceptions.
43
47
 
44
48
  ## Options
45
49
 
46
- There are several options you can pass to the generator, the method will receive these options as a hash so the order is not redundant.
50
+ There are several options you can pass to the generator
47
51
 
48
- 1. **nested** => by default is set to false, set this to true and will traverse all the associations from your object and will create does fixtures for you as well.
49
- 2. **overwrite** => by default is set to false, set this to true and will overwrite does file with same name inside the fixtures folder that you specify. If for some reason it find a file with the same name and the option is set to false it will ask you if you want to overwrite. That choice is up to you :-)
52
+ 1. **nested** => by default is set to false, set this to true and will traverse all the associations from your object and will create those fixtures for you as well.
53
+ 2. **overwrite** => by default is set to false, set this to true and will overwrite the file with same name inside the fixtures folder that you specify. If for some reason it finds a file with the same name and the option is set to false it will ask you if you want to overwrite. That choice is up to you :-)
50
54
  3. **create** => by default is set to true, this options tell the generator to create the file, if is set to false it will not generate any file.
51
55
  4. **skip_attr** => this accept an array of string which will remove those attributes from the object.
52
56
  5. **change_attr** => this accepts a hash with new values for the fixture you want to set.
53
57
 
54
58
  ## Examples
55
59
 
56
- ###This will create a switch fixture:
60
+ ###This will create a client fixture:
57
61
 
58
- `LazyFixtures.generate(Switch.last)`
62
+ `LazyFixtures.generate(Client.last)`
59
63
 
60
- ###This will create a switch fixture and traverse all the associations:
64
+ ###This will create a client fixture and traverse all the associations:
61
65
 
62
- `LazyFixtures.generate(Switch.last, nested: true)`
66
+ `LazyFixtures.generate(Client.last, nested: true)`
63
67
 
64
- ###This will create a switch fixture with skipped attributes:
68
+ ###This will create a client fixture with skipped attributes:
65
69
 
66
- `LazyFixtures.generate(Switch.last, skip_attr:['age', 'name'])`
70
+ `LazyFixtures.generate(Client.last, skip_attr:['age', 'name'])`
67
71
 
68
- ###This will create a switch fixture with custom values:
72
+ ###This will create a client fixture with custom values:
69
73
 
70
- `LazyFixtures.generate(Switch.last, change_attr:{'age' => 34, 'name' => 'John'})`
74
+ `LazyFixtures.generate(Client.last, change_attr:{'age' => 34, 'name' => 'John'})`
71
75
 
72
76
  ##Notes
73
- This version is the first version, I want to add more functionality to it, skipping and changing nested attributes from the object, and many more.
74
- If you use the gem I see any errors or some functionality that might be useful please let me know.
77
+ This version is the first version, I want to add more functionality to it, skipping
78
+ and changing nested attributes from the object, and many more.
79
+ If you use the gem I see any errors or some functionality that might be useful
80
+ please let me know.
75
81
  Thanks.
76
82
 
77
83
 
@@ -13,7 +13,7 @@ Gem::Specification.new do |spec|
13
13
  with different options you will be able to create a full compatible
14
14
  factory tree from your database
15
15
  }
16
- spec.homepage = ""
16
+ spec.homepage = "https://github.com/GustavoCaso/lazy_fixtures"
17
17
  spec.license = "MIT"
18
18
 
19
19
  spec.files = `git ls-files -z`.split("\x0")
@@ -23,6 +23,7 @@ Gem::Specification.new do |spec|
23
23
 
24
24
  spec.add_development_dependency "bundler", "~> 1.7"
25
25
  spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "pry"
26
27
  spec.add_development_dependency "activerecord"
27
28
  spec.add_development_dependency "sqlite3"
28
29
  spec.add_development_dependency "rspec"
@@ -1,9 +1,10 @@
1
1
  require 'lazy_fixtures/version'
2
2
  require 'lazy_fixtures/generator'
3
- require 'lazy_fixtures/association_manager'
4
- require 'lazy_fixtures/file_manager'
5
- require 'lazy_fixtures/value_mapper'
6
- require 'lazy_fixtures/attributes_manager'
3
+ require 'lazy_fixtures/factory_girl/factory_girl'
4
+ require 'lazy_fixtures/factory_girl/association_manager'
5
+ require 'lazy_fixtures/factory_girl/file_manager'
6
+ require 'lazy_fixtures/factory_girl/value_mapper'
7
+ require 'lazy_fixtures/factory_girl/attributes_manager'
7
8
 
8
9
  module LazyFixtures
9
10
  class << self
@@ -25,6 +26,6 @@ module LazyFixtures
25
26
  end
26
27
 
27
28
  def self.generate(object, options = {})
28
- LazyFixtures::Generator.new(object, options).generate
29
+ LazyFixtures::Generator.new(object, options).generate('FactoryGirl')
29
30
  end
30
31
  end
@@ -0,0 +1,36 @@
1
+ module LazyFixtures
2
+ module FactoryGirl
3
+ class AssociationManager
4
+ include LazyFixtures::ObjectHelper
5
+
6
+ attr_reader :object
7
+ def initialize(object)
8
+ @object = object
9
+ @klass = @object.class.name.constantize
10
+ end
11
+
12
+ def get_associations
13
+ columns_info.keys.map do |method|
14
+ get_object(object.send(method))
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def columns_info
21
+ @klass.reflections.keys.map do |x|
22
+ reflection = @klass.reflections[x]
23
+ [
24
+ x.to_s,
25
+ {
26
+ method: x.to_s,
27
+ macro: reflection.macro,
28
+ klass: reflection.class_name
29
+ }
30
+ ]
31
+ end.to_h
32
+ end
33
+
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,40 @@
1
+ module LazyFixtures
2
+ module FactoryGirl
3
+ class AttributesManager
4
+
5
+ attr_accessor :attributes
6
+ attr_reader :options, :object
7
+
8
+ def initialize(object, options={})
9
+ @object = object
10
+ @attributes = @object.attributes
11
+ @options = options
12
+ manipulate_attributes
13
+ end
14
+
15
+ def each
16
+ attributes
17
+ end
18
+
19
+ def stringify_attributes
20
+ attributes.map do |k,v|
21
+ value = ValueMapper.new(object, k.to_s.dup, v).get_value
22
+ key = ValueMapper.remove_encrypted(k.to_s.dup)
23
+ " #{key} #{value}\n"
24
+ end.join
25
+ end
26
+
27
+ def delete_association_attributes
28
+ attributes.delete_if {|k,v| k =~ Regexp.new(/.+_id/) && !v.nil?}
29
+ end
30
+
31
+ private
32
+
33
+ def manipulate_attributes
34
+ options[:skip_attr].each { |x| attributes.delete(x)} if options[:skip_attr].any?
35
+ attributes.merge!(options[:change_attr]) if options[:change_attr].any?
36
+ delete_association_attributes
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,78 @@
1
+ module LazyFixtures
2
+ module FactoryGirl
3
+ class FactoryGirl
4
+ attr_reader :object, :options
5
+
6
+ def initialize(obejct, options)
7
+ @object = obejct
8
+ @options = options
9
+ end
10
+
11
+ def generate
12
+ if create_file
13
+ @file.write_file(generate_factory(
14
+ AttributesManager.new(object, options).
15
+ stringify_attributes)
16
+ )
17
+ end
18
+ run_associations if options[:nested]
19
+ end
20
+
21
+ private
22
+
23
+ def generate_factory(text)
24
+ factory_name = get_factory_name
25
+ <<-EOF
26
+ FactoryGirl.define do
27
+ factory :#{factory_name}, class: #{class_name.constantize} do
28
+ #{text.chomp}
29
+ end
30
+ end
31
+ EOF
32
+ end
33
+
34
+ def create_file
35
+ @file ||= FileManager.new(class_name_underscore, options)
36
+ @file.create_file
37
+ end
38
+
39
+ def get_factory_name
40
+ if global_factory_names.include?(class_name_underscore)
41
+ "#{class_name_underscore}_new"
42
+ else
43
+ class_name_underscore
44
+ end
45
+ end
46
+
47
+ def run_associations
48
+ get_associations.each do |association|
49
+ unless options[:parent].include? association.class.name
50
+ (options[:parent] << association.class.name).uniq!
51
+ LazyFixtures::Generator.new(association, nested: true, parent: options[:parent]).
52
+ generate('FactoryGirl')
53
+ end
54
+ end
55
+ end
56
+
57
+ def get_associations
58
+ association.get_associations
59
+ end
60
+
61
+ def association
62
+ AssociationManager.new(object)
63
+ end
64
+
65
+ def global_factory_names
66
+ LazyFixtures.configuration.factory_names
67
+ end
68
+
69
+ def class_name
70
+ object.class.name
71
+ end
72
+
73
+ def class_name_underscore
74
+ class_name.underscore
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,52 @@
1
+ module LazyFixtures
2
+ module FactoryGirl
3
+ class FileManager
4
+ attr_reader :name, :file_path
5
+ def initialize(name, options)
6
+ @name = name
7
+ @options = options
8
+ @file_path = File.join(global_factory_directory, get_file_name)
9
+ end
10
+
11
+ def create_file
12
+ return unless @options[:create]
13
+ if check_existence? && !@options[:overwrite]
14
+ puts "There is a file already inside that folder with the same name #{get_file_name}"
15
+ puts "Would you like to overwrite that file? (yes/no)"
16
+ answer = gets.chomp
17
+ if answer == 'yes'
18
+ puts 'The generator is overwriting your file'
19
+ File.new(file_path, "w")
20
+ true
21
+ else
22
+ puts 'Exiting the program'
23
+ false
24
+ end
25
+ end
26
+ puts "creating new file under #{global_factory_directory}/#{get_file_name}"
27
+ File.new(file_path, "w")
28
+ true
29
+ end
30
+
31
+ def check_existence?
32
+ File.file?(file_path)
33
+ end
34
+
35
+ def write_file(text)
36
+ File.open(file_path, 'w') do |f|
37
+ f.write text
38
+ end
39
+ end
40
+
41
+ def global_factory_directory
42
+ LazyFixtures.configuration.factory_directory
43
+ end
44
+
45
+ def get_file_name
46
+ "#{name}.rb"
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+
@@ -0,0 +1,42 @@
1
+ module LazyFixtures
2
+ module FactoryGirl
3
+ class ValueMapper
4
+ attr_reader :object, :key, :object_class
5
+
6
+ def initialize(object, key, value)
7
+ @object = object
8
+ @object_class = @object.class.name.constantize
9
+ @key = key
10
+ @value = value
11
+ remove_encrypted_attributes
12
+ end
13
+
14
+ def remove_encrypted_attributes
15
+ if key =~ /(encrypted_)/
16
+ new_key = key.dup
17
+ new_key.slice!($1)
18
+ @value = object.send(new_key)
19
+ end
20
+ end
21
+
22
+ def get_value
23
+ type = object_class.columns_hash[key].type
24
+ if @value.nil?
25
+ "nil"
26
+ elsif type == :integer || type == :float
27
+ @value
28
+ else
29
+ "\'#{@value}\'"
30
+ end
31
+ end
32
+
33
+ def self.remove_encrypted(key)
34
+ return_key = key
35
+ if key =~ /(encrypted_)/
36
+ return_key.slice!($1)
37
+ end
38
+ return_key
39
+ end
40
+ end
41
+ end
42
+ end
@@ -1,7 +1,9 @@
1
+ require 'lazy_fixtures/object_helper'
2
+
1
3
  module LazyFixtures
2
4
  class Generator
3
-
4
- attr_reader :factory_body, :attributes, :options
5
+ include LazyFixtures::ObjectHelper
6
+ attr_reader :options, :object
5
7
 
6
8
  DEFAULT_OPTIONS = {
7
9
  nested: false,
@@ -15,81 +17,16 @@ module LazyFixtures
15
17
  def initialize(object, options = {})
16
18
  @options = DEFAULT_OPTIONS.merge(options)
17
19
  @object = get_object(object)
18
- @class_name = @object.class.name
19
- @attributes = @object.attributes
20
- @factory_body = ''
21
20
  end
22
21
 
23
- def generate
24
- # In this first implementation of the gem it only allow to manipulate the
25
- # first object
26
- attribute_manager.manipulate_attributes
27
- add_associations if @options[:nested]
28
- @factory_body += attribute_manager.add_attributes
29
- text = generate_factory
30
- if create_file
31
- @file.write_file(text)
32
- end
33
- self
22
+ def generate(type)
23
+ gather_class(type).new(object, options).generate
34
24
  end
35
25
 
36
- def generate_factory
37
- factory_name = get_factory_name
38
- <<-EOF
39
- FactoryGirl.define do
40
- factory :#{factory_name}, class: #{@class_name.constantize} do
41
- #{@factory_body}
42
- end
43
- end
44
- EOF
45
- end
46
-
47
- def add_associations
48
- association.columns_info.keys.each do |method|
49
- begin
50
- object = @object.send(method)
51
- object = get_object(object)
52
- object_class = object.class.name
53
- next if invalid_object?(object)
54
- parent_included = @options[:parent].include? object_class
55
- (@options[:parent] << object_class).uniq!
56
- self.class.new(object, nested: true, parent: @options[:parent]).generate unless parent_included
57
- @factory_body += association.determine_association(association.columns_info[method], object_class, method)
58
- attribute_manager.delete_association_attributes(method)
59
- rescue => e
60
- puts "There was an error creating the association #{e} => #{e.backtrace}"
61
- next
62
- end
63
- end
64
- end
65
-
66
- def get_object(object)
67
- object.respond_to?('first') ? object.first : object
68
- end
69
-
70
- def invalid_object?(object)
71
- object.nil? || (object.respond_to?('first') && object.empty?)
72
- end
73
-
74
- def create_file
75
- @file ||= FileManager.new(@object.class.name.downcase, @options)
76
- @file.create_file
77
- end
78
-
79
- def association
80
- AssociationManager.new(@object)
81
- end
82
-
83
- def attribute_manager
84
- @attribute_manager ||= AttributesManager.new(@object, @options)
85
- end
26
+ private
86
27
 
87
- def get_factory_name
88
- if LazyFixtures.configuration.factory_names.include?(@class_name.downcase)
89
- "#{@class_name.downcase}_new"
90
- else
91
- @class_name.downcase
92
- end
28
+ def gather_class(type)
29
+ "LazyFixtures::#{type}::#{type}".constantize
93
30
  end
94
31
  end
95
32
  end
@@ -0,0 +1,11 @@
1
+ module LazyFixtures
2
+ module ObjectHelper
3
+ def get_object(object)
4
+ object.respond_to?('first') ? object.first : object
5
+ end
6
+
7
+ def invalid_object?(object)
8
+ object.nil? || (object.respond_to?('first') && object.empty?)
9
+ end
10
+ end
11
+ end
@@ -1,3 +1,3 @@
1
1
  module LazyFixtures
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe LazyFixtures::FactoryGirl::AssociationManager do
4
+
5
+ before(:each) do
6
+ @user = User.create(name:'Evaristo', age: 67)
7
+ @post = Post.create(title:'Post title', post_body: 'This is not important', user_id: @user.id)
8
+ end
9
+
10
+ context '#get_associations' do
11
+ it 'return a array of associations' do
12
+ associations = described_class.new(@post).get_associations
13
+ expect(associations).to eq([@user])
14
+ end
15
+ end
16
+ end
@@ -1,11 +1,12 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe LazyFixtures::AttributesManager do
3
+ describe LazyFixtures::FactoryGirl::AttributesManager do
4
4
 
5
- it 'will return has with attributes' do
5
+ let(:default_options) { LazyFixtures::Generator::DEFAULT_OPTIONS }
6
+
7
+ it 'will return hash with attributes' do
6
8
  user = User.create(name: 'Caita', age: 57)
7
- attributes_manager = LazyFixtures::AttributesManager.new(user,
8
- LazyFixtures::Generator::DEFAULT_OPTIONS)
9
+ attributes_manager = described_class.new(user, default_options)
9
10
  expect_return = {'id' => user.id, 'name' => user.name,
10
11
  'age' => user.age, 'created_at' => user.created_at,
11
12
  'updated_at' => user.updated_at}
@@ -14,49 +15,46 @@ describe LazyFixtures::AttributesManager do
14
15
 
15
16
  it 'will return string with all the value from the object' do
16
17
  user = User.create(name: 'Caita', age: 57)
17
- attributes_manager = LazyFixtures::AttributesManager.new(user,
18
- LazyFixtures::Generator::DEFAULT_OPTIONS)
18
+ attributes_manager = described_class.new(user, default_options)
19
19
  expected_attributes = " id #{user.id}\n name 'Caita'\n age 57\n created_at '1986-09-29T00:00:00+00:00'\n updated_at '1986-09-29T00:00:00+00:00'\n"
20
- expect(attributes_manager.add_attributes).to eq expected_attributes
20
+ expect(attributes_manager.stringify_attributes).to eq expected_attributes
21
21
  end
22
22
 
23
23
  it 'will return string with all value from the object removing the encrypted part' do
24
24
  user = UserWithEncrypted.create(name: 'Eduardo', age: 60,
25
25
  encrypted_password: 'retvbhj')
26
- attributes_manager = LazyFixtures::AttributesManager.new(user,
27
- LazyFixtures::Generator::DEFAULT_OPTIONS)
28
-
26
+ attributes_manager = described_class.new(user, default_options)
29
27
  expected_attributes = " id #{user.id}\n name 'Eduardo'\n age 60\n password 'retvbhj'\n"
30
- expect(attributes_manager.add_attributes).to eq expected_attributes
28
+ expect(attributes_manager.stringify_attributes).to eq expected_attributes
31
29
  end
32
30
 
33
31
  it 'will skip attributes from the options hash' do
34
32
  user = User.create(name: 'Caita', age: 57)
35
- options = LazyFixtures::Generator::DEFAULT_OPTIONS.merge({skip_attr: ['age']})
36
- attributes_manager = LazyFixtures::AttributesManager.new(user, options)
33
+ options = default_options.merge({skip_attr: ['age']})
34
+ attributes_manager = described_class.new(user, options)
37
35
  expected_attributes = {'id' => user.id, 'name' => user.name,
38
36
  'created_at' => user.created_at,
39
37
  'updated_at' => user.updated_at}
40
- expect(attributes_manager.manipulate_attributes).to eq expected_attributes
38
+ expect(attributes_manager.each).to eq expected_attributes
41
39
  end
42
40
 
43
41
  it 'will modify attributes value from the options hash' do
44
42
  user = User.create(name: 'Caita', age: 57)
45
- options = LazyFixtures::Generator::DEFAULT_OPTIONS.merge({change_attr: {'age' => 109}})
46
- attributes_manager = LazyFixtures::AttributesManager.new(user, options)
43
+ options = default_options.merge({change_attr: {'age' => 109}})
44
+ attributes_manager = described_class.new(user, options)
47
45
  expected_attributes = {'id' => user.id, 'name' => user.name,
48
46
  'age' => 109, 'created_at' => user.created_at,
49
47
  'updated_at' => user.updated_at}
50
- expect(attributes_manager.manipulate_attributes).to eq expected_attributes
48
+ expect(attributes_manager.each).to eq expected_attributes
51
49
  end
52
50
 
53
51
  it 'will remove attributes from association which are not nill' do
54
52
  user = User.create(name: 'Caita', age: 57)
55
53
  post = Post.create(title:'Post title', post_body: 'This is not important', user_id: user.id)
56
- options = LazyFixtures::Generator::DEFAULT_OPTIONS
57
- attributes_manager = LazyFixtures::AttributesManager.new(post, options)
54
+ options = default_options
55
+ attributes_manager = described_class.new(post, options)
58
56
  expected_attributes = {'id' => post.id, 'title' => post.title,
59
57
  'post_body' => post.post_body}
60
- expect(attributes_manager.delete_association_attributes('user')).to eq expected_attributes
58
+ expect(attributes_manager.delete_association_attributes).to eq expected_attributes
61
59
  end
62
60
  end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ describe LazyFixtures::FactoryGirl::FactoryGirl do
4
+ let(:user) { User.create(name:'Evaristo', age: 67)}
5
+ let(:post) { Post.create(title:'Post title', post_body: 'This is not important', user_id: user.id) }
6
+
7
+ context 'Create FactoryGirl File' do
8
+ xit 'write the specific file' do
9
+ LazyFixtures::Generator.new(post, {create: false, nested: true}).generate('FactoryGirl')
10
+
11
+ expect(File.exist?("#{LazyFixtures.configuration.factory_directory}/post.rb"))
12
+ end
13
+ end
14
+ end
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe LazyFixtures::ValueMapper do
4
- describe '#map_values' do
3
+ describe LazyFixtures::FactoryGirl::ValueMapper do
4
+ describe '#get_value' do
5
5
  before(:each) do
6
6
  @user = User.create(name: 'Gustavo', age: 26)
7
7
  allow(@user).to receive(:columns_hash).and_return({})
@@ -9,31 +9,31 @@ describe LazyFixtures::ValueMapper do
9
9
 
10
10
  it 'return correct value format for integer' do
11
11
  allow({}).to receive(:type).and_return(:integer)
12
- value = LazyFixtures::ValueMapper.new(@user, 'id', 6).map_values
12
+ value = described_class.new(@user, 'id', 6).get_value
13
13
  expect(value).to eq 6
14
14
  end
15
15
 
16
16
  it 'return correct format for datetime' do
17
17
  allow({}).to receive(:type).and_return(:datetime)
18
- value = LazyFixtures::ValueMapper.new(@user, 'created_at', '1986-09-29T00:00:00+00:00').map_values
18
+ value = described_class.new(@user, 'created_at', '1986-09-29T00:00:00+00:00').get_value
19
19
  expect(value).to eq "'1986-09-29T00:00:00+00:00'"
20
20
  end
21
21
 
22
22
  it 'return correct format for string' do
23
23
  allow({}).to receive(:type).and_return(:string)
24
- value = LazyFixtures::ValueMapper.new(@user, 'name', 'Gustavo').map_values
24
+ value = described_class.new(@user, 'name', 'Gustavo').get_value
25
25
  expect(value).to eq "'Gustavo'"
26
26
  end
27
27
 
28
28
  it 'return correct format for float' do
29
29
  allow({}).to receive(:type).and_return(:float)
30
- value = LazyFixtures::ValueMapper.new(@user, 'id', 1.5).map_values
30
+ value = described_class.new(@user, 'id', 1.5).get_value
31
31
  expect(value).to eq 1.5
32
32
  end
33
33
 
34
34
  it 'return correct format for nil' do
35
35
  allow({}).to receive(:type).and_return(:string)
36
- value = LazyFixtures::ValueMapper.new(@user, 'name', nil).map_values
36
+ value = described_class.new(@user, 'name', nil).get_value
37
37
  expect(value).to eq "nil"
38
38
  end
39
39
  end
@@ -42,7 +42,7 @@ describe LazyFixtures::ValueMapper do
42
42
  it 'return non encrypted values' do
43
43
  dummy = Struct.new('User', :encrypted_name, :age, :name)
44
44
  test_user = dummy.new('dueucvwuevcbwbcw', 23, 'gustavo')
45
- value = LazyFixtures::ValueMapper.new(test_user, 'encrypted_name', 'dueucvwuevcbwbcw').remove_encrypted_attributes
45
+ value = described_class.new(test_user, 'encrypted_name', 'dueucvwuevcbwbcw').remove_encrypted_attributes
46
46
  expect(value).to eq 'gustavo'
47
47
  end
48
48
  end
@@ -50,9 +50,9 @@ describe LazyFixtures::ValueMapper do
50
50
  describe 'self#remove_encrypted' do
51
51
  it 'return key without encrypted in it' do
52
52
  key = 'encrypted_password'
53
- return_value = LazyFixtures::ValueMapper.remove_encrypted(key)
53
+ return_value = described_class.remove_encrypted(key)
54
54
  expect(return_value).to eq 'password'
55
55
  end
56
56
  end
57
57
 
58
- end
58
+ end
@@ -1,111 +1,17 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe LazyFixtures::Generator do
4
-
5
- describe 'attr_reader' do
6
- before(:each) do
7
- @user = User.create(name: 'Gustavo', age: 26)
8
- @factory = LazyFixtures::Generator.new(@user, create: false).generate
9
- end
10
-
11
- it '#attributes' do
12
- attributes = {
13
- "id" => @user.id,
14
- "name" => 'Gustavo',
15
- "age" => 26,
16
- "created_at" => DateTime.new(1986,9,29),
17
- "updated_at" => DateTime.new(1986,9,29)
18
- }
19
-
20
- expect(@factory.attributes).to eq attributes
21
- end
22
-
23
- it '#factory_body' do
24
- text = <<-EOF
25
- id #{@user.id}
26
- name 'Gustavo'
27
- age 26
28
- created_at '1986-09-29T00:00:00+00:00'
29
- updated_at '1986-09-29T00:00:00+00:00'
30
- EOF
31
-
32
- factory_body = @factory.factory_body
33
- expect(factory_body).to eq text
34
- end
35
-
36
- it '#options' do
37
- options = {
38
- nested: false,
39
- overwrite: false,
40
- create: false,
41
- parent: [],
42
- skip_attr: [],
43
- change_attr: {}
44
- }
45
- expect(@factory.options).to eq options
46
-
47
- end
48
- end
49
-
50
- describe 'generator methods' do
51
- before(:each) do
52
- @user = User.create(name: 'Gustavo', age: 26)
53
- end
54
-
55
- describe 'getter object and determine validity of object methods' do
56
-
4
+ describe 'Generator' do
5
+ context 'LazyFixtures::LazyFixtures::FactoryGirl' do
57
6
  before(:each) do
58
- @factory = LazyFixtures::Generator.new(@user, create: false)
7
+ @user = User.create(name: 'Gustavo', age: 26)
8
+ @generator = LazyFixtures::Generator.new(@user, create: false)
59
9
  end
60
10
 
61
- it '#invalid_object? will return true with nil value' do
62
- return_value = @factory.invalid_object?(nil)
63
- expect(return_value).to eq true
11
+ it 'receive generate message' do
12
+ expect_any_instance_of(LazyFixtures::FactoryGirl::FactoryGirl).to receive(:generate)
13
+ @generator.generate('FactoryGirl')
64
14
  end
65
-
66
- it '#invalid_object? will return true with empty value' do
67
- return_value = @factory.invalid_object?([])
68
- expect(return_value).to eq true
69
- end
70
-
71
- it '#invalid_object? will return false with valid value' do
72
- return_value = @factory.invalid_object?(@user)
73
- expect(return_value).to eq false
74
- end
75
-
76
- it '#get_object will return the object' do
77
- return_value = @factory.get_object(@user)
78
- expect(return_value).to eq @user
79
- end
80
-
81
- it '#get_object will return the object' do
82
- return_value = @factory.get_object(nil)
83
- expect(return_value).to eq nil
84
- end
85
-
86
- it '#get_object will return the object' do
87
- return_value = @factory.get_object([])
88
- expect(return_value).to eq nil
89
- end
90
-
91
- it '#get_object will return the object' do
92
- user2 = User.new(name: 'Test', age: 30)
93
- return_value = @factory.get_object([user2, @user])
94
- expect(return_value).to eq user2
95
- end
96
-
97
- it '#get_factory_name will return the name with appended string' do
98
- LazyFixtures.configuration.factory_names = ['user']
99
- return_value = @factory.get_factory_name
100
- expect(return_value).to eq 'user_new'
101
- end
102
-
103
- it '#get_factory_name will return the name' do
104
- LazyFixtures.configuration.factory_names = ['switch']
105
- return_value = @factory.get_factory_name
106
- expect(return_value).to eq 'user'
107
- end
108
-
109
15
  end
110
16
  end
111
- end
17
+ end
@@ -6,7 +6,7 @@ load File.dirname(__FILE__) + '/support/schema.rb'
6
6
  require File.dirname(__FILE__) +'/support/models'
7
7
 
8
8
  LazyFixtures.configure do |config|
9
- config.factory_directory = '.'
9
+ config.factory_directory = './tmp/test'
10
10
  config.factory_names = []
11
11
  end
12
12
 
@@ -18,4 +18,4 @@ RSpec.configure do |config|
18
18
  config.after(:each) do
19
19
  User.destroy_all
20
20
  end
21
- end
21
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lazy_fixtures
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - GustavoCaso
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-05 00:00:00.000000000 Z
11
+ date: 2015-11-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
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
  - !ruby/object:Gem::Dependency
42
56
  name: activerecord
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -97,20 +111,23 @@ files:
97
111
  - Rakefile
98
112
  - lazy_fixtures.gemspec
99
113
  - lib/lazy_fixtures.rb
100
- - lib/lazy_fixtures/association_manager.rb
101
- - lib/lazy_fixtures/attributes_manager.rb
102
- - lib/lazy_fixtures/file_manager.rb
114
+ - lib/lazy_fixtures/factory_girl/association_manager.rb
115
+ - lib/lazy_fixtures/factory_girl/attributes_manager.rb
116
+ - lib/lazy_fixtures/factory_girl/factory_girl.rb
117
+ - lib/lazy_fixtures/factory_girl/file_manager.rb
118
+ - lib/lazy_fixtures/factory_girl/value_mapper.rb
103
119
  - lib/lazy_fixtures/generator.rb
104
- - lib/lazy_fixtures/value_mapper.rb
120
+ - lib/lazy_fixtures/object_helper.rb
105
121
  - lib/lazy_fixtures/version.rb
106
- - spec/association_manager_spec.rb
107
- - spec/attributes_manager_spec.rb
122
+ - spec/factory_girl/association_manager_spec.rb
123
+ - spec/factory_girl/attributes_manager_spec.rb
124
+ - spec/factory_girl/factory_girl_spec.rb
125
+ - spec/factory_girl/value_mapper_spec.rb
108
126
  - spec/generator_spec.rb
109
127
  - spec/spec_helper.rb
110
128
  - spec/support/models.rb
111
129
  - spec/support/schema.rb
112
- - spec/value_mapper_spec.rb
113
- homepage: ''
130
+ homepage: https://github.com/GustavoCaso/lazy_fixtures
114
131
  licenses:
115
132
  - MIT
116
133
  metadata: {}
@@ -130,15 +147,16 @@ required_rubygems_version: !ruby/object:Gem::Requirement
130
147
  version: '0'
131
148
  requirements: []
132
149
  rubyforge_project:
133
- rubygems_version: 2.4.5
150
+ rubygems_version: 2.4.6
134
151
  signing_key:
135
152
  specification_version: 4
136
153
  summary: Easy generator to save time when creating them
137
154
  test_files:
138
- - spec/association_manager_spec.rb
139
- - spec/attributes_manager_spec.rb
155
+ - spec/factory_girl/association_manager_spec.rb
156
+ - spec/factory_girl/attributes_manager_spec.rb
157
+ - spec/factory_girl/factory_girl_spec.rb
158
+ - spec/factory_girl/value_mapper_spec.rb
140
159
  - spec/generator_spec.rb
141
160
  - spec/spec_helper.rb
142
161
  - spec/support/models.rb
143
162
  - spec/support/schema.rb
144
- - spec/value_mapper_spec.rb
@@ -1,45 +0,0 @@
1
- module LazyFixtures
2
- class AssociationManager
3
- def initialize(item)
4
- @item = item
5
- @klass = @item.class.name.constantize
6
- end
7
-
8
- def columns_info
9
- return_hash = {}
10
- @klass.reflections.keys.map do |x|
11
- reflection = @klass.reflections[x]
12
- return_hash[x.to_s]= {
13
- method: x,
14
- macro: reflection.macro,
15
- klass: reflection.class_name
16
- }
17
- end
18
- return_hash
19
- end
20
-
21
- def determine_association(association_info, class_name, method)
22
- relation = association_info[:macro]
23
- text = if relation == :belongs_to
24
- create_belongs_to_association(association_info[:klass], class_name, method)
25
- elsif relation == :has_many || relation == :has_and_belongs_to_many
26
- create_has_many_associations(association_info[:klass])
27
- end
28
- <<-EOF
29
- #{text}
30
- EOF
31
- end
32
-
33
- def create_belongs_to_association(klass, class_name, method)
34
- method = method == klass.downcase ? klass.downcase : method
35
- class_name = klass == class_name ? klass : class_name
36
- "association :#{method}, factory: :#{class_name.downcase}"
37
- end
38
-
39
- def create_has_many_associations(klass)
40
- %Q(after(:create) do |x|
41
- create_list(:#{klass.downcase}, 1, #{@item.class.name.downcase}: x)
42
- end)
43
- end
44
- end
45
- end
@@ -1,36 +0,0 @@
1
- module LazyFixtures
2
- class AttributesManager
3
-
4
- attr_reader :attributes
5
-
6
- def initialize(object, options={})
7
- @object = object
8
- @attributes = @object.attributes
9
- @options = options
10
- end
11
-
12
- def add_attributes
13
- return_value = ''
14
- attributes.each do |k,v|
15
- value = ValueMapper.new(@object, k.to_s.dup, v).map_values
16
- key = ValueMapper.remove_encrypted(k.to_s.dup)
17
- return_value += " #{key} #{value}\n"
18
- end
19
- return_value
20
- end
21
-
22
- def manipulate_attributes
23
- @options[:skip_attr].each { |x| attributes.delete(x)} unless @options[:skip_attr].empty?
24
- attributes.merge!(@options[:change_attr]) unless @options[:change_attr].empty?
25
- attributes
26
- end
27
-
28
- def delete_association_attributes(method)
29
- attributes.delete_if {|k,v| k =~ Regexp.new(method) && !v.nil?}
30
- end
31
-
32
- def each
33
- attributes
34
- end
35
- end
36
- end
@@ -1,40 +0,0 @@
1
- module LazyFixtures
2
- class FileManager
3
- def initialize(name, options)
4
- @name = name
5
- @options = options
6
- @title = "#{@name}.rb"
7
- @file_path = File.join(LazyFixtures.configuration.factory_directory, @title)
8
- end
9
-
10
- def create_file
11
- return unless @options[:create]
12
- if check_existence? && !@options[:overwrite]
13
- puts "There is a file already inside that folder with the same name #{@name}"
14
- puts "Would you like to overwrite that file? (yes/no)"
15
- answer = gets.chomp
16
- if answer == 'yes'
17
- puts 'The generator is overwriting your file'
18
- File.new(@file_path, "w")
19
- true
20
- else
21
- puts 'Exiting the program'
22
- false
23
- end
24
- end
25
- puts "creating new file under #{LazyFixtures.configuration.factory_directory}/#{@name}"
26
- File.new(@file_path, "w")
27
- true
28
- end
29
-
30
- def check_existence?
31
- File.file?(@file_path)
32
- end
33
-
34
- def write_file(text)
35
- File.open(@file_path, 'w') do |f|
36
- f.write text
37
- end
38
- end
39
- end
40
- end
@@ -1,38 +0,0 @@
1
- module LazyFixtures
2
- class ValueMapper
3
- def initialize(object, key, value)
4
- @object = object
5
- @object_class = @object.class.name.constantize
6
- @key = key
7
- @value = value
8
- remove_encrypted_attributes
9
- end
10
-
11
- def remove_encrypted_attributes
12
- if @key =~ /(encrypted_)/
13
- key = @key.dup
14
- key.slice!($1)
15
- @value = @object.send(key)
16
- end
17
- end
18
-
19
- def map_values
20
- type = @object_class.columns_hash[@key].type
21
- value = if @value.nil?
22
- "nil"
23
- elsif type == :string || type == :datetime || type == :text || type == :date
24
- "\'#{@value}\'"
25
- else
26
- @value
27
- end
28
- end
29
-
30
- def self.remove_encrypted(key)
31
- return_key = key
32
- if key =~ /(encrypted_)/
33
- return_key.slice!($1)
34
- end
35
- return_key
36
- end
37
- end
38
- end
@@ -1,48 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe LazyFixtures::AssociationManager do
4
-
5
- before(:each) do
6
- @user = User.create(name:'Evaristo', age: 67)
7
- @post = Post.create(title:'Post title', post_body: 'This is not important', user_id: 3)
8
- end
9
-
10
- describe '#columns_info' do
11
- it 'will return hash with reflections information' do
12
- return_value = LazyFixtures::AssociationManager.new(@user).columns_info
13
- expect(return_value).to eq({'posts' => {method: :posts, macro: :has_many, klass: 'Post'}})
14
- end
15
-
16
- it 'will return hash with reflections information' do
17
- return_value = LazyFixtures::AssociationManager.new(@post).columns_info
18
- expect(return_value).to eq('user' => {method: :user, macro: :belongs_to, klass: 'User'})
19
- end
20
- end
21
-
22
- describe '#determine_association' do
23
- it 'will return text for belongs to association' do
24
- user_association_info = LazyFixtures::AssociationManager.new(@post).columns_info['user']
25
- return_value = LazyFixtures::AssociationManager.new(@post).determine_association(user_association_info, 'User', 'user')
26
- expect(return_value).to eq " association :user, factory: :user\n"
27
- end
28
-
29
- it 'will return text for belongs to association with right association' do
30
- user_association_info = LazyFixtures::AssociationManager.new(@post).columns_info['user']
31
- return_value = LazyFixtures::AssociationManager.new(@post).determine_association(user_association_info, 'User', 'writter')
32
- expect(return_value).to eq " association :writter, factory: :user\n"
33
- end
34
-
35
- it 'will return text for belongs to association with right factory' do
36
- user_association_info = LazyFixtures::AssociationManager.new(@post).columns_info['user']
37
- return_value = LazyFixtures::AssociationManager.new(@post).determine_association(user_association_info, 'Writter', 'user')
38
- expect(return_value).to eq " association :user, factory: :writter\n"
39
- end
40
-
41
-
42
- it 'will return text for has_many to association' do
43
- post_association_info = LazyFixtures::AssociationManager.new(@user).columns_info['posts']
44
- return_value = LazyFixtures::AssociationManager.new(@user).determine_association(post_association_info, 'Post', 'post')
45
- expect(return_value).to eq " after(:create) do |x|\n create_list(:post, 1, user: x)\n end\n"
46
- end
47
- end
48
- end