fixturizer 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +40 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/fixturizer.gemspec +27 -0
- data/lib/fixturizer/rake/manage.rb +2 -0
- data/lib/fixturizer/rake/rules/database.rake +18 -0
- data/lib/fixturizer/version.rb +3 -0
- data/lib/fixturizer.rb +109 -0
- metadata +60 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ab10d4fabe09191e653897a9b9f56a2e2b6ca877bff029f0088a9b7b8e8b4f9a
|
4
|
+
data.tar.gz: 124246f423b1252d8469efcedbe0291ace136ef2748108d675b5730617cae423
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cb2aa87307709f9ba955921c7b8b084780999e169285e82ea98c1bbd53741278b4f0c44368071b695e04358e9583146f1a7c25e2c17a95d823e0ee074d68526f
|
7
|
+
data.tar.gz: 4a48949150074bd679b4f14a1014735e68953b7d4f45bb6d6c62c1e70e75284d2e3c5cd8a0b6dde1bb3882e8d2d735fe11bd469be862d5477e6ce3cb37e06acc
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 Pierre Alphonse
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# Fixturizer
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/fixturizer`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'fixturizer'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install fixturizer
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/fixturizer.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "fixturizer"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/fixturizer.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require_relative 'lib/fixturizer/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "fixturizer"
|
5
|
+
spec.version = Fixturizer::VERSION
|
6
|
+
spec.authors = ["Pierre Alphonse"]
|
7
|
+
spec.email = ["pierre.alphonse@orange.com"]
|
8
|
+
|
9
|
+
spec.summary = "Ruby fixturizer for databases models"
|
10
|
+
spec.description = "Ruby fixturizer for databases models"
|
11
|
+
spec.homepage = "https://github.com/Ultragreen/fixturizer"
|
12
|
+
spec.license = "MIT"
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
14
|
+
|
15
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
16
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
17
|
+
spec.metadata["changelog_uri"] = spec.homepage
|
18
|
+
|
19
|
+
# Specify which files should be added to the gem when it is released.
|
20
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
21
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
22
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
23
|
+
end
|
24
|
+
spec.bindir = "exe"
|
25
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
26
|
+
spec.require_paths = ["lib"]
|
27
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
namespace :fixturizer do
|
2
|
+
namespace :database do
|
3
|
+
|
4
|
+
desc "Drop database"
|
5
|
+
task :drop do
|
6
|
+
my_fixturer = Fixturizer::Engine::new filename: './config/rules.yml'
|
7
|
+
my_fixturer.drop_database
|
8
|
+
end
|
9
|
+
|
10
|
+
desc "Populate database"
|
11
|
+
task :populate do
|
12
|
+
my_fixturer = Fixturizer::Engine::new filename: './config/rules.yml'
|
13
|
+
my_fixturer.generate_data
|
14
|
+
my_fixturer.inject_data
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
data/lib/fixturizer.rb
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'mongoid'
|
2
|
+
require "fixturizer/version"
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
module Fixturizer
|
6
|
+
module Adapters
|
7
|
+
module Mongoid
|
8
|
+
def injector(model_infos:, item:)
|
9
|
+
model = Object.const_get(model_infos[:class])
|
10
|
+
model.create!(item) unless model.where(model_infos[:unicity] => item[model_infos[:unicity]]).exists?
|
11
|
+
end
|
12
|
+
|
13
|
+
def binder(item:)
|
14
|
+
result = Hash::new
|
15
|
+
item.each do |element|
|
16
|
+
model_class = @models[element[:collection]][:class]
|
17
|
+
model = Object.const_get(model_class)
|
18
|
+
if element[:index].include?(:at)
|
19
|
+
result[element[:fkey]] = model.all[element[:index][:at] - 1][element[:pkey]].to_s
|
20
|
+
elsif element[:index].include?(:search_key)
|
21
|
+
raise 'Links configuration failure' unless element[:index].include?(:for)
|
22
|
+
|
23
|
+
result[element[:fkey]] = model.where({ element[:index][:search_key] => element[:index][:for] }).first[element[:pkey]]
|
24
|
+
else
|
25
|
+
raise 'Links configuration failure'
|
26
|
+
end
|
27
|
+
end
|
28
|
+
return result
|
29
|
+
end
|
30
|
+
|
31
|
+
def drop_database
|
32
|
+
::Mongoid.purge!
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class Engine
|
38
|
+
ADAPTERS = {:mongoid => ::Fixturizer::Adapters::Mongoid}
|
39
|
+
|
40
|
+
attr_reader :generated
|
41
|
+
|
42
|
+
def initialize(filename:)
|
43
|
+
@rules = read_rules(filename: filename)
|
44
|
+
@generations = @rules[:fixtures][:rules][:generation]
|
45
|
+
@models = @rules[:fixtures][:models][:definition]
|
46
|
+
@order = @rules[:fixtures][:models].include?(:order) ? @rules[:fixtures][:models][:order] : false
|
47
|
+
@generated = Hash::new
|
48
|
+
self.extend ADAPTERS[@rules[:fixtures][:type]]
|
49
|
+
end
|
50
|
+
|
51
|
+
def generate_from(rule:)
|
52
|
+
rule = @generations[rule]
|
53
|
+
myproc = eval("lambda { #{rule[:proc]} } ")
|
54
|
+
return myproc.call
|
55
|
+
end
|
56
|
+
|
57
|
+
def link_data
|
58
|
+
end
|
59
|
+
|
60
|
+
def generate_data
|
61
|
+
@generated.clear
|
62
|
+
raise 'Order field format missmatch, not an array' unless @order == false || @order.class == Array
|
63
|
+
|
64
|
+
if @order then
|
65
|
+
raise 'Order field size missmatch for configurate definitions' unless @order.size == @models.size
|
66
|
+
|
67
|
+
@order.each do |item|
|
68
|
+
raise "Definition #{item} not found in models definitions" unless @models.include?(item)
|
69
|
+
|
70
|
+
@generated[item] = generate_collection(name: item)
|
71
|
+
end
|
72
|
+
else
|
73
|
+
@models.each do |key, _value|
|
74
|
+
@generated[key] = generate_collection(name: key)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def inject_data
|
80
|
+
raise 'Data not generated' if @generated.empty?
|
81
|
+
|
82
|
+
@generated.each do |key, value|
|
83
|
+
model_infos = @models[key].dup
|
84
|
+
model_infos.delete(:collection)
|
85
|
+
value.each do |item|
|
86
|
+
item[:attributes].merge!(self.binder(item: item[:links])) if item.include?(:links)
|
87
|
+
self.injector model_infos: model_infos, item: item[:attributes]
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
private
|
93
|
+
def read_rules(filename:)
|
94
|
+
return YAML.load(File::readlines(filename).join)
|
95
|
+
end
|
96
|
+
|
97
|
+
def generate_collection(name:)
|
98
|
+
data = @models[name][:collection].dup
|
99
|
+
data.each do |item|
|
100
|
+
|
101
|
+
item[:attributes].each do |key, _value|
|
102
|
+
item[:attributes][key] = generate_from(rule: @models[name][:rules][key]) if @models[name].include?(:rules) && @models[name][:rules].include?(key)
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
106
|
+
return data
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fixturizer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Pierre Alphonse
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-09-15 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Ruby fixturizer for databases models
|
14
|
+
email:
|
15
|
+
- pierre.alphonse@orange.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".gitignore"
|
21
|
+
- ".rspec"
|
22
|
+
- ".travis.yml"
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- bin/console
|
28
|
+
- bin/setup
|
29
|
+
- fixturizer.gemspec
|
30
|
+
- lib/fixturizer.rb
|
31
|
+
- lib/fixturizer/rake/manage.rb
|
32
|
+
- lib/fixturizer/rake/rules/database.rake
|
33
|
+
- lib/fixturizer/version.rb
|
34
|
+
homepage: https://github.com/Ultragreen/fixturizer
|
35
|
+
licenses:
|
36
|
+
- MIT
|
37
|
+
metadata:
|
38
|
+
homepage_uri: https://github.com/Ultragreen/fixturizer
|
39
|
+
source_code_uri: https://github.com/Ultragreen/fixturizer
|
40
|
+
changelog_uri: https://github.com/Ultragreen/fixturizer
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: 2.3.0
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
requirements: []
|
56
|
+
rubygems_version: 3.1.2
|
57
|
+
signing_key:
|
58
|
+
specification_version: 4
|
59
|
+
summary: Ruby fixturizer for databases models
|
60
|
+
test_files: []
|