fixture 0.1.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.
- checksums.yaml +7 -0
- data/README.md +93 -0
- data/Rakefile +15 -0
- data/fixture.gemspec +20 -0
- data/lib/fixture.rb +1 -0
- data/lib/fixture/fixture.rb +71 -0
- metadata +50 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: fb661623c1edb55964c1bc0b184b41c2722fcce3
|
|
4
|
+
data.tar.gz: aaef71922e49ca39904cc8d02b39fec6cf76d0cc
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: fe3255ba4e465154d07e809c41f1d154ebd677d129ee9e43b9fc1ae50db5ed18b16e76c795f5fd380964df853faec3ba04161bc15cdb27a82bad3701c3315d93
|
|
7
|
+
data.tar.gz: a2fab15f9aeb29dc0bafaeef35bc35919ad6b0230cc796d5f68f279c5433af5ff3d057ba0ca68f58d93783a688afc6e9ddd3dab630123994c696fcd924d7589c
|
data/README.md
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# Fixture, a simple fixture for rspec and others
|
|
2
|
+
|
|
3
|
+
## What is it?
|
|
4
|
+
|
|
5
|
+
####It
|
|
6
|
+
|
|
7
|
+
* is a very simple and lightweight fixture easy to use
|
|
8
|
+
* can be used under various situations
|
|
9
|
+
* requires no other libraries
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
gem install fixture
|
|
14
|
+
|
|
15
|
+
## How to use
|
|
16
|
+
####write fixture
|
|
17
|
+
|
|
18
|
+
#spec/fixtures.rb
|
|
19
|
+
|
|
20
|
+
Fixture.manage User do
|
|
21
|
+
assign :user1 do
|
|
22
|
+
name 'Taro Yamada'
|
|
23
|
+
email { #this is a block }
|
|
24
|
+
created_at Time.now
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
assign :user2 do
|
|
28
|
+
name { Faker::Name.name + 's' }
|
|
29
|
+
email Faker::Internet.email
|
|
30
|
+
deleted true
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
Fixture.manage Post do
|
|
35
|
+
assign :post1 do
|
|
36
|
+
title 'title1'
|
|
37
|
+
body '....'
|
|
38
|
+
user_id { Fixture.get(:user1).id }
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
Of cource, you can add many records at once by like this.
|
|
43
|
+
|
|
44
|
+
Fixture.manage User do
|
|
45
|
+
1000.times do |i|
|
|
46
|
+
assign "user#{i+1}".to_sym do
|
|
47
|
+
name Faker::Name.name
|
|
48
|
+
email Faker::Internet.email
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
And you can put it as separate files in other directory.
|
|
54
|
+
|
|
55
|
+
Fixture automatically loads
|
|
56
|
+
|
|
57
|
+
* spec/fixtures.rb
|
|
58
|
+
* spec/fixture/*.rb
|
|
59
|
+
* test/fixtures.rb
|
|
60
|
+
* test/fixture/*.rb
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
####load fixture
|
|
66
|
+
|
|
67
|
+
#spec/spec_helper.rb
|
|
68
|
+
require 'fixture'
|
|
69
|
+
Fixture.load
|
|
70
|
+
|
|
71
|
+
if you don't want to insert records but use model's instances,
|
|
72
|
+
|
|
73
|
+
Fixture.prepare
|
|
74
|
+
|
|
75
|
+
instead of Fixture.load.
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
####use fixture
|
|
79
|
+
|
|
80
|
+
\#spec/models/user_spec.rb
|
|
81
|
+
|
|
82
|
+
describe User do
|
|
83
|
+
let (:user) { Fixture.get(:user2) }
|
|
84
|
+
.....
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
##Assumption
|
|
88
|
+
|
|
89
|
+
Fixture assumes that model classes
|
|
90
|
+
|
|
91
|
+
* have accessor methods to each attributes(i.e; columns of record)
|
|
92
|
+
* have save! or save method to insert new record
|
|
93
|
+
|
data/Rakefile
ADDED
data/fixture.gemspec
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Gem::Specification.new do |s|
|
|
2
|
+
s.name = "fixture"
|
|
3
|
+
s.version = "0.1.0"
|
|
4
|
+
s.platform = Gem::Platform::RUBY
|
|
5
|
+
s.summary = "simple fixture"
|
|
6
|
+
s.license = "MIT"
|
|
7
|
+
|
|
8
|
+
s.description = <<-EOF
|
|
9
|
+
Formerly known as 'fixit'.
|
|
10
|
+
see https://github.com/borot/fixture .
|
|
11
|
+
EOF
|
|
12
|
+
|
|
13
|
+
s.files = Dir['{lib/**/*}'] +
|
|
14
|
+
%w(fixture.gemspec Rakefile README.md)
|
|
15
|
+
s.require_path = 'lib'
|
|
16
|
+
|
|
17
|
+
s.author = 'Masato Ishimoto'
|
|
18
|
+
s.email = 'masato.ishimoto@gmail.com'
|
|
19
|
+
s.homepage = 'https://github.com/borot/fixture'
|
|
20
|
+
end
|
data/lib/fixture.rb
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require 'fixture/fixture'
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
class Fixture
|
|
2
|
+
def initialize(klass)
|
|
3
|
+
@klass = klass
|
|
4
|
+
end
|
|
5
|
+
|
|
6
|
+
def assign( name, &block )
|
|
7
|
+
self.class.candidates[name] = @candidate = { obj: @klass.new, attributes: {} }
|
|
8
|
+
yield
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def attribute( name, *args, &block)
|
|
12
|
+
val = args.first
|
|
13
|
+
val = block if block_given?
|
|
14
|
+
@candidate[:attributes][name] = val
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
alias method_missing attribute
|
|
18
|
+
|
|
19
|
+
class << self
|
|
20
|
+
FIXTURE_FILE = 'fixtures.rb'
|
|
21
|
+
FIXTURE_DIR = 'fixtures'
|
|
22
|
+
|
|
23
|
+
@@prepared = false
|
|
24
|
+
|
|
25
|
+
def manage( klass, &block )
|
|
26
|
+
self.new(klass).instance_eval &block
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def get( name )
|
|
30
|
+
assigneds[name]
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def assigneds
|
|
34
|
+
@@assigneds ||= {}
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def candidates
|
|
38
|
+
@@candidates ||= {}
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def load
|
|
42
|
+
prepare unless prepared?
|
|
43
|
+
|
|
44
|
+
candidates.each do |name, candidate|
|
|
45
|
+
candidate[:attributes].each do |attr, val|
|
|
46
|
+
candidate[:obj].send("#{attr}=", val.is_a?(Proc) ? val.call : val)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
assigneds[name] = obj = candidate[:obj]
|
|
50
|
+
|
|
51
|
+
if obj.respond_to? :save!
|
|
52
|
+
obj.save!
|
|
53
|
+
elsif obj.respond_to? :save
|
|
54
|
+
obj.save
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def prepare
|
|
60
|
+
%w(spec test).each do |dir|
|
|
61
|
+
require "#{dir}/#{FIXTURE_FILE}" if File.exists? "#{dir}/#{FIXTURE_FILE}"
|
|
62
|
+
Dir["#{dir}/#{FIXTURE_DIR}/*.rb"].each {|f| require f }
|
|
63
|
+
end
|
|
64
|
+
@@prepared = true
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def prepared?
|
|
68
|
+
!!@@prepared
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: fixture
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Masato Ishimoto
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2016-03-18 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: |
|
|
14
|
+
Formerly known as 'fixit'.
|
|
15
|
+
see https://github.com/borot/fixture .
|
|
16
|
+
email: masato.ishimoto@gmail.com
|
|
17
|
+
executables: []
|
|
18
|
+
extensions: []
|
|
19
|
+
extra_rdoc_files: []
|
|
20
|
+
files:
|
|
21
|
+
- README.md
|
|
22
|
+
- Rakefile
|
|
23
|
+
- fixture.gemspec
|
|
24
|
+
- lib/fixture.rb
|
|
25
|
+
- lib/fixture/fixture.rb
|
|
26
|
+
homepage: https://github.com/borot/fixture
|
|
27
|
+
licenses:
|
|
28
|
+
- MIT
|
|
29
|
+
metadata: {}
|
|
30
|
+
post_install_message:
|
|
31
|
+
rdoc_options: []
|
|
32
|
+
require_paths:
|
|
33
|
+
- lib
|
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
35
|
+
requirements:
|
|
36
|
+
- - ">="
|
|
37
|
+
- !ruby/object:Gem::Version
|
|
38
|
+
version: '0'
|
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
40
|
+
requirements:
|
|
41
|
+
- - ">="
|
|
42
|
+
- !ruby/object:Gem::Version
|
|
43
|
+
version: '0'
|
|
44
|
+
requirements: []
|
|
45
|
+
rubyforge_project:
|
|
46
|
+
rubygems_version: 2.4.5.1
|
|
47
|
+
signing_key:
|
|
48
|
+
specification_version: 4
|
|
49
|
+
summary: simple fixture
|
|
50
|
+
test_files: []
|