zyra 0.0.2 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -0
- data/README.md +67 -1
- data/config/yardstick.yml +7 -15
- data/lib/zyra/{builder.rb → creator.rb} +18 -49
- data/lib/zyra/exceptions.rb +3 -2
- data/lib/zyra/finder.rb +103 -0
- data/lib/zyra/finder_creator.rb +162 -0
- data/lib/zyra/registry.rb +179 -13
- data/lib/zyra/version.rb +1 -1
- data/lib/zyra.rb +130 -56
- data/spec/integration/readme/zyra_spec.rb +54 -0
- data/spec/integration/yard/zyra/registry_spec.rb +135 -0
- data/spec/integration/yard/zyra_spec.rb +128 -0
- data/spec/lib/zyra/creator_spec.rb +75 -0
- data/spec/lib/zyra/finder_creator_spec.rb +263 -0
- data/spec/lib/zyra/finder_spec.rb +117 -0
- data/spec/lib/zyra/registry_spec.rb +6 -92
- data/spec/lib/zyra_spec.rb +8 -144
- data/spec/spec_helper.rb +13 -0
- data/spec/support/factories/user.rb +9 -0
- data/spec/support/factory_bot.rb +7 -0
- data/spec/support/models/post.rb +5 -0
- data/spec/support/models/user.rb +1 -0
- data/spec/support/schema.rb +8 -0
- data/spec/support/shared_examples/registry/find_or_create.rb +130 -0
- data/spec/support/shared_examples/registry/on.rb +42 -0
- data/spec/support/shared_examples/registry/register.rb +54 -0
- data/zyra.gemspec +3 -1
- metadata +53 -12
- data/spec/lib/zyra/builder_spec.rb +0 -129
@@ -1,129 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
describe Zyra::Builder do
|
6
|
-
subject(:builder) { described_class.new(model_class) }
|
7
|
-
|
8
|
-
let(:model_class) { User }
|
9
|
-
|
10
|
-
describe '#build' do
|
11
|
-
it do
|
12
|
-
expect(builder.build).to be_a(model_class)
|
13
|
-
end
|
14
|
-
|
15
|
-
it do
|
16
|
-
expect(builder.build)
|
17
|
-
.not_to be_persisted
|
18
|
-
end
|
19
|
-
|
20
|
-
context 'when attributes are given' do
|
21
|
-
let(:name) { SecureRandom.hex(10) }
|
22
|
-
|
23
|
-
it 'initializes the model with the given attribute' do
|
24
|
-
expect(builder.build(name: name).name)
|
25
|
-
.to eq(name)
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
context 'when a block is given' do
|
30
|
-
let(:name) { SecureRandom.hex(10) }
|
31
|
-
|
32
|
-
it 'initializes the model with the given attribute' do
|
33
|
-
value = name
|
34
|
-
expect(builder.build { |model| model.name = value }.name)
|
35
|
-
.to eq(name)
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
context 'when building has an after build handler' do
|
40
|
-
let(:name) { SecureRandom.hex(10) }
|
41
|
-
|
42
|
-
before do
|
43
|
-
value = name
|
44
|
-
builder.after(:build) { |model| model.name = value }
|
45
|
-
end
|
46
|
-
|
47
|
-
it 'runs the event handler' do
|
48
|
-
expect(builder.build.name)
|
49
|
-
.to eq(name)
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
describe '#create' do
|
55
|
-
it do
|
56
|
-
expect(builder.create).to be_a(model_class)
|
57
|
-
end
|
58
|
-
|
59
|
-
it do
|
60
|
-
expect(builder.create)
|
61
|
-
.to be_persisted
|
62
|
-
end
|
63
|
-
|
64
|
-
context 'when attributes are given' do
|
65
|
-
let(:name) { SecureRandom.hex(10) }
|
66
|
-
|
67
|
-
it 'initializes the model with the given attribute' do
|
68
|
-
expect(builder.create(name: name).name)
|
69
|
-
.to eq(name)
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
context 'when a block is given' do
|
74
|
-
let(:name) { SecureRandom.hex(10) }
|
75
|
-
|
76
|
-
it 'initializes the model with the given attribute' do
|
77
|
-
value = name
|
78
|
-
expect(builder.create { |model| model.name = value }.name)
|
79
|
-
.to eq(name)
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
context 'when building has an after build handler' do
|
84
|
-
let(:name) { SecureRandom.hex(10) }
|
85
|
-
|
86
|
-
before do
|
87
|
-
value = name
|
88
|
-
builder.after(:build) { |model| model.name = "#{value}#{model.id}" }
|
89
|
-
end
|
90
|
-
|
91
|
-
it 'runs the event handler' do
|
92
|
-
expect(builder.create.name)
|
93
|
-
.to eq(name)
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
context 'when building has an after create handler' do
|
98
|
-
let(:name) { SecureRandom.hex(10) }
|
99
|
-
|
100
|
-
before do
|
101
|
-
value = name
|
102
|
-
builder.after(:create) { |model| model.name = "#{value}#{model.id}" }
|
103
|
-
end
|
104
|
-
|
105
|
-
it 'runs the event handler' do
|
106
|
-
model = builder.create
|
107
|
-
expect(model.name)
|
108
|
-
.to eq("#{name}#{model.id}")
|
109
|
-
end
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
describe '#after' do
|
114
|
-
let(:name) { SecureRandom.hex(10) }
|
115
|
-
|
116
|
-
it 'register a handler to be ran after an event' do
|
117
|
-
value = name
|
118
|
-
|
119
|
-
expect { builder.after(:build) { |model| model.name = value } }
|
120
|
-
.to change { builder.build.name }
|
121
|
-
.from(nil).to(name)
|
122
|
-
end
|
123
|
-
|
124
|
-
it do
|
125
|
-
expect(builder.after(:build) {})
|
126
|
-
.to be(builder)
|
127
|
-
end
|
128
|
-
end
|
129
|
-
end
|