cockroach 0.0.0
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.
- data/.rvmrc +1 -0
- data/Gemfile +28 -0
- data/LICENSE.txt +22 -0
- data/README.md +124 -0
- data/Rakefile +37 -0
- data/VERSION +1 -0
- data/cockroach.gemspec +96 -0
- data/lib/cockroach.rb +43 -0
- data/lib/cockroach/base/load_nodes.rb +38 -0
- data/lib/cockroach/base/node.rb +192 -0
- data/lib/cockroach/base/node_structure.rb +11 -0
- data/lib/cockroach/config.rb +54 -0
- data/lib/cockroach/config/loader.rb +35 -0
- data/lib/cockroach/fixtures/factory_girl.rb +4 -0
- data/lib/cockroach/fixtures/factory_girl/loader.rb +43 -0
- data/lib/cockroach/fixtures/factory_girl/node.rb +51 -0
- data/lib/cockroach/fixtures/factory_girl/profiler.rb +34 -0
- data/lib/cockroach/railtie.rb +7 -0
- data/lib/cockroach/source.rb +40 -0
- data/lib/cockroach/source/model.rb +33 -0
- data/lib/cockroach/source/node.rb +27 -0
- data/lib/cockroach/version.rb +3 -0
- data/lib/generators/cockroach/install_generator.rb +20 -0
- data/lib/generators/cockroach/templates/cockroach.rb +8 -0
- data/lib/generators/cockroach/templates/faker.yml +16 -0
- data/lib/tasks/faker.rake +35 -0
- data/test/config/config_test.rb +129 -0
- data/test/config/loader_test.rb +42 -0
- data/test/fixturers/factory_girl/aliasing_test.rb +151 -0
- data/test/fixturers/factory_girl/loading_test.rb +45 -0
- data/test/fixturers/factory_girl/node_test.rb +386 -0
- data/test/fixturers/factory_girl/profiler_test.rb +124 -0
- data/test/fixturers/source_test.rb +153 -0
- data/test/generators/install_generator_test.rb +14 -0
- data/test/support/active_record.rb +11 -0
- data/test/support/data/correct_with_option.yml +13 -0
- data/test/support/data/correct_without_option.yml +10 -0
- data/test/support/data/dummy_structure/config/faker.yml +13 -0
- data/test/support/data/dummy_structure/config/user_only.yml +1 -0
- data/test/support/data/dummy_structure/config/witness.yml +6 -0
- data/test/support/data/dummy_structure/test/factories/user_factory.rb +16 -0
- data/test/support/database_cleaner.rb +13 -0
- data/test/support/factory_girl_mocked.rb +32 -0
- data/test/support/models/user.rb +23 -0
- data/test/test_helper.rb +26 -0
- metadata +151 -0
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use --create 1.9.3@artirix_faker
|
data/Gemfile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gem "activerecord", :require => false
|
4
|
+
gem "activesupport", :require => false
|
5
|
+
gem "faker"
|
6
|
+
|
7
|
+
group :development do
|
8
|
+
gem "yard", "~> 0.6.0"
|
9
|
+
gem "jeweler", "~> 1.6.4"
|
10
|
+
end
|
11
|
+
|
12
|
+
group :test do
|
13
|
+
gem "rails"
|
14
|
+
gem "test-unit"
|
15
|
+
gem 'shoulda-context'
|
16
|
+
gem "mocha"
|
17
|
+
|
18
|
+
gem "factory_girl"
|
19
|
+
|
20
|
+
gem "simplecov", "~> 0.6.4", :platform => :ruby_19
|
21
|
+
gem "rcov", "~> 1.0.0", :platform => :ruby_18
|
22
|
+
|
23
|
+
gem "debugger", "~> 1.1.3", :platform => :ruby_19
|
24
|
+
gem 'ruby-debug', :platform => :ruby_18
|
25
|
+
|
26
|
+
gem "database_cleaner", :group => :test
|
27
|
+
gem "sqlite3", :group => :test
|
28
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 TODO: Write your name
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
# Cockroach
|
2
|
+
|
3
|
+
Cockroach allows you to simplify faking the database, during development. Idea is simple:
|
4
|
+
There are ready fixtures in your database. **YOU SHOULD REUSE THEM!**
|
5
|
+
|
6
|
+
Well, one may say that you you should follow TDD/BDD, hence there is no need in this gem.
|
7
|
+
But wait, what about designers? Shall they follow this approach as well? You want to spend your time
|
8
|
+
explaining them the process? Sure, go ahead!
|
9
|
+
|
10
|
+
# Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
gem 'cockroach'
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it as:
|
21
|
+
|
22
|
+
$ gem install cockroach
|
23
|
+
|
24
|
+
# After instalation
|
25
|
+
|
26
|
+
After the gem was installed, all the required files may now be generated with Rails generator:
|
27
|
+
|
28
|
+
$ bundle exec rails g cockroach:install
|
29
|
+
|
30
|
+
That will create config/faker.yml
|
31
|
+
|
32
|
+
# Rake tasks
|
33
|
+
|
34
|
+
There are several rake tasks available:
|
35
|
+
|
36
|
+
rake cockroach:validate
|
37
|
+
|
38
|
+
The task above will validate your config file after you edited it.
|
39
|
+
|
40
|
+
rake cockroach:generate
|
41
|
+
|
42
|
+
After, you validated your file, you may now proceed to generation.
|
43
|
+
|
44
|
+
rake cockroach:reload
|
45
|
+
|
46
|
+
In case you would like to to have a new and nice database, just run the task above.
|
47
|
+
That will drop and create existing DB, apply all migrations, seed the database and generate fake data afterwards.
|
48
|
+
|
49
|
+
# Usage
|
50
|
+
|
51
|
+
Cockroach will try to find your fixtures first. Once it is done:
|
52
|
+
There are several ways how to specify the required amount of the records:
|
53
|
+
|
54
|
+
### First way is *simple*:
|
55
|
+
|
56
|
+
Just specify the amount of the records that are require or provide the range:
|
57
|
+
An example:
|
58
|
+
|
59
|
+
#a strict amount
|
60
|
+
users_amount: 1000 # what will generate 1000 records for user factory
|
61
|
+
|
62
|
+
#or a fuzzy way
|
63
|
+
feeds_ratio: 1000 # what will generate a random amount of users, in the range between 500 and 2000.
|
64
|
+
|
65
|
+
**amount** - will just generate the strict amount of record. In the example above,
|
66
|
+
there will be 1000 users in the database.
|
67
|
+
|
68
|
+
**ratio** - will pick a random amount of Feed records, within a range a range.
|
69
|
+
By default, the limitations are obeying the following principle:
|
70
|
+
`(specified_number/2, specified_number*2)`
|
71
|
+
NB! It will **ALWAYS** be flored Integer.
|
72
|
+
|
73
|
+
This method is helpful, once you are dealing with data structure you can consider as simple.
|
74
|
+
So you need to create a specific amount of records, without any subsequential creation.
|
75
|
+
|
76
|
+
### Next way is *simple subsequential*:
|
77
|
+
|
78
|
+
You can specify the amount of records one level under the record type:
|
79
|
+
An examples:
|
80
|
+
|
81
|
+
# a strict amount
|
82
|
+
business:
|
83
|
+
amount: 1000
|
84
|
+
|
85
|
+
# or fuzzy way:
|
86
|
+
users:
|
87
|
+
ratio: 1000
|
88
|
+
|
89
|
+
# or more fuzzy way:
|
90
|
+
users:
|
91
|
+
ratio:
|
92
|
+
lower_limit: 300
|
93
|
+
upper_limit: 10000
|
94
|
+
|
95
|
+
**amount** and **ratio** have same meaning as in the previous example.
|
96
|
+
In the last example, the amount will be a random number, within `(lower_limit..upper_limit)`
|
97
|
+
|
98
|
+
### Next way is *complicated subsequential*:
|
99
|
+
|
100
|
+
It is often required to generate a associated models, like in case when
|
101
|
+
City'es may have many associated Place'es.
|
102
|
+
A brief examples:
|
103
|
+
|
104
|
+
# simple case
|
105
|
+
cities:
|
106
|
+
amount: 1000
|
107
|
+
places_ratio: 300
|
108
|
+
|
109
|
+
# or a bit more complecated
|
110
|
+
cities:
|
111
|
+
amount: 1000
|
112
|
+
places:
|
113
|
+
ratio:
|
114
|
+
lower_limit: 100
|
115
|
+
upper_limit: 1000
|
116
|
+
address_amount: 1
|
117
|
+
|
118
|
+
### Contributing
|
119
|
+
|
120
|
+
1. Fork it
|
121
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
122
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
123
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
124
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'rake'
|
11
|
+
|
12
|
+
require 'jeweler'
|
13
|
+
Jeweler::Tasks.new do |gem|
|
14
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
15
|
+
gem.name = "cockroach"
|
16
|
+
gem.homepage = "https://github.com/artirix/cockroach"
|
17
|
+
gem.license = "MIT"
|
18
|
+
gem.summary = %Q{This gem allows one to fake all the date for the Artirix project}
|
19
|
+
gem.description = %Q{In order to simplify the life of the Artirix developers, this gem was build.
|
20
|
+
It allows to generate the data, and fill teh database with fake records.}
|
21
|
+
gem.email = "max.zab@artirix.com"
|
22
|
+
gem.authors = ["E-Max"]
|
23
|
+
# dependencies defined in Gemfile
|
24
|
+
end
|
25
|
+
Jeweler::RubygemsDotOrgTasks.new
|
26
|
+
|
27
|
+
require 'rake/testtask'
|
28
|
+
Rake::TestTask.new(:test) do |test|
|
29
|
+
test.libs << 'lib' << 'test'
|
30
|
+
test.pattern = 'test/**/*_test.rb'
|
31
|
+
test.verbose = true
|
32
|
+
end
|
33
|
+
|
34
|
+
task :default => :test
|
35
|
+
|
36
|
+
require 'yard'
|
37
|
+
YARD::Rake::YardocTask.new
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.0
|
data/cockroach.gemspec
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "cockroach"
|
8
|
+
s.version = "0.0.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["E-Max"]
|
12
|
+
s.date = "2012-10-12"
|
13
|
+
s.description = "In order to simplify the life of the Artirix developers, this gem was build.\n It allows to generate the data, and fill teh database with fake records."
|
14
|
+
s.email = "max.zab@artirix.com"
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.md"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".rvmrc",
|
21
|
+
"Gemfile",
|
22
|
+
"LICENSE.txt",
|
23
|
+
"README.md",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"cockroach.gemspec",
|
27
|
+
"lib/cockroach.rb",
|
28
|
+
"lib/cockroach/base/load_nodes.rb",
|
29
|
+
"lib/cockroach/base/node.rb",
|
30
|
+
"lib/cockroach/base/node_structure.rb",
|
31
|
+
"lib/cockroach/config.rb",
|
32
|
+
"lib/cockroach/config/loader.rb",
|
33
|
+
"lib/cockroach/fixtures/factory_girl.rb",
|
34
|
+
"lib/cockroach/fixtures/factory_girl/loader.rb",
|
35
|
+
"lib/cockroach/fixtures/factory_girl/node.rb",
|
36
|
+
"lib/cockroach/fixtures/factory_girl/profiler.rb",
|
37
|
+
"lib/cockroach/railtie.rb",
|
38
|
+
"lib/cockroach/source.rb",
|
39
|
+
"lib/cockroach/source/model.rb",
|
40
|
+
"lib/cockroach/source/node.rb",
|
41
|
+
"lib/cockroach/version.rb",
|
42
|
+
"lib/generators/cockroach/install_generator.rb",
|
43
|
+
"lib/generators/cockroach/templates/cockroach.rb",
|
44
|
+
"lib/generators/cockroach/templates/faker.yml",
|
45
|
+
"lib/tasks/faker.rake",
|
46
|
+
"test/config/config_test.rb",
|
47
|
+
"test/config/loader_test.rb",
|
48
|
+
"test/fixturers/factory_girl/aliasing_test.rb",
|
49
|
+
"test/fixturers/factory_girl/loading_test.rb",
|
50
|
+
"test/fixturers/factory_girl/node_test.rb",
|
51
|
+
"test/fixturers/factory_girl/profiler_test.rb",
|
52
|
+
"test/fixturers/source_test.rb",
|
53
|
+
"test/generators/install_generator_test.rb",
|
54
|
+
"test/support/active_record.rb",
|
55
|
+
"test/support/data/correct_with_option.yml",
|
56
|
+
"test/support/data/correct_without_option.yml",
|
57
|
+
"test/support/data/dummy_structure/config/faker.yml",
|
58
|
+
"test/support/data/dummy_structure/config/user_only.yml",
|
59
|
+
"test/support/data/dummy_structure/config/witness.yml",
|
60
|
+
"test/support/data/dummy_structure/test/factories/user_factory.rb",
|
61
|
+
"test/support/database_cleaner.rb",
|
62
|
+
"test/support/factory_girl_mocked.rb",
|
63
|
+
"test/support/models/user.rb",
|
64
|
+
"test/test_helper.rb"
|
65
|
+
]
|
66
|
+
s.homepage = "https://github.com/artirix/cockroach"
|
67
|
+
s.licenses = ["MIT"]
|
68
|
+
s.require_paths = ["lib"]
|
69
|
+
s.rubygems_version = "1.8.10"
|
70
|
+
s.summary = "This gem allows one to fake all the date for the Artirix project"
|
71
|
+
|
72
|
+
if s.respond_to? :specification_version then
|
73
|
+
s.specification_version = 3
|
74
|
+
|
75
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
76
|
+
s.add_runtime_dependency(%q<activerecord>, [">= 0"])
|
77
|
+
s.add_runtime_dependency(%q<activesupport>, [">= 0"])
|
78
|
+
s.add_runtime_dependency(%q<faker>, [">= 0"])
|
79
|
+
s.add_development_dependency(%q<yard>, ["~> 0.6.0"])
|
80
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
|
81
|
+
else
|
82
|
+
s.add_dependency(%q<activerecord>, [">= 0"])
|
83
|
+
s.add_dependency(%q<activesupport>, [">= 0"])
|
84
|
+
s.add_dependency(%q<faker>, [">= 0"])
|
85
|
+
s.add_dependency(%q<yard>, ["~> 0.6.0"])
|
86
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
87
|
+
end
|
88
|
+
else
|
89
|
+
s.add_dependency(%q<activerecord>, [">= 0"])
|
90
|
+
s.add_dependency(%q<activesupport>, [">= 0"])
|
91
|
+
s.add_dependency(%q<faker>, [">= 0"])
|
92
|
+
s.add_dependency(%q<yard>, ["~> 0.6.0"])
|
93
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
data/lib/cockroach.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require "active_support/dependencies/autoload"
|
2
|
+
require "active_support/core_ext/module"
|
3
|
+
require "cockroach/railtie" if defined?(Rails)
|
4
|
+
|
5
|
+
module Cockroach
|
6
|
+
extend ActiveSupport::Autoload
|
7
|
+
|
8
|
+
autoload :VERSION
|
9
|
+
autoload :Config
|
10
|
+
|
11
|
+
autoload :Source
|
12
|
+
|
13
|
+
module Base
|
14
|
+
autoload :Node, 'cockroach/base/node'
|
15
|
+
autoload :LoadNodes, 'cockroach/base/load_nodes'
|
16
|
+
end
|
17
|
+
|
18
|
+
autoload_under 'fixtures' do
|
19
|
+
autoload :FactoryGirl
|
20
|
+
end
|
21
|
+
|
22
|
+
class InvalideStructureError < Exception; end
|
23
|
+
class MissingFixtureError < Exception; end
|
24
|
+
|
25
|
+
mattr_reader :config
|
26
|
+
|
27
|
+
class << self
|
28
|
+
delegate :profiler, :to => :config
|
29
|
+
|
30
|
+
def setup &block
|
31
|
+
block.yield(Cockroach::Config)
|
32
|
+
@@config = Cockroach::Config.new (Cockroach::Config.config_path || "config/faker.yml")
|
33
|
+
load_fixturer
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
#
|
39
|
+
def load_fixturer
|
40
|
+
self.const_get(@@config.fixturer)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
3
|
+
module Cockroach
|
4
|
+
module Base
|
5
|
+
module LoadNodes
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
attr_reader :nodes
|
10
|
+
|
11
|
+
def load! *agrs
|
12
|
+
raise "Abstract method"
|
13
|
+
end
|
14
|
+
|
15
|
+
def nodes
|
16
|
+
@nodes ||= {}
|
17
|
+
end
|
18
|
+
|
19
|
+
protected
|
20
|
+
|
21
|
+
# Parse structure file and create a branch of sub nodes.
|
22
|
+
def load_nodes
|
23
|
+
@structure.each do |name, structure|
|
24
|
+
node = Cockroach::FactoryGirl::Node.new(name, structure, {:parrent => self})
|
25
|
+
nodes[node.node_name] = node
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Load all the sub nodes
|
30
|
+
def load_nodes! fixturer_opts = nil
|
31
|
+
nodes.each_value do |node|
|
32
|
+
node.load! fixturer_opts
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,192 @@
|
|
1
|
+
require "cockroach/base/node_structure"
|
2
|
+
|
3
|
+
module Cockroach
|
4
|
+
module Base
|
5
|
+
# Node deals only with a specific records class. It makes sure that
|
6
|
+
# fixtures are created specific amount of time and will make sure that
|
7
|
+
# the associations are correctly assigned.
|
8
|
+
class Node
|
9
|
+
include Cockroach::Base::LoadNodes
|
10
|
+
include Cockroach::Base::NodeStructure
|
11
|
+
|
12
|
+
APPROACHES = %w(amount ratio).freeze
|
13
|
+
|
14
|
+
class << self
|
15
|
+
# Method validating structure for Node operations.
|
16
|
+
# It will only accepts a structure if it is a Hash object,
|
17
|
+
# with one key only.
|
18
|
+
# In any other case a _false_ value will be returned
|
19
|
+
def valid_structure? structure
|
20
|
+
structure.is_a?(Hash) && structure.keys.size == 1 ||
|
21
|
+
structure.is_a?(Array) && structure.size == 2
|
22
|
+
end
|
23
|
+
|
24
|
+
# Deduct the information from Node instance name:
|
25
|
+
# will try to get:
|
26
|
+
# name: compulsary
|
27
|
+
# approach: optional
|
28
|
+
# Returns array containing name and approach
|
29
|
+
#
|
30
|
+
# Example:
|
31
|
+
#
|
32
|
+
# # no apprach
|
33
|
+
# ["users", nil]
|
34
|
+
#
|
35
|
+
# # ratio apprach
|
36
|
+
# ["users", "ratio"]
|
37
|
+
def extract_info symbol_or_string
|
38
|
+
symbol_or_string = symbol_or_string.to_s
|
39
|
+
approach = (symbol_or_string.match(/_(#{APPROACHES.join('|')})$/)[1] rescue nil)
|
40
|
+
symbol_or_string.gsub!(/(_#{APPROACHES.join('|_')})$/,'') unless approach.blank?
|
41
|
+
[symbol_or_string.to_s.singularize, approach]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
attr_reader :name, :approach, :ids, :source
|
46
|
+
|
47
|
+
delegate :find, :sample, :to => :source
|
48
|
+
|
49
|
+
def initialize *opts
|
50
|
+
node_name, @structure, @options = extract_params_from_attrs *opts
|
51
|
+
@name, @approach = self.class.extract_info(node_name.dup)
|
52
|
+
@ids = []
|
53
|
+
@source = define_source if @structure.is_a?(Hash)
|
54
|
+
|
55
|
+
if @approach.blank?
|
56
|
+
extract_options
|
57
|
+
complicated_approch
|
58
|
+
load_nodes
|
59
|
+
else
|
60
|
+
simple_approach
|
61
|
+
end
|
62
|
+
|
63
|
+
raise InvalideStructureError.new("Approch was not specified") if @approach.blank?
|
64
|
+
raise MissingFixtureError.new("Approch was not specified") if @approach.blank?
|
65
|
+
|
66
|
+
after_initialize
|
67
|
+
end
|
68
|
+
|
69
|
+
# Just in case Fixturer requires anything to be done after the node initialization
|
70
|
+
def after_initialize
|
71
|
+
end
|
72
|
+
|
73
|
+
# Keeps the number, that represents the amount of the records that will
|
74
|
+
# created for this node.
|
75
|
+
# This will be the final number of the records that are going to be created.
|
76
|
+
# In case there was a ratio specified, that method will randomly get a
|
77
|
+
# number within the provided range and keep it.
|
78
|
+
def amount
|
79
|
+
@amount || get_random_amount
|
80
|
+
end
|
81
|
+
|
82
|
+
# Returns an aliased node name. The one that will be used for keeping it
|
83
|
+
# in sup and sub structures
|
84
|
+
def node_name
|
85
|
+
@alias_as || @name
|
86
|
+
end
|
87
|
+
|
88
|
+
protected
|
89
|
+
|
90
|
+
# There are four possible options here
|
91
|
+
# - structure
|
92
|
+
# - node_name, structure
|
93
|
+
# - structure, options
|
94
|
+
# - node_name, structure, options
|
95
|
+
def extract_params_from_attrs *attrs
|
96
|
+
structure = if attrs.size == 1
|
97
|
+
attrs.first
|
98
|
+
elsif attrs.size == 3
|
99
|
+
options = attrs.pop
|
100
|
+
attrs
|
101
|
+
else
|
102
|
+
options = attrs.pop if attrs.first.is_a?(Hash)
|
103
|
+
attrs
|
104
|
+
end
|
105
|
+
|
106
|
+
raise InvalideStructureError.new("Node has faced invalid structure") unless self.class.valid_structure?(structure)
|
107
|
+
structure.flatten << (options || {})
|
108
|
+
end
|
109
|
+
|
110
|
+
# Complicated approach is used, once the amount directives were specified
|
111
|
+
# within node subconfig.
|
112
|
+
def complicated_approch
|
113
|
+
approach = @options.select { |k,v| APPROACHES.include? k }
|
114
|
+
raise InvalideStructureError.new("Amount is not specified or specified multiple times") unless approach.keys.size == 1
|
115
|
+
case (@approach = approach.keys[0])
|
116
|
+
when "amount"
|
117
|
+
@amount = @options[@approach].to_i
|
118
|
+
when "ratio"
|
119
|
+
@options[@approach].is_a?(String) || @options[@approach].kind_of?(Numeric) ?
|
120
|
+
set_simple_limits(@options[@approach].to_i) :
|
121
|
+
extract_limits(@options[@approach])
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
# Simple approach is used when the amount of the records is specified as the
|
126
|
+
# Yaml node content, and the way this number should be treated as a node
|
127
|
+
# suffix
|
128
|
+
def simple_approach
|
129
|
+
case @approach
|
130
|
+
when "amount"
|
131
|
+
@amount = @structure.to_i
|
132
|
+
when "ratio"
|
133
|
+
set_simple_limits @structure.to_i
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
# Clear the node subconfig. Will extract all the options or directives from
|
138
|
+
# provided Hash, and assigne the to the corresponding variables.
|
139
|
+
def extract_options
|
140
|
+
@options.merge! @structure.extract!(*APPROACHES).delete_if {|k,v| v.nil?}
|
141
|
+
@alias_as = @structure.delete("as")
|
142
|
+
@structure.each_pair {| key, value | (@aliases ||= {})[$1] = value if key =~ /^(.*)_as$/ }
|
143
|
+
@structure.delete_if {| key, value | key =~ /_as$/ }
|
144
|
+
end
|
145
|
+
|
146
|
+
# Defines a simple limits for random amount. It is as simple as
|
147
|
+
# lowest possible value a half of middle_line
|
148
|
+
# highest value is twice the middle_line
|
149
|
+
def set_simple_limits middle_line
|
150
|
+
@upper_limit, @lower_limit = [middle_line / 2, middle_line * 2]
|
151
|
+
end
|
152
|
+
|
153
|
+
def extract_limits hash
|
154
|
+
@upper_limit, @lower_limit = [hash['lower_limit'].to_i, hash['upper_limit'].to_i]
|
155
|
+
end
|
156
|
+
|
157
|
+
# Returns manimum and minumum options within an array, for the amount of
|
158
|
+
# the records, that will be used to generate the amount of the records
|
159
|
+
# that will created for this node.
|
160
|
+
#
|
161
|
+
# Example:
|
162
|
+
#
|
163
|
+
# [5, 12]
|
164
|
+
def get_limits
|
165
|
+
[@upper_limit, @lower_limit]
|
166
|
+
end
|
167
|
+
|
168
|
+
# Generates the number, that represents the amount of the records that will
|
169
|
+
# created for this node.
|
170
|
+
def get_random_amount
|
171
|
+
min, max = get_limits
|
172
|
+
|
173
|
+
if max - min > 0
|
174
|
+
min + Random.rand(max - min)
|
175
|
+
else
|
176
|
+
min
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
# Returns orm class deducted from fixturer
|
181
|
+
def orm_class
|
182
|
+
raise "Implement"
|
183
|
+
end
|
184
|
+
|
185
|
+
def define_source
|
186
|
+
source_refs = @structure.delete('source')
|
187
|
+
return unless source_refs
|
188
|
+
Cockroach::Source.get_source source_refs
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
192
|
+
end
|