zyra 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +20 -6
- data/Dockerfile +1 -1
- data/Dockerfile.circleci +7 -0
- data/README.md +2 -2
- data/config/check_specs.yml +1 -0
- data/config/yardstick.yml +16 -4
- data/docker-compose.yml +11 -0
- data/lib/zyra/builder.rb +99 -0
- data/lib/zyra/exceptions.rb +10 -0
- data/lib/zyra/registry.rb +35 -0
- data/lib/zyra/version.rb +2 -2
- data/lib/zyra.rb +113 -1
- data/spec/lib/zyra/builder_spec.rb +129 -0
- data/spec/lib/zyra/registry_spec.rb +105 -0
- data/spec/lib/zyra_spec.rb +150 -1
- data/spec/spec_helper.rb +5 -0
- data/spec/support/models/user.rb +4 -0
- data/spec/support/schema.rb +10 -0
- data/zyra.gemspec +5 -0
- metadata +66 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5cb11a8449da1cdc0b87c0600b96c3b7e9c76b024b95bc4a722b764570296f50
|
4
|
+
data.tar.gz: 5848a55463e899d78ff4c28dd6ae81993916cd449b3dfd35692bb47f46777452
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5b13ca88db8dfa338c68f4af3177d39202d2274a88ec0738a7146b59619e6ccb3369277280e0954fe68150e43ed3f8aac1566d95802c79a552b74db340235f96
|
7
|
+
data.tar.gz: ff5cfc24cbc93e90107ec311b1c6baf25c9fc81d9e8aba0bb89ba1f2d876cf93198f6769a5fcf21d3db5db670e4ad399a1762081a41cf21cdd848d46bbf0dc8f
|
data/.circleci/config.yml
CHANGED
@@ -7,8 +7,12 @@ workflows:
|
|
7
7
|
filters:
|
8
8
|
tags:
|
9
9
|
only: /.*/
|
10
|
+
- checks:
|
11
|
+
filters:
|
12
|
+
tags:
|
13
|
+
only: /.*/
|
10
14
|
- build-and-release:
|
11
|
-
requires: [test]
|
15
|
+
requires: [test, checks]
|
12
16
|
filters:
|
13
17
|
tags:
|
14
18
|
only: /\d+\.\d+\.\d+/
|
@@ -18,7 +22,7 @@ workflows:
|
|
18
22
|
jobs:
|
19
23
|
test:
|
20
24
|
docker:
|
21
|
-
- image: darthjee/
|
25
|
+
- image: darthjee/circleci_rails_gems:1.1.0
|
22
26
|
environment:
|
23
27
|
PROJECT: zyra
|
24
28
|
steps:
|
@@ -32,12 +36,22 @@ jobs:
|
|
32
36
|
- run:
|
33
37
|
name: RSpec
|
34
38
|
command: bundle exec rspec
|
35
|
-
- run:
|
36
|
-
name: Rubocop
|
37
|
-
command: rubocop
|
38
39
|
- run:
|
39
40
|
name: Coverage Test Report
|
40
41
|
command: cc-test-reporter after-build --exit-code $?
|
42
|
+
checks:
|
43
|
+
docker:
|
44
|
+
- image: darthjee/circleci_rails_gems:1.1.0
|
45
|
+
environment:
|
46
|
+
PROJECT: zyra
|
47
|
+
steps:
|
48
|
+
- checkout
|
49
|
+
- run:
|
50
|
+
name: Bundle Install
|
51
|
+
command: bundle install
|
52
|
+
- run:
|
53
|
+
name: Rubocop
|
54
|
+
command: rubocop
|
41
55
|
- run:
|
42
56
|
name: Yardstick coverage check
|
43
57
|
command: bundle exec rake verify_measurements
|
@@ -52,7 +66,7 @@ jobs:
|
|
52
66
|
command: check_specs
|
53
67
|
build-and-release:
|
54
68
|
docker:
|
55
|
-
- image: darthjee/
|
69
|
+
- image: darthjee/circleci_rails_gems:1.1.0
|
56
70
|
environment:
|
57
71
|
PROJECT: zyra
|
58
72
|
steps:
|
data/Dockerfile
CHANGED
data/Dockerfile.circleci
ADDED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
Zyra
|
2
2
|
====
|
3
3
|
[![Code Climate](https://codeclimate.com/github/darthjee/zyra/badges/gpa.svg)](https://codeclimate.com/github/darthjee/zyra)
|
4
4
|
[![Test Coverage](https://codeclimate.com/github/darthjee/zyra/badges/coverage.svg)](https://codeclimate.com/github/darthjee/zyra/coverage)
|
@@ -11,7 +11,7 @@ Jace
|
|
11
11
|
|
12
12
|
Yard Documentation
|
13
13
|
-------------------
|
14
|
-
[https://www.rubydoc.info/gems/zyra/0.0.
|
14
|
+
[https://www.rubydoc.info/gems/zyra/0.0.2](https://www.rubydoc.info/gems/zyra/0.0.2)
|
15
15
|
|
16
16
|
Installation
|
17
17
|
---------------
|
data/config/check_specs.yml
CHANGED
data/config/yardstick.yml
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
threshold:
|
1
|
+
threshold: 96
|
2
2
|
require_exact_threshold: false
|
3
3
|
rules:
|
4
4
|
ApiTag::Presence:
|
@@ -15,13 +15,25 @@ rules:
|
|
15
15
|
exclude: []
|
16
16
|
ExampleTag:
|
17
17
|
enabled: true
|
18
|
-
exclude:
|
18
|
+
exclude:
|
19
|
+
- Zyra.register
|
20
|
+
- Zyra.after
|
21
|
+
- Zyra.build
|
22
|
+
- Zyra.create
|
23
|
+
- Zyra::Builder#after
|
24
|
+
- Zyra::Builder#build
|
25
|
+
- Zyra::Builder#create
|
26
|
+
- Zyra::Registry#register
|
19
27
|
ReturnTag:
|
20
28
|
enabled: true
|
21
|
-
exclude:
|
29
|
+
exclude:
|
30
|
+
- Zyra.after
|
31
|
+
- Zyra.build
|
32
|
+
- Zyra.create
|
22
33
|
Summary::Presence:
|
23
34
|
enabled: true
|
24
|
-
exclude:
|
35
|
+
exclude:
|
36
|
+
- Zyra::Builder#initialize
|
25
37
|
Summary::Length:
|
26
38
|
enabled: true
|
27
39
|
exclude: []
|
data/docker-compose.yml
CHANGED
@@ -21,3 +21,14 @@ services:
|
|
21
21
|
<<: *base
|
22
22
|
depends_on: [base_build]
|
23
23
|
command: /bin/bash -c 'rspec && yard && rake yardstick_measure && rake verify_measurements'
|
24
|
+
|
25
|
+
circleci:
|
26
|
+
<<: *base
|
27
|
+
build:
|
28
|
+
context: ./
|
29
|
+
dockerfile: Dockerfile.circleci
|
30
|
+
command: echo done
|
31
|
+
image: zyra_circleci
|
32
|
+
working_dir: /home/circleci/project
|
33
|
+
volumes:
|
34
|
+
- .:/home/circleci/project
|
data/lib/zyra/builder.rb
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Zyra
|
4
|
+
# @api private
|
5
|
+
# @author Darthjee
|
6
|
+
#
|
7
|
+
# Class responsible for building a model
|
8
|
+
class Builder
|
9
|
+
# @param model_class [Class] Model class to be initialized
|
10
|
+
# into a model
|
11
|
+
def initialize(model_class)
|
12
|
+
@model_class = model_class
|
13
|
+
end
|
14
|
+
|
15
|
+
# @api public
|
16
|
+
# Builds an instance of the registered model class
|
17
|
+
#
|
18
|
+
# @param attributes [Hash] attributes to be set in the model
|
19
|
+
# @param block [Proc] block to be ran after where more attributes
|
20
|
+
# will be set
|
21
|
+
#
|
22
|
+
# @yield [Object] Instance of the model class
|
23
|
+
#
|
24
|
+
# @return [Object] an instance of model class
|
25
|
+
def build(**attributes, &block)
|
26
|
+
block ||= proc {}
|
27
|
+
|
28
|
+
model_class.new(attributes).tap(&block).tap do |model|
|
29
|
+
event_registry.trigger(:build, model)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# @api public
|
34
|
+
# Creates an instance of the registered model class
|
35
|
+
#
|
36
|
+
# This behaves like {#build}, but persists the entry
|
37
|
+
#
|
38
|
+
# @param (see #build)
|
39
|
+
# @yield (see #build)
|
40
|
+
# @return (see #build)
|
41
|
+
def create(**attributes, &block)
|
42
|
+
model = build(**attributes, &block)
|
43
|
+
|
44
|
+
event_registry.trigger(:create, model) do
|
45
|
+
model.tap(&:save)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# @api public
|
50
|
+
# Register a handler on a certain event
|
51
|
+
#
|
52
|
+
# Possible events are +build+, +create+
|
53
|
+
#
|
54
|
+
# @param event [Symbol,String] event to be watched.
|
55
|
+
# @param block [Proc] block to be executed when the event is called
|
56
|
+
#
|
57
|
+
# @yield [Object] the model built
|
58
|
+
#
|
59
|
+
# @return [Builder] the builder itself
|
60
|
+
def after(event, &block)
|
61
|
+
tap { event_registry.register(event, &block) }
|
62
|
+
end
|
63
|
+
|
64
|
+
# Checks if another builder is equal to the current builder
|
65
|
+
#
|
66
|
+
# This is used mostly for rspec expectations
|
67
|
+
#
|
68
|
+
# @param other [Object] other object to be compared
|
69
|
+
#
|
70
|
+
# @return [TrueClass,FalseClass]
|
71
|
+
def ==(other)
|
72
|
+
return unless other.class == self.class
|
73
|
+
|
74
|
+
other.model_class == model_class
|
75
|
+
end
|
76
|
+
|
77
|
+
protected
|
78
|
+
|
79
|
+
# @method model_class
|
80
|
+
# @api private
|
81
|
+
#
|
82
|
+
# Model class to be initialized into a model
|
83
|
+
#
|
84
|
+
# @return [Class]
|
85
|
+
attr_reader :model_class
|
86
|
+
|
87
|
+
# @private
|
88
|
+
#
|
89
|
+
# Event registry
|
90
|
+
#
|
91
|
+
# The event registry will contain all handlers for
|
92
|
+
# post build or creating events
|
93
|
+
#
|
94
|
+
# @return [Jace::Registry]
|
95
|
+
def event_registry
|
96
|
+
@event_registry ||= Jace::Registry.new
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Zyra
|
4
|
+
# @api private
|
5
|
+
#
|
6
|
+
# Registry of all registered builders
|
7
|
+
class Registry
|
8
|
+
# (see Zyra.register)
|
9
|
+
def register(klass, key = klass.name.gsub(/::([A-Z])/, '_\1').downcase)
|
10
|
+
registry[key.to_sym] = Builder.new(klass)
|
11
|
+
end
|
12
|
+
|
13
|
+
# Returns a registered builder
|
14
|
+
#
|
15
|
+
# when the builder was not registerd, +nil+ is returned
|
16
|
+
#
|
17
|
+
# @param key [String,Symbol] key under which the builder is registered
|
18
|
+
#
|
19
|
+
# @return [Zyra::Builder]
|
20
|
+
def builder_for(key)
|
21
|
+
registry[key.to_sym]
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
# @private
|
27
|
+
#
|
28
|
+
# Registry store for all builders
|
29
|
+
#
|
30
|
+
# @return [Hash] map of all registered builders
|
31
|
+
def registry
|
32
|
+
@registry ||= {}
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/zyra/version.rb
CHANGED
data/lib/zyra.rb
CHANGED
@@ -1,7 +1,119 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'sinclair'
|
4
|
+
require 'jace'
|
5
|
+
|
3
6
|
# @api public
|
4
7
|
# @author darthjee
|
5
|
-
|
8
|
+
#
|
9
|
+
# Zyra allows builders to be registered to ease up
|
10
|
+
# seeding
|
11
|
+
module Zyra
|
6
12
|
autoload :VERSION, 'zyra/version'
|
13
|
+
|
14
|
+
autoload :Builder, 'zyra/builder'
|
15
|
+
autoload :Exceptions, 'zyra/exceptions'
|
16
|
+
autoload :Registry, 'zyra/registry'
|
17
|
+
|
18
|
+
class << self
|
19
|
+
# @method .register
|
20
|
+
# @api public
|
21
|
+
#
|
22
|
+
# Register a new builder
|
23
|
+
#
|
24
|
+
# The builder will focus on one class and be registered under a
|
25
|
+
# symbol key
|
26
|
+
#
|
27
|
+
# @param klass [Class] Model class to be used by the builder
|
28
|
+
#
|
29
|
+
# @overload register(klass)
|
30
|
+
# When the key is not provided, it is infered from the class name
|
31
|
+
#
|
32
|
+
# @overload register(klass, key)
|
33
|
+
# @param key [String,Symbol] key to be used when storyin the builder
|
34
|
+
#
|
35
|
+
# @return [Zyra::Builder] registered builder
|
36
|
+
delegate :register, to: :registry
|
37
|
+
|
38
|
+
# Register a handler on a certain event
|
39
|
+
#
|
40
|
+
# Possible events are +build+, +create+
|
41
|
+
#
|
42
|
+
# @param key [String,Symbol] key under which the {Builder builder}
|
43
|
+
# is {Registry registered}
|
44
|
+
# @param (see Builder#after)
|
45
|
+
#
|
46
|
+
# @yield [Object] the model built
|
47
|
+
#
|
48
|
+
# @return (see Builder#after)
|
49
|
+
def after(key, event, &block)
|
50
|
+
builder_for(key).after(event, &block)
|
51
|
+
end
|
52
|
+
|
53
|
+
# Builds an instance of the registered model class
|
54
|
+
#
|
55
|
+
# @param key [String,Symbol] key under which the {Builder builder}
|
56
|
+
# is {Registry registered}
|
57
|
+
# @param (see Builder#build)
|
58
|
+
#
|
59
|
+
# @yield (see Builder#build)
|
60
|
+
#
|
61
|
+
# @return (see Builder#build)
|
62
|
+
#
|
63
|
+
# @see .register
|
64
|
+
# @see Builder#build
|
65
|
+
def build(key, **attributes, &block)
|
66
|
+
builder_for(key).build(**attributes, &block)
|
67
|
+
end
|
68
|
+
|
69
|
+
# Creates an instance of the registered model class
|
70
|
+
#
|
71
|
+
# This behaves like {.build}, but persists the entry
|
72
|
+
#
|
73
|
+
# @param (see .build)
|
74
|
+
# @yield (see .build)
|
75
|
+
# @return (see .build)
|
76
|
+
#
|
77
|
+
# @see .register
|
78
|
+
# @see Builder#create
|
79
|
+
def create(key, **attributes, &block)
|
80
|
+
builder_for(key).create(**attributes, &block)
|
81
|
+
end
|
82
|
+
|
83
|
+
# @api private
|
84
|
+
#
|
85
|
+
# Resets the state of the registry
|
86
|
+
#
|
87
|
+
# This is mainly used for testing
|
88
|
+
#
|
89
|
+
# @return [NilClass]
|
90
|
+
def reset
|
91
|
+
@registry = nil
|
92
|
+
end
|
93
|
+
|
94
|
+
private
|
95
|
+
|
96
|
+
# @private
|
97
|
+
# @api private
|
98
|
+
#
|
99
|
+
# Returns a registered builder for a key
|
100
|
+
#
|
101
|
+
# @param key [String,Symbol] key under which the {Builder builder}
|
102
|
+
# is {Registry registered}
|
103
|
+
#
|
104
|
+
# @return [Builder]
|
105
|
+
def builder_for(key)
|
106
|
+
registry.builder_for(key) || raise(Exceptions::BuilderNotRegistered)
|
107
|
+
end
|
108
|
+
|
109
|
+
# @private
|
110
|
+
# @api private
|
111
|
+
#
|
112
|
+
# Returns the registry containing all the builders
|
113
|
+
#
|
114
|
+
# @return [Registry]
|
115
|
+
def registry
|
116
|
+
@registry ||= Registry.new
|
117
|
+
end
|
118
|
+
end
|
7
119
|
end
|
@@ -0,0 +1,129 @@
|
|
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
|
@@ -0,0 +1,105 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Zyra::Registry do
|
6
|
+
let(:registry) { described_class.new }
|
7
|
+
|
8
|
+
describe '#register' do
|
9
|
+
context 'when providing symbol alias' do
|
10
|
+
let(:key) { :user_alias }
|
11
|
+
|
12
|
+
it 'creates a builder for the given class' do
|
13
|
+
expect(registry.register(User, key))
|
14
|
+
.to eq(Zyra::Builder.new(User))
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'register builder under the key' do
|
18
|
+
expect { registry.register(User, key) }
|
19
|
+
.to change { registry.builder_for(key) }
|
20
|
+
.from(nil).to(Zyra::Builder.new(User))
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'when providing string alias' do
|
25
|
+
let(:key) { 'user' }
|
26
|
+
|
27
|
+
it 'creates a builder for the given class' do
|
28
|
+
expect(registry.register(User, key))
|
29
|
+
.to eq(Zyra::Builder.new(User))
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'register builder under the key' do
|
33
|
+
expect { registry.register(User, key) }
|
34
|
+
.to change { registry.builder_for(key) }
|
35
|
+
.from(nil).to(Zyra::Builder.new(User))
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context 'when not providing an alias' do
|
40
|
+
let(:key) { :user }
|
41
|
+
|
42
|
+
it 'creates a builder for the given class' do
|
43
|
+
expect(registry.register(User))
|
44
|
+
.to eq(Zyra::Builder.new(User))
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'register builder under the correct key' do
|
48
|
+
expect { registry.register(User) }
|
49
|
+
.to change { registry.builder_for(key) }
|
50
|
+
.from(nil).to(Zyra::Builder.new(User))
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe '#builder_for' do
|
56
|
+
let(:key) { :user }
|
57
|
+
|
58
|
+
context 'when there is no builder registered' do
|
59
|
+
it do
|
60
|
+
expect(registry.builder_for(key))
|
61
|
+
.to be_nil
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context 'when there is no builder registered on a symbol key' do
|
66
|
+
before do
|
67
|
+
registry.register(User, :user)
|
68
|
+
end
|
69
|
+
|
70
|
+
it do
|
71
|
+
expect(registry.builder_for(key))
|
72
|
+
.to eq(Zyra::Builder.new(User))
|
73
|
+
end
|
74
|
+
|
75
|
+
context 'when passing a string key' do
|
76
|
+
let(:key) { 'user' }
|
77
|
+
|
78
|
+
it do
|
79
|
+
expect(registry.builder_for(key))
|
80
|
+
.to eq(Zyra::Builder.new(User))
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
context 'when there is no builder registered on a string key' do
|
86
|
+
before do
|
87
|
+
registry.register(User, 'user')
|
88
|
+
end
|
89
|
+
|
90
|
+
it do
|
91
|
+
expect(registry.builder_for(key))
|
92
|
+
.to eq(Zyra::Builder.new(User))
|
93
|
+
end
|
94
|
+
|
95
|
+
context 'when passing a string key' do
|
96
|
+
let(:key) { 'user' }
|
97
|
+
|
98
|
+
it do
|
99
|
+
expect(registry.builder_for(key))
|
100
|
+
.to eq(Zyra::Builder.new(User))
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
data/spec/lib/zyra_spec.rb
CHANGED
@@ -3,6 +3,155 @@
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
5
|
describe Zyra do
|
6
|
-
|
6
|
+
before { described_class.reset }
|
7
|
+
|
8
|
+
describe '.build' do
|
9
|
+
let(:model_class) { User }
|
10
|
+
let(:key) { :user }
|
11
|
+
|
12
|
+
context 'when a builder has not been registered' do
|
13
|
+
it do
|
14
|
+
expect { described_class.build(key) }
|
15
|
+
.to raise_error(Zyra::Exceptions::BuilderNotRegistered)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'when a builder has been registered' do
|
20
|
+
before do
|
21
|
+
described_class.register(model_class)
|
22
|
+
end
|
23
|
+
|
24
|
+
it do
|
25
|
+
expect(described_class.build(key)).to be_a(model_class)
|
26
|
+
end
|
27
|
+
|
28
|
+
it do
|
29
|
+
expect(described_class.build(key))
|
30
|
+
.not_to be_persisted
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'when attributes are given' do
|
34
|
+
let(:name) { SecureRandom.hex(10) }
|
35
|
+
|
36
|
+
it 'initializes the model with the given attribute' do
|
37
|
+
expect(described_class.build(key, name: name).name)
|
38
|
+
.to eq(name)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context 'when a block is given' do
|
43
|
+
let(:name) { SecureRandom.hex(10) }
|
44
|
+
|
45
|
+
it 'initializes the model with the given attribute' do
|
46
|
+
value = name
|
47
|
+
expect(described_class.build(key) { |model| model.name = value }.name)
|
48
|
+
.to eq(name)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'when building has an after build handler' do
|
53
|
+
let(:name) { SecureRandom.hex(10) }
|
54
|
+
|
55
|
+
before do
|
56
|
+
value = name
|
57
|
+
described_class.after(:user, :build) do |model|
|
58
|
+
model.name = value
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'runs the event handler' do
|
63
|
+
expect(described_class.build(key) .name)
|
64
|
+
.to eq(name)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
describe '.create' do
|
71
|
+
let(:model_class) { User }
|
72
|
+
let(:key) { :user }
|
73
|
+
|
74
|
+
context 'when a builder has not been registered' do
|
75
|
+
it do
|
76
|
+
expect { described_class.create(key) }
|
77
|
+
.to raise_error(Zyra::Exceptions::BuilderNotRegistered)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
context 'when a builder has been registered' do
|
82
|
+
before do
|
83
|
+
described_class.register(model_class)
|
84
|
+
end
|
85
|
+
|
86
|
+
it do
|
87
|
+
expect(described_class.create(key)).to be_a(model_class)
|
88
|
+
end
|
89
|
+
|
90
|
+
it do
|
91
|
+
expect(described_class.create(key))
|
92
|
+
.to be_persisted
|
93
|
+
end
|
94
|
+
|
95
|
+
context 'when attributes are given' do
|
96
|
+
let(:name) { SecureRandom.hex(10) }
|
97
|
+
|
98
|
+
it 'initializes the model with the given attribute' do
|
99
|
+
expect(described_class.create(key, name: name).name)
|
100
|
+
.to eq(name)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
context 'when a block is given' do
|
105
|
+
let(:name) { SecureRandom.hex(10) }
|
106
|
+
|
107
|
+
it 'initializes the model with the given attribute' do
|
108
|
+
value = name
|
109
|
+
model = described_class.create(key) { |mdl| mdl.name = value }
|
110
|
+
expect(model.name)
|
111
|
+
.to eq(name)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
context 'when building has an after build handler' do
|
116
|
+
let(:name) { SecureRandom.hex(10) }
|
117
|
+
|
118
|
+
before do
|
119
|
+
value = name
|
120
|
+
described_class.after(:user, :build) do |model|
|
121
|
+
model.name = value
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
it 'runs the event handler' do
|
126
|
+
expect(described_class.create(key) .name)
|
127
|
+
.to eq(name)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
describe '.after' do
|
134
|
+
let(:model_class) { User }
|
135
|
+
let(:key) { :user }
|
136
|
+
let(:name) { SecureRandom.hex(10) }
|
137
|
+
|
138
|
+
context 'when a builder has been registered' do
|
139
|
+
before do
|
140
|
+
described_class.register(model_class)
|
141
|
+
end
|
142
|
+
|
143
|
+
it 'register a handler to be ran after an event' do
|
144
|
+
value = name
|
145
|
+
|
146
|
+
expect { described_class.after(key, :build) { |m| m.name = value } }
|
147
|
+
.to change { described_class.build(key).name }
|
148
|
+
.from(nil).to(name)
|
149
|
+
end
|
150
|
+
|
151
|
+
it do
|
152
|
+
expect(described_class.after(key, :build) {})
|
153
|
+
.to be_a(Zyra::Builder)
|
154
|
+
end
|
155
|
+
end
|
7
156
|
end
|
8
157
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -11,6 +11,11 @@ SimpleCov.start 'gem'
|
|
11
11
|
require 'zyra'
|
12
12
|
require 'pry-nav'
|
13
13
|
|
14
|
+
require 'active_record'
|
15
|
+
ActiveRecord::Base.establish_connection(
|
16
|
+
adapter: 'sqlite3', database: ':memory:'
|
17
|
+
)
|
18
|
+
|
14
19
|
support_files = File.expand_path('spec/support/**/*.rb')
|
15
20
|
|
16
21
|
Dir[support_files].sort.each { |file| require file }
|
data/zyra.gemspec
CHANGED
@@ -19,6 +19,10 @@ Gem::Specification.new do |gem|
|
|
19
19
|
gem.test_files = gem.files.grep(%r{^(test|gem|features)/})
|
20
20
|
gem.require_paths = ['lib']
|
21
21
|
|
22
|
+
gem.add_runtime_dependency 'jace', '>= 0.1.1'
|
23
|
+
gem.add_runtime_dependency 'sinclair', '>= 1.8.0'
|
24
|
+
|
25
|
+
gem.add_development_dependency 'activerecord', '5.2.8.1'
|
22
26
|
gem.add_development_dependency 'bundler', '2.3.20'
|
23
27
|
gem.add_development_dependency 'pry', '0.14.1'
|
24
28
|
gem.add_development_dependency 'pry-nav', '1.0.0'
|
@@ -33,6 +37,7 @@ Gem::Specification.new do |gem|
|
|
33
37
|
gem.add_development_dependency 'rubocop-rspec', '1.38.1'
|
34
38
|
gem.add_development_dependency 'rubycritic', '4.7.0'
|
35
39
|
gem.add_development_dependency 'simplecov', '0.21.2'
|
40
|
+
gem.add_development_dependency 'sqlite3', '1.4.2'
|
36
41
|
gem.add_development_dependency 'yard', '0.9.27'
|
37
42
|
gem.add_development_dependency 'yardstick', '0.9.9'
|
38
43
|
end
|
metadata
CHANGED
@@ -1,15 +1,57 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zyra
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- DarthJee
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-09-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: jace
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.1.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.1.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sinclair
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.8.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.8.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activerecord
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 5.2.8.1
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 5.2.8.1
|
13
55
|
- !ruby/object:Gem::Dependency
|
14
56
|
name: bundler
|
15
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -206,6 +248,20 @@ dependencies:
|
|
206
248
|
- - '='
|
207
249
|
- !ruby/object:Gem::Version
|
208
250
|
version: 0.21.2
|
251
|
+
- !ruby/object:Gem::Dependency
|
252
|
+
name: sqlite3
|
253
|
+
requirement: !ruby/object:Gem::Requirement
|
254
|
+
requirements:
|
255
|
+
- - '='
|
256
|
+
- !ruby/object:Gem::Version
|
257
|
+
version: 1.4.2
|
258
|
+
type: :development
|
259
|
+
prerelease: false
|
260
|
+
version_requirements: !ruby/object:Gem::Requirement
|
261
|
+
requirements:
|
262
|
+
- - '='
|
263
|
+
- !ruby/object:Gem::Version
|
264
|
+
version: 1.4.2
|
209
265
|
- !ruby/object:Gem::Dependency
|
210
266
|
name: yard
|
211
267
|
requirement: !ruby/object:Gem::Requirement
|
@@ -247,6 +303,7 @@ files:
|
|
247
303
|
- ".rubocop.yml"
|
248
304
|
- ".rubocop_todo.yml"
|
249
305
|
- Dockerfile
|
306
|
+
- Dockerfile.circleci
|
250
307
|
- Gemfile
|
251
308
|
- LICENSE
|
252
309
|
- README.md
|
@@ -257,12 +314,19 @@ files:
|
|
257
314
|
- config/yardstick.yml
|
258
315
|
- docker-compose.yml
|
259
316
|
- lib/zyra.rb
|
317
|
+
- lib/zyra/builder.rb
|
318
|
+
- lib/zyra/exceptions.rb
|
319
|
+
- lib/zyra/registry.rb
|
260
320
|
- lib/zyra/version.rb
|
261
321
|
- spec/integration/readme/.keep
|
262
322
|
- spec/integration/yard/.keep
|
323
|
+
- spec/lib/zyra/builder_spec.rb
|
324
|
+
- spec/lib/zyra/registry_spec.rb
|
263
325
|
- spec/lib/zyra_spec.rb
|
264
326
|
- spec/spec_helper.rb
|
265
327
|
- spec/support/models/.keep
|
328
|
+
- spec/support/models/user.rb
|
329
|
+
- spec/support/schema.rb
|
266
330
|
- spec/support/shared_examples/.keep
|
267
331
|
- zyra.gemspec
|
268
332
|
- zyra.jpg
|