sinclair 1.13.0 → 1.14.1
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 +4 -4
- data/Dockerfile.circleci +28 -0
- data/README.md +22 -10
- data/config/check_specs.yml +1 -0
- data/config/yardstick.yml +1 -0
- data/docker-compose.yml +11 -0
- data/lib/sinclair/method_definition/parameter_helper.rb +14 -3
- data/lib/sinclair/model/builder.rb +20 -7
- data/lib/sinclair/model/builder_options.rb +13 -0
- data/lib/sinclair/model.rb +51 -1
- data/lib/sinclair/version.rb +1 -1
- data/sinclair.gemspec +1 -1
- data/spec/lib/sinclair/method_definition/parameter_helper_spec.rb +22 -0
- data/spec/lib/sinclair/model_spec.rb +55 -97
- data/spec/support/models/car.rb +2 -1
- data/spec/support/models/human.rb +2 -1
- data/spec/support/models/job.rb +2 -1
- data/spec/support/models/tv.rb +2 -1
- data/spec/support/shared_examples/model.rb +121 -0
- metadata +8 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4984ad62935fbe5a85e51b846b5124b72153b94cf9433a8a668f824c11e5eeaf
|
4
|
+
data.tar.gz: 336e7c54378ccd676f59eb92fe2a1bf32de84ff9a8bc2a91c8a6dd1e6494b46d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f3a84c2a80644b15e31991cb99a314378453e642c29c9864080e96f28bd34234f2476735284291ba56dd48ab9d67883a5b16709a1aa67b31116ef262dd6b1df1
|
7
|
+
data.tar.gz: d1405b56826158314eb4d304b33c02dc18015e63fe58e716371f5a58c7a8f417652ea1026d5b533c844e7c5a89ffeae7990f70334e61dbf4019006f412359dfe
|
data/Dockerfile.circleci
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
FROM darthjee/scripts:0.3.1 as scripts
|
2
|
+
|
3
|
+
FROM darthjee/circleci_ruby_270:1.2.0 as base
|
4
|
+
|
5
|
+
COPY --chown=app:app ./ /home/circleci/project/
|
6
|
+
|
7
|
+
######################################
|
8
|
+
|
9
|
+
FROM base as builder
|
10
|
+
|
11
|
+
COPY --chown=circleci:circleci --from=scripts /home/scripts/builder/bundle_builder.sh /usr/local/sbin/bundle_builder.sh
|
12
|
+
|
13
|
+
ENV PROJECT sinclair
|
14
|
+
ENV HOME_DIR /home/circleci
|
15
|
+
RUN bundle_builder.sh
|
16
|
+
|
17
|
+
#######################
|
18
|
+
#FINAL IMAGE
|
19
|
+
FROM base
|
20
|
+
|
21
|
+
COPY --chown=circleci:circleci --from=builder /home/circleci/bundle/ /home/circleci/.rubygems/
|
22
|
+
|
23
|
+
COPY --chown=circleci:circleci --from=scripts /home/scripts/sbin/rubycritic.sh /usr/local/sbin/
|
24
|
+
COPY --chown=circleci:circleci --from=scripts /home/scripts/sbin/check_specs /usr/local/sbin/
|
25
|
+
COPY --chown=circleci:circleci --from=scripts /home/scripts/sbin/check_readme.sh /usr/local/sbin/
|
26
|
+
COPY --chown=circleci:circleci --from=scripts /home/scripts/sbin/build_gem.sh /usr/local/sbin/
|
27
|
+
|
28
|
+
RUN bundle install
|
data/README.md
CHANGED
@@ -9,17 +9,19 @@ Sinclair
|
|
9
9
|
|
10
10
|

