fixture_box 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/MIT-LICENSE +20 -0
- data/README.md +89 -0
- data/Rakefile +33 -0
- data/lib/fixture_box.rb +48 -0
- data/lib/fixture_box/version.rb +3 -0
- data/lib/tasks/fixture_box_tasks.rake +4 -0
- metadata +78 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: efec83e114bb1aabedb7ede59d43ea64ac37b96d3c6d995fba438b082840409e
|
4
|
+
data.tar.gz: 315e4c0d734ac5384c42dcbaa9cbea903de5eed8fcffb608f20cde928be3aeb6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fc59bc42eb9f6d7548e8678b803a80d9a29e8c9b52bfd81fef17ba4cea9ec3abfb87fe5ee72c2205b549bffbfd9e8349dd517f33d857fbad93f2aef1aca5bcb2
|
7
|
+
data.tar.gz: dc65c3b9f1f2c7894856d6fab8fdd99241192765ca443998bc453d3ae12c28381acaec9a4091acc8e1a2e5c7a93bb3204e35c240ec43de21c2fd4645f33f1f1f
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2018 Yuichi Takeuchi
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
# FixtureBox
|
2
|
+
Short description and motivation.
|
3
|
+
|
4
|
+
## Usage
|
5
|
+
|
6
|
+
```ruby
|
7
|
+
class Book < ApplicationRecord
|
8
|
+
belongs_to :author
|
9
|
+
has_many :files, class_name: 'Book::File'
|
10
|
+
end
|
11
|
+
|
12
|
+
class Author < ApplicationRecord
|
13
|
+
belongs_to :book
|
14
|
+
end
|
15
|
+
|
16
|
+
class Book::File < ApplicationRecord
|
17
|
+
belongs_to :book
|
18
|
+
end
|
19
|
+
|
20
|
+
fixture_data = {
|
21
|
+
books: {
|
22
|
+
wagahai_wa_neko_de_aru: {
|
23
|
+
name: 'Wagahai wa Neko de Aru',
|
24
|
+
author: :natsume_soseki
|
25
|
+
},
|
26
|
+
ame_ni_mo_makezu: {
|
27
|
+
name: 'Ame ni mo Makezu',
|
28
|
+
author: :miyazawa_kenji
|
29
|
+
}
|
30
|
+
},
|
31
|
+
authors: {
|
32
|
+
natsume_soseki: {
|
33
|
+
name: 'Natsume Soseki'
|
34
|
+
},
|
35
|
+
miyazawa_kenji: {
|
36
|
+
name: 'Miyazawa Kenji'
|
37
|
+
}
|
38
|
+
},
|
39
|
+
book_files: {
|
40
|
+
wagahai_wa_neko_de_aru_zip: {
|
41
|
+
book: :wagahai_wa_neko_de_aru,
|
42
|
+
content_type: 'application/zip'
|
43
|
+
},
|
44
|
+
wagahai_wa_neko_de_aru_ebk: {
|
45
|
+
book: :wagahai_wa_neko_de_aru,
|
46
|
+
content_type: 'application/x-expandedbook'
|
47
|
+
}
|
48
|
+
}
|
49
|
+
}
|
50
|
+
class_names = {
|
51
|
+
book_files: Book::File
|
52
|
+
}
|
53
|
+
|
54
|
+
fixture_box = FixtureBox.new(fixture_data, class_names)
|
55
|
+
|
56
|
+
assert_equal 'Wagahai wa Neko de Aru', @fixture_box.books(:wagahai_wa_neko_de_aru).name
|
57
|
+
|
58
|
+
# belongs_to
|
59
|
+
assert_equal 'Natsume Soseki', @fixture_box.books(:wagahai_wa_neko_de_aru).author.name
|
60
|
+
|
61
|
+
# Set the model class
|
62
|
+
assert_equal %w(application/zip application/x-expandedbook), @fixture_box.books(:wagahai_wa_neko_de_aru).files.pluck(:content_type)
|
63
|
+
|
64
|
+
# Get all instances
|
65
|
+
assert_equal ['Wagahai wa Neko de Aru', 'Ame ni mo Makezu'], @fixture_box.books.map(&:name)
|
66
|
+
```
|
67
|
+
|
68
|
+
## Installation
|
69
|
+
Add this line to your application's Gemfile:
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
gem 'fixture_box'
|
73
|
+
```
|
74
|
+
|
75
|
+
And then execute:
|
76
|
+
```bash
|
77
|
+
$ bundle
|
78
|
+
```
|
79
|
+
|
80
|
+
Or install it yourself as:
|
81
|
+
```bash
|
82
|
+
$ gem install fixture_box
|
83
|
+
```
|
84
|
+
|
85
|
+
## Contributing
|
86
|
+
Contribution directions go here.
|
87
|
+
|
88
|
+
## License
|
89
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'FixtureBox'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
require 'bundler/gem_tasks'
|
23
|
+
|
24
|
+
require 'rake/testtask'
|
25
|
+
|
26
|
+
Rake::TestTask.new(:test) do |t|
|
27
|
+
t.libs << 'test'
|
28
|
+
t.pattern = 'test/**/*_test.rb'
|
29
|
+
t.verbose = false
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
task default: :test
|
data/lib/fixture_box.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "fixture_box/version"
|
4
|
+
|
5
|
+
class FixtureBox
|
6
|
+
|
7
|
+
def initialize(data, class_names={})
|
8
|
+
data = data.stringify_keys
|
9
|
+
class_names = class_names.stringify_keys
|
10
|
+
|
11
|
+
Dir.mktmpdir do |tmp_fixtures_dir|
|
12
|
+
decorated_fixture_set_names = []
|
13
|
+
decorated_class_names = {}
|
14
|
+
data.each do |fixture_set_name, fixture_data|
|
15
|
+
fixture_class = class_names[fixture_set_name] || fixture_set_name.to_s.classify.constantize
|
16
|
+
decorated_fixture_set_name = fixture_set_name
|
17
|
+
|
18
|
+
decorated_fixture_set_names << decorated_fixture_set_name
|
19
|
+
decorated_class_names[decorated_fixture_set_name] = fixture_class
|
20
|
+
|
21
|
+
File.open(File.join(tmp_fixtures_dir, "#{decorated_fixture_set_name}.yml"), 'w') do |file|
|
22
|
+
YAML.dump(fixture_data.deep_stringify_keys, file)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
fixtures = ActiveRecord::FixtureSet.create_fixtures(tmp_fixtures_dir, decorated_fixture_set_names, decorated_class_names)
|
26
|
+
@loaded_fixture_sets = Hash[fixtures.map { |f| [f.name, f] }]
|
27
|
+
end
|
28
|
+
|
29
|
+
setup_fixture_accessors
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def setup_fixture_accessors
|
35
|
+
@loaded_fixture_sets.each do |fixture_set_name, fixture_set|
|
36
|
+
define_singleton_method(fixture_set_name) do |*fixture_names|
|
37
|
+
fixture_names = @loaded_fixture_sets[fixture_set_name].fixtures.keys if fixture_names.empty?
|
38
|
+
|
39
|
+
instances = fixture_names.map do |fixture_name|
|
40
|
+
fixture_set[fixture_name.to_s].find
|
41
|
+
end
|
42
|
+
|
43
|
+
fixture_names.size == 1 ? instances.first : instances
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fixture_box
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Takeyu Web Inc.
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-01-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sqlite3
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Dynamically creating fixtures.
|
42
|
+
email:
|
43
|
+
- yuichi.takeuchi@takeyuweb.co.jp
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- MIT-LICENSE
|
49
|
+
- README.md
|
50
|
+
- Rakefile
|
51
|
+
- lib/fixture_box.rb
|
52
|
+
- lib/fixture_box/version.rb
|
53
|
+
- lib/tasks/fixture_box_tasks.rake
|
54
|
+
homepage: https://github.com/takeyuwebcoltd/fixture_box
|
55
|
+
licenses:
|
56
|
+
- MIT
|
57
|
+
metadata: {}
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
requirements: []
|
73
|
+
rubyforge_project:
|
74
|
+
rubygems_version: 2.7.3
|
75
|
+
signing_key:
|
76
|
+
specification_version: 4
|
77
|
+
summary: Dynamically creating fixtures.
|
78
|
+
test_files: []
|