|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
Sinclair is a Ruby gem that provides developers with a variety of utility modules and classes
|
13
|
+
to simplify common tasks, reusability and avoid boilerplate code. Whether you need to class methods to create methods on the fly,
|
14
|
+
create custom comparators, configure your application, create powerfull options, Sinclair has got you covered.
|
15
15
|
|
16
|
-
|
16
|
+
Employing Sinclair in your applications helps you streamline your development workflow and enhance your development process through more efficient, cleaner code
|
17
17
|
|
18
|
-
[
|
18
|
+
Current Release: [1.14.1](https://github.com/darthjee/sinclair/tree/1.14.1)
|
19
|
+
|
20
|
+
[Next release](https://github.com/darthjee/sinclair/compare/1.14.1...master)
|
19
21
|
|
20
22
|
Yard Documentation
|
21
23
|
-------------------
|
22
|
-
[https://www.rubydoc.info/gems/sinclair/1.
|
24
|
+
[https://www.rubydoc.info/gems/sinclair/1.14.1](https://www.rubydoc.info/gems/sinclair/1.14.1)
|
23
25
|
|
24
26
|
Installation
|
25
27
|
---------------
|
@@ -524,8 +526,16 @@ Client.config.url # returns 'http://interstella.com:8080'
|
|
524
526
|
|
525
527
|
### Sinclair::EnvSettable
|
526
528
|
|
527
|
-
|
528
|
-
|
529
|
+
EnvSettable is a convenient utility that allows you to read environment
|
530
|
+
variables using Ruby class methods.
|
531
|
+
|
532
|
+
With this tool, you can define the usage of environment variables for your application in a single location
|
533
|
+
allowing the use of prefixes to isolate groups of variables.
|
534
|
+
|
535
|
+
This not only makes your code more readable and maintainable but also adds layer of security by ensuring
|
536
|
+
that sensitive information like API keys and passwords are not exposed in your source code.
|
537
|
+
|
538
|
+
EnvSettable allows accessing those variables thorugh a simple meta-programable way
|
529
539
|
|
530
540
|
<details>
|
531
541
|
<summary>Using env settable example</summary>
|
@@ -627,7 +637,8 @@ When creating a model class, options can be passed
|
|
627
637
|
<summary>Example of simple usage</summary>
|
628
638
|
|
629
639
|
```ruby
|
630
|
-
class Human < Sinclair::Model
|
640
|
+
class Human < Sinclair::Model
|
641
|
+
initialize_with :name, :age, { gender: :undefined }, **{}
|
631
642
|
end
|
632
643
|
|
633
644
|
human1 = Human.new(name: 'John Doe', age: 22)
|
@@ -644,7 +655,8 @@ human1 == human2 # returns true
|
|
644
655
|
<summary>Example with options</summary>
|
645
656
|
|
646
657
|
```ruby
|
647
|
-
class Tv < Sinclair::Model
|
658
|
+
class Tv < Sinclair::Model
|
659
|
+
initialize_with :model, writter: false, comparable: false
|
648
660
|
end
|
649
661
|
|
650
662
|
tv1 = Tv.new(model: 'Sans Sunga Xt')
|
data/config/check_specs.yml
CHANGED
data/config/yardstick.yml
CHANGED
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
|
+
sinclair_circleci:
|
26
|
+
<<: *base
|
27
|
+
image: sinclair_circleci
|
28
|
+
container_name: sinclair_circleci
|
29
|
+
volumes:
|
30
|
+
- .:/home/circleci/project
|
31
|
+
working_dir: /home/circleci/project
|
32
|
+
command: /bin/bash
|
33
|
+
build:
|
34
|
+
dockerfile: Dockerfile.circleci
|
@@ -26,7 +26,7 @@ class Sinclair
|
|
26
26
|
# @param parameters_list [Array<Object>] list of parameters and defaults
|
27
27
|
# @param named [TrueClass,FalseClass] Flag informing if the parameters are
|
28
28
|
# named parameters
|
29
|
-
def initialize(parameters_list, named:
|
29
|
+
def initialize(parameters_list, named: nil)
|
30
30
|
@parameters_list = parameters_list
|
31
31
|
@named = named
|
32
32
|
end
|
@@ -40,7 +40,7 @@ class Sinclair
|
|
40
40
|
def strings
|
41
41
|
return [] unless parameters_list
|
42
42
|
|
43
|
-
parameters_strings + defaults_strings
|
43
|
+
parameters_strings + defaults_strings + wild_card_parameters
|
44
44
|
end
|
45
45
|
|
46
46
|
private
|
@@ -81,7 +81,18 @@ class Sinclair
|
|
81
81
|
# @return [Array<Symbol>]
|
82
82
|
def parameters
|
83
83
|
parameters_list.reject do |param|
|
84
|
-
param.is_a?(Hash)
|
84
|
+
param.is_a?(Hash) || param.to_s.match?(/^\*/)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
# Returns the named parameters that have not been defined
|
89
|
+
#
|
90
|
+
# THis is usually extra options
|
91
|
+
#
|
92
|
+
# @return [Array<String>]
|
93
|
+
def wild_card_parameters
|
94
|
+
parameters_list.reject do |param|
|
95
|
+
param.is_a?(Hash) || !param.to_s.match?(/^\*/)
|
85
96
|
end
|
86
97
|
end
|
87
98
|
|
@@ -26,11 +26,10 @@ class Sinclair
|
|
26
26
|
# method should be added
|
27
27
|
# @param comparable [TrueClass,FalseClass] flag to make the class {Comparable}
|
28
28
|
# by the fields
|
29
|
-
def initialize(klass, *attributes,
|
29
|
+
def initialize(klass, *attributes, **options)
|
30
30
|
super(klass)
|
31
31
|
@attributes = attributes.flatten
|
32
|
-
@
|
33
|
-
@comparable = comparable
|
32
|
+
@options = BuilderOptions.new(**options)
|
34
33
|
|
35
34
|
add_methods
|
36
35
|
change_equals
|
@@ -39,7 +38,9 @@ class Sinclair
|
|
39
38
|
|
40
39
|
private
|
41
40
|
|
42
|
-
attr_reader :attributes, :
|
41
|
+
attr_reader :attributes, :options
|
42
|
+
|
43
|
+
delegate :writter, :comparable, to: :options
|
43
44
|
alias writter? writter
|
44
45
|
alias comparable? comparable
|
45
46
|
|
@@ -54,6 +55,14 @@ class Sinclair
|
|
54
55
|
#
|
55
56
|
# @return [Array<Symbol,Hash>]
|
56
57
|
|
58
|
+
# @!method options
|
59
|
+
# @api private
|
60
|
+
# @private
|
61
|
+
#
|
62
|
+
# Class building options
|
63
|
+
#
|
64
|
+
# @return [BuilderOptions]
|
65
|
+
|
57
66
|
# @!method writter
|
58
67
|
# @api private
|
59
68
|
# @private
|
@@ -118,11 +127,15 @@ class Sinclair
|
|
118
127
|
#
|
119
128
|
# @return [Array<MethodDefinition>]
|
120
129
|
def change_initializer
|
121
|
-
|
130
|
+
lines = attributes_names.map do |attr|
|
122
131
|
"@#{attr} = #{attr}"
|
123
|
-
end
|
132
|
+
end
|
133
|
+
|
134
|
+
lines << 'super(**attributes)'
|
135
|
+
|
136
|
+
code = lines.join("\n")
|
124
137
|
|
125
|
-
add_method(:initialize, code, named_parameters: attributes)
|
138
|
+
add_method(:initialize, code, named_parameters: attributes + ['**attributes'])
|
126
139
|
end
|
127
140
|
|
128
141
|
# @private
|
data/lib/sinclair/model.rb
CHANGED
@@ -6,11 +6,14 @@ class Sinclair
|
|
6
6
|
#
|
7
7
|
# Basic model to be used when defining new classes quickly
|
8
8
|
class Model
|
9
|
-
autoload :Builder,
|
9
|
+
autoload :Builder, 'sinclair/model/builder'
|
10
|
+
autoload :BuilderOptions, 'sinclair/model/builder_options'
|
10
11
|
|
11
12
|
class << self
|
12
13
|
# Returns a new class that inherits from model
|
13
14
|
#
|
15
|
+
# @deprecated Use {.initialize_with} instead
|
16
|
+
#
|
14
17
|
# @overload for(*attributes, writter: true, comparable: true)
|
15
18
|
# @param attributes [Array<Symbol>] attributes to be added in both the
|
16
19
|
# initialization and adding the methos to the model
|
@@ -53,6 +56,53 @@ class Sinclair
|
|
53
56
|
Builder.new(klass, *attributes, **options).build
|
54
57
|
end
|
55
58
|
end
|
59
|
+
|
60
|
+
# Adds methods needed for the model
|
61
|
+
#
|
62
|
+
# The readers/writters, +==+ and initializer are added
|
63
|
+
#
|
64
|
+
# @overload initialize_with(*attributes, writter: true, comparable: true)
|
65
|
+
# @param attributes [Array<Symbol>] attributes to be added in both the
|
66
|
+
# initialization and adding the methos to the model
|
67
|
+
# @param writter [TrueClass,FalseClass] flag informing if the writter/setter
|
68
|
+
# method should be added
|
69
|
+
# @param comparable [TrueClass,FalseClass] flag to make the class {Comparable}
|
70
|
+
# by the fields
|
71
|
+
#
|
72
|
+
# @example A model with readers
|
73
|
+
# class Car < Sinclair::Model
|
74
|
+
# initialize_with(:brand, :model, writter: false)
|
75
|
+
# end
|
76
|
+
#
|
77
|
+
# car = Car.new(brand: :ford, model: :T)
|
78
|
+
#
|
79
|
+
# car.brand # returns :ford
|
80
|
+
# car.model # returns :T
|
81
|
+
#
|
82
|
+
# @overload initialize_with(*attributes, defaults, writter: true, comparable: true)
|
83
|
+
# @param attributes [Array<Symbol>] attributes to be added in both the
|
84
|
+
# initialization and adding the methos to the model
|
85
|
+
# @param defaults [Hash] attributes to be added with a default value in the initializer
|
86
|
+
# @param writter [TrueClass,FalseClass] flag informing if the writter/setter
|
87
|
+
# method should be added
|
88
|
+
# @param comparable [TrueClass,FalseClass] flag to make the class {Comparable}
|
89
|
+
# by the fields
|
90
|
+
#
|
91
|
+
# @example A model with writters
|
92
|
+
# class Job < Sinclair::Model
|
93
|
+
# initialize_with({ state: :starting }, writter: true)
|
94
|
+
# end
|
95
|
+
#
|
96
|
+
# job = Job.new
|
97
|
+
#
|
98
|
+
# job.state # returns :starting
|
99
|
+
# job.state = :done
|
100
|
+
# job.state # returns :done
|
101
|
+
#
|
102
|
+
# @return [Array<MethodDefinition>]
|
103
|
+
def initialize_with(*attributes, **options)
|
104
|
+
Builder.new(self, *attributes, **options).build
|
105
|
+
end
|
56
106
|
end
|
57
107
|
end
|
58
108
|
end
|
data/lib/sinclair/version.rb
CHANGED
data/sinclair.gemspec
CHANGED
@@ -21,7 +21,7 @@ Gem::Specification.new do |gem|
|
|
21
21
|
|
22
22
|
gem.add_runtime_dependency 'activesupport', '~> 5.2.0'
|
23
23
|
|
24
|
-
gem.add_development_dependency 'bundler', '2.3.25'
|
24
|
+
gem.add_development_dependency 'bundler', '>= 2.3.25'
|
25
25
|
gem.add_development_dependency 'pry', '0.14.1'
|
26
26
|
gem.add_development_dependency 'pry-nav', '1.0.0'
|
27
27
|
gem.add_development_dependency 'rake', '13.0.1'
|
@@ -22,6 +22,17 @@ describe Sinclair::MethodDefinition::ParameterHelper do
|
|
22
22
|
.to eq(['a = 10', 'b = "word"', 'c = true', 'd = false', 'e = nil', 'f = :symbol'])
|
23
23
|
end
|
24
24
|
end
|
25
|
+
|
26
|
+
context 'when the parameter is an undefined parameter' do
|
27
|
+
let(:parameters) do
|
28
|
+
[:a, :b, '*args', { c: 10, d: 'word' }]
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'returns a list of parameters' do
|
32
|
+
expect(described_class.parameters_from(parameters))
|
33
|
+
.to eq(['a', 'b', 'c = 10', 'd = "word"', '*args'])
|
34
|
+
end
|
35
|
+
end
|
25
36
|
end
|
26
37
|
|
27
38
|
context 'when parameters are named' do
|
@@ -42,6 +53,17 @@ describe Sinclair::MethodDefinition::ParameterHelper do
|
|
42
53
|
.to eq(['a: 10', 'b: "word"', 'c: true', 'd: false', 'e: nil', 'f: :symbol'])
|
43
54
|
end
|
44
55
|
end
|
56
|
+
|
57
|
+
context 'when the parameter is an undefined parameter' do
|
58
|
+
let(:parameters) do
|
59
|
+
[:a, :b, '**opts', { c: 10, d: 'word' }]
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'returns a list of parameters' do
|
63
|
+
expect(described_class.parameters_from(parameters, named: true))
|
64
|
+
.to eq(['a:', 'b:', 'c: 10', 'd: "word"', '**opts'])
|
65
|
+
end
|
66
|
+
end
|
45
67
|
end
|
46
68
|
end
|
47
69
|
end
|
@@ -3,136 +3,94 @@
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
5
|
describe Sinclair::Model do
|
6
|
-
|
7
|
-
subject(:model) { klass.new(name: name) }
|
6
|
+
subject(:model) { klass.new(name: name) }
|
8
7
|
|
9
|
-
|
10
|
-
|
8
|
+
let(:name) { SecureRandom.hex(10) }
|
9
|
+
let(:attributes) { %i[name] }
|
10
|
+
let(:options) { {} }
|
11
11
|
|
12
|
-
|
13
|
-
|
12
|
+
describe '.for' do
|
13
|
+
subject(:klass) { described_class.for(*attributes, **options) }
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
.to eq(described_class)
|
18
|
-
end
|
19
|
-
|
20
|
-
it 'returns a class with getter' do
|
21
|
-
expect(klass.instance_method(:name))
|
22
|
-
.to be_a(UnboundMethod)
|
23
|
-
end
|
24
|
-
|
25
|
-
it 'returns a class with setter' do
|
26
|
-
expect(klass.instance_method(:name=))
|
27
|
-
.to be_a(UnboundMethod)
|
28
|
-
end
|
15
|
+
it_behaves_like 'sinclair model building'
|
16
|
+
end
|
29
17
|
|
30
|
-
|
31
|
-
|
32
|
-
end
|
18
|
+
describe '.initialize_with' do
|
19
|
+
subject(:klass) { Class.new(described_class) }
|
33
20
|
|
34
|
-
|
35
|
-
|
21
|
+
context 'when no options are given' do
|
22
|
+
it do
|
23
|
+
expect { klass.initialize_with(*attributes, **options) }
|
24
|
+
.to add_method(:name).to(klass)
|
36
25
|
end
|
37
26
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
end
|
27
|
+
it do
|
28
|
+
expect { klass.initialize_with(*attributes, **options) }
|
29
|
+
.to add_method(:name=).to(klass)
|
42
30
|
end
|
43
31
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
it do
|
49
|
-
expect { model.name = name }
|
50
|
-
.to change(model, :name)
|
51
|
-
.from(nil)
|
52
|
-
.to(name)
|
53
|
-
end
|
32
|
+
it do
|
33
|
+
expect { klass.initialize_with(*attributes, **options) }
|
34
|
+
.to change_method(:==).on(klass)
|
54
35
|
end
|
55
36
|
end
|
56
37
|
|
57
|
-
context 'when
|
58
|
-
|
59
|
-
|
60
|
-
let(:options) { { comparable: false } }
|
38
|
+
context 'when writter and comparable are not enabled' do
|
39
|
+
let(:options) { { writter: false, comparable: false } }
|
61
40
|
|
62
|
-
it
|
63
|
-
expect
|
41
|
+
it do
|
42
|
+
expect { klass.initialize_with(*attributes, **options) }
|
43
|
+
.to add_method(:name).to(klass)
|
64
44
|
end
|
65
|
-
end
|
66
|
-
|
67
|
-
context 'when the call happens with reader options' do
|
68
|
-
subject(:klass) { described_class.for(*attributes, **options) }
|
69
|
-
|
70
|
-
let(:options) { { writter: false } }
|
71
45
|
|
72
|
-
it
|
73
|
-
expect
|
74
|
-
.
|
46
|
+
it do
|
47
|
+
expect { klass.initialize_with(*attributes, **options) }
|
48
|
+
.not_to add_method(:name=).to(klass)
|
75
49
|
end
|
76
50
|
|
77
|
-
it
|
78
|
-
expect
|
79
|
-
.
|
51
|
+
it do
|
52
|
+
expect { klass.initialize_with(*attributes, **options) }
|
53
|
+
.not_to change_method(:==).on(klass)
|
80
54
|
end
|
55
|
+
end
|
81
56
|
|
82
|
-
|
83
|
-
|
84
|
-
.to raise_error(NameError)
|
85
|
-
end
|
57
|
+
context 'when class is subclass of another model' do
|
58
|
+
subject(:model) { klass.new(name: name, age: age) }
|
86
59
|
|
87
|
-
|
88
|
-
|
89
|
-
|
60
|
+
let(:age) { Random.rand(10..20) }
|
61
|
+
|
62
|
+
let(:superclass) do
|
63
|
+
Class.new(described_class) do
|
64
|
+
initialize_with(:age)
|
90
65
|
end
|
91
66
|
end
|
92
|
-
end
|
93
67
|
|
94
|
-
|
95
|
-
|
96
|
-
|
68
|
+
let(:klass) do
|
69
|
+
Class.new(superclass) do
|
70
|
+
initialize_with(:name)
|
71
|
+
end
|
97
72
|
end
|
98
73
|
|
99
|
-
it '
|
100
|
-
expect
|
101
|
-
.
|
74
|
+
it 'is initialized with both attributes' do
|
75
|
+
expect { klass.new(name: name, age: age) }
|
76
|
+
.not_to raise_error
|
102
77
|
end
|
103
78
|
|
104
|
-
it '
|
105
|
-
expect(
|
106
|
-
.to be_a(UnboundMethod)
|
79
|
+
it 'is initializes new attributes' do
|
80
|
+
expect(model.name).to eq(name)
|
107
81
|
end
|
108
82
|
|
109
|
-
it '
|
110
|
-
expect(
|
111
|
-
.to be_a(UnboundMethod)
|
83
|
+
it 'is initializes old attributes' do
|
84
|
+
expect(model.age).to eq(age)
|
112
85
|
end
|
86
|
+
end
|
113
87
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
let(:name) { SecureRandom.hex(10) }
|
118
|
-
|
119
|
-
it 'returns the dfault value' do
|
120
|
-
expect(model.name).to eq('John Doe')
|
121
|
-
end
|
88
|
+
context 'when the build is done' do
|
89
|
+
before do
|
90
|
+
klass.initialize_with(*attributes, **options)
|
122
91
|
end
|
123
92
|
|
124
|
-
|
125
|
-
subject(:model) { klass.new }
|
126
|
-
|
127
|
-
let(:name) { SecureRandom.hex(10) }
|
128
|
-
|
129
|
-
it do
|
130
|
-
expect { model.name = name }
|
131
|
-
.to change(model, :name)
|
132
|
-
.from('John Doe')
|
133
|
-
.to(name)
|
134
|
-
end
|
135
|
-
end
|
93
|
+
it_behaves_like 'sinclair model building'
|
136
94
|
end
|
137
95
|
end
|
138
96
|
end
|
data/spec/support/models/car.rb
CHANGED
data/spec/support/models/job.rb
CHANGED
data/spec/support/models/tv.rb
CHANGED
@@ -0,0 +1,121 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
shared_examples 'sinclair model building' do
|
4
|
+
context 'when the call happens with no options' do
|
5
|
+
it 'Returns a new class' do
|
6
|
+
expect(klass.superclass)
|
7
|
+
.to eq(described_class)
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'returns a class with getter' do
|
11
|
+
expect(klass.instance_method(:name))
|
12
|
+
.to be_a(UnboundMethod)
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'returns a class with setter' do
|
16
|
+
expect(klass.instance_method(:name=))
|
17
|
+
.to be_a(UnboundMethod)
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'returns a new class with a comparable that finds matches' do
|
21
|
+
expect(model).to eq(klass.new(name: name))
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'returns a new class with a comparable that find misses' do
|
25
|
+
expect(model).not_to eq(klass.new(name: SecureRandom.hex(10)))
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'when reader is called' do
|
29
|
+
it do
|
30
|
+
expect(model.name).to eq(name)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'when setter is called' do
|
35
|
+
let(:name) { SecureRandom.hex(10) }
|
36
|
+
let(:model) { klass.new(name: nil) }
|
37
|
+
|
38
|
+
it do
|
39
|
+
expect { model.name = name }
|
40
|
+
.to change(model, :name)
|
41
|
+
.from(nil)
|
42
|
+
.to(name)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'when the call happens with comparable false' do
|
48
|
+
let(:options) { { comparable: false } }
|
49
|
+
|
50
|
+
it 'returns a new class without comparable' do
|
51
|
+
expect(model).not_to eq(klass.new(name: name))
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context 'when the call happens with reader options' do
|
56
|
+
let(:options) { { writter: false } }
|
57
|
+
|
58
|
+
it 'Returns a new class' do
|
59
|
+
expect(klass.superclass)
|
60
|
+
.to eq(described_class)
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'returns a class with getter' do
|
64
|
+
expect(klass.instance_method(:name))
|
65
|
+
.to be_a(UnboundMethod)
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'returns a class without setter' do
|
69
|
+
expect { klass.instance_method(:name=) }
|
70
|
+
.to raise_error(NameError)
|
71
|
+
end
|
72
|
+
|
73
|
+
context 'when reader is called' do
|
74
|
+
it do
|
75
|
+
expect(model.name).to eq(name)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
context 'when the call happens with defaults' do
|
81
|
+
let(:attributes) { [{ name: 'John Doe' }] }
|
82
|
+
|
83
|
+
it 'Returns a new class' do
|
84
|
+
expect(klass.superclass)
|
85
|
+
.to eq(described_class)
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'returns a class with getter' do
|
89
|
+
expect(klass.instance_method(:name))
|
90
|
+
.to be_a(UnboundMethod)
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'returns a class with setter' do
|
94
|
+
expect(klass.instance_method(:name=))
|
95
|
+
.to be_a(UnboundMethod)
|
96
|
+
end
|
97
|
+
|
98
|
+
context 'when reader is called' do
|
99
|
+
subject(:model) { klass.new }
|
100
|
+
|
101
|
+
let(:name) { SecureRandom.hex(10) }
|
102
|
+
|
103
|
+
it 'returns the dfault value' do
|
104
|
+
expect(model.name).to eq('John Doe')
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
context 'when setter is called' do
|
109
|
+
subject(:model) { klass.new }
|
110
|
+
|
111
|
+
let(:name) { SecureRandom.hex(10) }
|
112
|
+
|
113
|
+
it do
|
114
|
+
expect { model.name = name }
|
115
|
+
.to change(model, :name)
|
116
|
+
.from('John Doe')
|
117
|
+
.to(name)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinclair
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.14.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- DarthJee
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-04-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -28,14 +28,14 @@ dependencies:
|
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: 2.3.25
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: 2.3.25
|
41
41
|
- !ruby/object:Gem::Dependency
|
@@ -261,6 +261,7 @@ files:
|
|
261
261
|
- ".rubocop.yml"
|
262
262
|
- ".rubocop_todo.yml"
|
263
263
|
- Dockerfile
|
264
|
+
- Dockerfile.circleci
|
264
265
|
- Gemfile
|
265
266
|
- LICENSE
|
266
267
|
- README.md
|
@@ -316,6 +317,7 @@ files:
|
|
316
317
|
- lib/sinclair/method_definitions.rb
|
317
318
|
- lib/sinclair/model.rb
|
318
319
|
- lib/sinclair/model/builder.rb
|
320
|
+
- lib/sinclair/model/builder_options.rb
|
319
321
|
- lib/sinclair/options.rb
|
320
322
|
- lib/sinclair/options/builder.rb
|
321
323
|
- lib/sinclair/options/class_methods.rb
|
@@ -448,6 +450,7 @@ files:
|
|
448
450
|
- spec/support/shared_examples/config_factory.rb
|
449
451
|
- spec/support/shared_examples/env_settable.rb
|
450
452
|
- spec/support/shared_examples/instance_method_definition.rb
|
453
|
+
- spec/support/shared_examples/model.rb
|
451
454
|
- spec/support/shared_examples/sinclair.rb
|
452
455
|
homepage: https://github.com/darthjee/sinclair
|
453
456
|
licenses: []
|
@@ -467,7 +470,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
467
470
|
- !ruby/object:Gem::Version
|
468
471
|
version: '0'
|
469
472
|
requirements: []
|
470
|
-
rubygems_version: 3.
|
473
|
+
rubygems_version: 3.1.2
|
471
474
|
signing_key:
|
472
475
|
specification_version: 4
|
473
476
|
summary: Gem for easy concern creation
|