sinclair 1.4.2 → 1.5.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 +4 -4
- data/.circleci/config.yml +4 -1
- data/Dockerfile +3 -3
- data/README.md +1 -1
- data/config/check_specs.yml +5 -0
- data/config/yardstick.yml +3 -0
- data/lib/sinclair.rb +26 -9
- data/lib/sinclair/config/methods_builder.rb +3 -1
- data/lib/sinclair/config_factory.rb +4 -2
- data/lib/sinclair/matchers/add_class_method_to.rb +1 -0
- data/lib/sinclair/method_builder.rb +61 -0
- data/lib/sinclair/method_builder/base.rb +67 -0
- data/lib/sinclair/method_builder/block_method_builder.rb +34 -0
- data/lib/sinclair/method_builder/string_method_builder.rb +50 -0
- data/lib/sinclair/method_definition.rb +38 -17
- data/lib/sinclair/method_definition/block_definition.rb +8 -94
- data/lib/sinclair/method_definition/block_helper.rb +46 -0
- data/lib/sinclair/method_definition/string_definition.rb +7 -66
- data/lib/sinclair/method_definitions.rb +16 -4
- data/lib/sinclair/version.rb +1 -1
- data/sinclair.gemspec +1 -1
- data/spec/lib/sinclair/method_builder/block_method_builder_spec.rb +55 -0
- data/spec/lib/sinclair/method_builder/string_method_builder_spec.rb +54 -0
- data/spec/lib/sinclair/method_builder_spec.rb +53 -0
- data/spec/lib/sinclair/method_definition/block_definition_spec.rb +66 -0
- data/spec/lib/sinclair/method_definition/block_helper_spec.rb +111 -0
- data/spec/lib/sinclair/method_definition/string_definition_spec.rb +70 -0
- data/spec/lib/sinclair/method_definition_spec.rb +23 -88
- data/spec/lib/sinclair/method_definitions_spec.rb +45 -0
- data/spec/support/models/random_generator.rb +13 -0
- metadata +18 -20
- data/lib/sinclair/method_definition/class_block_definition.rb +0 -22
- data/lib/sinclair/method_definition/class_method_definition.rb +0 -50
- data/lib/sinclair/method_definition/class_string_definition.rb +0 -24
- data/lib/sinclair/method_definition/instance_block_definition.rb +0 -22
- data/lib/sinclair/method_definition/instance_method_definition.rb +0 -48
- data/lib/sinclair/method_definition/instance_string_definition.rb +0 -24
- data/spec/integration/yard/sinclair/method_definition/class_block_definition_spec.rb +0 -34
- data/spec/integration/yard/sinclair/method_definition/class_method_definition_spec.rb +0 -28
- data/spec/integration/yard/sinclair/method_definition/class_string_definition_spec.rb +0 -23
- data/spec/integration/yard/sinclair/method_definition/instance_block_definition_spec.rb +0 -25
- data/spec/integration/yard/sinclair/method_definition/instance_method_definition_spec.rb +0 -35
- data/spec/integration/yard/sinclair/method_definition/instance_string_definition_spec.rb +0 -32
- data/spec/lib/sinclair/method_definition/class_block_definition_spec.rb +0 -29
- data/spec/lib/sinclair/method_definition/class_string_definition_spec.rb +0 -27
- data/spec/lib/sinclair/method_definition/instance_block_definition_spec.rb +0 -30
- data/spec/lib/sinclair/method_definition/instance_string_definition_spec.rb +0 -28
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4e368ae3e5bd4a4fdea678f56442dd2d88fb29c1b59ef31b0bebb668c35eb6f4
|
4
|
+
data.tar.gz: 2662bab987171ad5199be408d3b88af8076f261ee038265f1d143eeb3a56a6b6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f6d067cd6b01d8f1244ab3c32587d377d7b33460b1110f5209df3f0b5fd6b57dab5e5aa34fd7e7bfa1f462178e46ce8e50ce16ebbb8e78463447211753fde260
|
7
|
+
data.tar.gz: a25a3ad8031fe74fc8861c52e6cdfe1069a54049fd306929e0164f88f77e7f215d5ed816f842b06b80868862523d3e022b27f4dc78448cc39cf0a3f5af1f720d
|
data/.circleci/config.yml
CHANGED
@@ -2,7 +2,7 @@ version: 2
|
|
2
2
|
jobs:
|
3
3
|
build:
|
4
4
|
docker:
|
5
|
-
- image: darthjee/circleci_ruby_gems:0.2.
|
5
|
+
- image: darthjee/circleci_ruby_gems:0.2.3
|
6
6
|
environment:
|
7
7
|
PROJECT: sinclair
|
8
8
|
steps:
|
@@ -31,3 +31,6 @@ jobs:
|
|
31
31
|
- run:
|
32
32
|
name: Rubycritcs check
|
33
33
|
command: rubycritic.sh
|
34
|
+
- run:
|
35
|
+
name: Check unit tests
|
36
|
+
command: check_specs
|
data/Dockerfile
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
FROM darthjee/ruby_gems:0.2.
|
2
|
-
FROM darthjee/scripts:0.1.
|
1
|
+
FROM darthjee/ruby_gems:0.2.3 as base
|
2
|
+
FROM darthjee/scripts:0.1.2 as scripts
|
3
3
|
|
4
4
|
######################################
|
5
5
|
|
@@ -24,7 +24,7 @@ COPY --chown=app:app --from=builder /home/app/bundle/specifications /usr/local/b
|
|
24
24
|
COPY --chown=app:app --from=builder /home/app/bundle/bin /usr/local/bundle/bin
|
25
25
|
COPY --chown=app:app --from=builder /home/app/bundle/extensions /usr/local/bundle/extensions
|
26
26
|
|
27
|
-
COPY --chown=app ./*.gemspec ./Gemfile
|
27
|
+
COPY --chown=app ./*.gemspec ./Gemfile /home/app/app/
|
28
28
|
COPY --chown=app ./lib/sinclair/version.rb /home/app/app/lib/sinclair/
|
29
29
|
USER app
|
30
30
|
RUN bundle install
|
data/README.md
CHANGED
data/config/yardstick.yml
CHANGED
@@ -26,6 +26,7 @@ rules:
|
|
26
26
|
exclude:
|
27
27
|
- Sinclair::Matchers::AddClassMethodTo#raise_block_syntax_error
|
28
28
|
- Sinclair::Matchers::AddInstanceMethodTo#raise_block_syntax_error
|
29
|
+
- Sinclair::MethodBuilder#build_from_definition
|
29
30
|
Summary::Presence:
|
30
31
|
enabled: true
|
31
32
|
exclude:
|
@@ -35,6 +36,8 @@ rules:
|
|
35
36
|
- Sinclair::Matchers::AddInstanceMethodTo#instance
|
36
37
|
- Sinclair::Matchers::AddMethod#initialize
|
37
38
|
- Sinclair::Matchers::AddMethodTo#initialize
|
39
|
+
- Sinclair::MethodBuilder
|
40
|
+
- Sinclair::MethodBuilder::Base#initialize
|
38
41
|
- Sinclair::MethodDefinition#initialize
|
39
42
|
- Sinclair::MethodDefinition::BlockDefinition#initialize
|
40
43
|
- Sinclair::MethodDefinition::StringDefinition#initialize
|
data/lib/sinclair.rb
CHANGED
@@ -88,6 +88,7 @@ class Sinclair
|
|
88
88
|
autoload :ConfigClass, 'sinclair/config_class'
|
89
89
|
autoload :ConfigFactory, 'sinclair/config_factory'
|
90
90
|
autoload :Configurable, 'sinclair/configurable'
|
91
|
+
autoload :MethodBuilder, 'sinclair/method_builder'
|
91
92
|
autoload :MethodDefinition, 'sinclair/method_definition'
|
92
93
|
autoload :MethodDefinitions, 'sinclair/method_definitions'
|
93
94
|
|
@@ -167,9 +168,8 @@ class Sinclair
|
|
167
168
|
#
|
168
169
|
# @return [Array<MethodDefinition>]
|
169
170
|
def build
|
170
|
-
definitions
|
171
|
-
|
172
|
-
end
|
171
|
+
builder.build_methods(definitions, MethodBuilder::INSTANCE_METHOD)
|
172
|
+
builder.build_methods(class_definitions, MethodBuilder::CLASS_METHOD)
|
173
173
|
end
|
174
174
|
|
175
175
|
# Add a method to the method list to be created on klass instances
|
@@ -220,7 +220,6 @@ class Sinclair
|
|
220
220
|
# @return [Array<MethodDefinition>]
|
221
221
|
def add_method(name, code = nil, **options, &block)
|
222
222
|
definitions.add(
|
223
|
-
MethodDefinition::InstanceMethodDefinition,
|
224
223
|
name, code, **options, &block
|
225
224
|
)
|
226
225
|
end
|
@@ -266,8 +265,7 @@ class Sinclair
|
|
266
265
|
#
|
267
266
|
# @return [Array<MethodDefinition>]
|
268
267
|
def add_class_method(name, code = nil, **options, &block)
|
269
|
-
|
270
|
-
MethodDefinition::ClassMethodDefinition,
|
268
|
+
class_definitions.add(
|
271
269
|
name, code, **options, &block
|
272
270
|
)
|
273
271
|
end
|
@@ -348,6 +346,7 @@ class Sinclair
|
|
348
346
|
|
349
347
|
private
|
350
348
|
|
349
|
+
attr_reader :klass
|
351
350
|
# @method klass
|
352
351
|
# @api private
|
353
352
|
# @private
|
@@ -355,16 +354,34 @@ class Sinclair
|
|
355
354
|
# Class that will receive the methods
|
356
355
|
#
|
357
356
|
# @return [Class]
|
358
|
-
attr_reader :klass
|
359
357
|
|
360
358
|
# @private
|
361
|
-
#
|
362
359
|
# @api private
|
363
360
|
#
|
364
|
-
# List of
|
361
|
+
# List of instance method definitions
|
365
362
|
#
|
366
363
|
# @return [MethodDefinitions]
|
367
364
|
def definitions
|
368
365
|
@definitions ||= MethodDefinitions.new
|
369
366
|
end
|
367
|
+
|
368
|
+
# @private
|
369
|
+
# @api private
|
370
|
+
#
|
371
|
+
# List of class method definitions
|
372
|
+
#
|
373
|
+
# @return [MethodDefinitions]
|
374
|
+
def class_definitions
|
375
|
+
@class_definitions ||= MethodDefinitions.new
|
376
|
+
end
|
377
|
+
|
378
|
+
# @private
|
379
|
+
# @api private
|
380
|
+
#
|
381
|
+
# MethodBuilder binded to the class
|
382
|
+
#
|
383
|
+
# @return [MethodBuilder]
|
384
|
+
def builder
|
385
|
+
@builder ||= MethodBuilder.new(klass)
|
386
|
+
end
|
370
387
|
end
|
@@ -70,8 +70,10 @@ class Sinclair
|
|
70
70
|
|
71
71
|
private
|
72
72
|
|
73
|
+
attr_reader :names, :defaults
|
73
74
|
# @method names
|
74
75
|
# @private
|
76
|
+
# @api private
|
75
77
|
#
|
76
78
|
# List of configuration names
|
77
79
|
#
|
@@ -79,11 +81,11 @@ class Sinclair
|
|
79
81
|
|
80
82
|
# @method defaults
|
81
83
|
# @private
|
84
|
+
# @api private
|
82
85
|
#
|
83
86
|
# Configurations that will receive a default value
|
84
87
|
#
|
85
88
|
# @return [Hash]
|
86
|
-
attr_reader :names, :defaults
|
87
89
|
|
88
90
|
# @private
|
89
91
|
#
|
@@ -144,8 +144,10 @@ class Sinclair
|
|
144
144
|
|
145
145
|
private
|
146
146
|
|
147
|
-
|
147
|
+
attr_reader :config_class, :config_attributes
|
148
|
+
# @method config_class
|
148
149
|
# @private
|
150
|
+
# @api private
|
149
151
|
#
|
150
152
|
# Configuration class to be used
|
151
153
|
#
|
@@ -153,11 +155,11 @@ class Sinclair
|
|
153
155
|
|
154
156
|
# @method config_attributes
|
155
157
|
# @private
|
158
|
+
# @api private
|
156
159
|
#
|
157
160
|
# List of possible configurations
|
158
161
|
#
|
159
162
|
# @return [Array<Symbol,String>]
|
160
|
-
attr_reader :config_class, :config_attributes
|
161
163
|
|
162
164
|
# @private
|
163
165
|
#
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Sinclair
|
4
|
+
# @api private
|
5
|
+
# @author darthjee
|
6
|
+
#
|
7
|
+
# Class responsible for building methods
|
8
|
+
class MethodBuilder
|
9
|
+
autoload :Base, 'sinclair/method_builder/base'
|
10
|
+
autoload :StringMethodBuilder, 'sinclair/method_builder/string_method_builder'
|
11
|
+
autoload :BlockMethodBuilder, 'sinclair/method_builder/block_method_builder'
|
12
|
+
|
13
|
+
CLASS_METHOD = :class
|
14
|
+
INSTANCE_METHOD = :instance
|
15
|
+
|
16
|
+
# @param klass [Class] class to receive the method
|
17
|
+
def initialize(klass)
|
18
|
+
@klass = klass
|
19
|
+
end
|
20
|
+
|
21
|
+
# Builds methods
|
22
|
+
#
|
23
|
+
# @param definitions [MethodDefinitions] all methods
|
24
|
+
# definitions to be built
|
25
|
+
# @param type [Symbol] type of method to be built
|
26
|
+
#
|
27
|
+
# @return [MethodDefinitions]
|
28
|
+
def build_methods(definitions, type)
|
29
|
+
definitions.each do |definition|
|
30
|
+
build_from_definition(definition, type)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
attr_reader :klass
|
37
|
+
# @method klass
|
38
|
+
# @private
|
39
|
+
# @api private
|
40
|
+
#
|
41
|
+
# class to receive the method
|
42
|
+
#
|
43
|
+
# @return [Class]
|
44
|
+
|
45
|
+
# @private
|
46
|
+
#
|
47
|
+
# Build one method from definition
|
48
|
+
#
|
49
|
+
# @param definition [MethodDefinition] the method definition
|
50
|
+
# @param type [Symbol] type of method to be built
|
51
|
+
#
|
52
|
+
# @return (see Base#build)
|
53
|
+
def build_from_definition(definition, type)
|
54
|
+
if definition.string?
|
55
|
+
StringMethodBuilder.new(klass, definition, type: type).build
|
56
|
+
else
|
57
|
+
BlockMethodBuilder.new(klass, definition, type: type).build
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Sinclair
|
4
|
+
class MethodBuilder
|
5
|
+
# @api private
|
6
|
+
# @author darthjee
|
7
|
+
#
|
8
|
+
# Base class responsible for building methods
|
9
|
+
class Base
|
10
|
+
# @param klass [Class] class to receive the method
|
11
|
+
# @param definition [MethodDefinition] method defined
|
12
|
+
# @param type [Symbol] type of method to be build
|
13
|
+
# - +:instance+ instance methods
|
14
|
+
# - +:class+ class methods
|
15
|
+
def initialize(klass, definition, type:)
|
16
|
+
@klass = klass
|
17
|
+
@definition = definition
|
18
|
+
@type = type
|
19
|
+
end
|
20
|
+
|
21
|
+
# Build method (should be implemented in subclass)
|
22
|
+
#
|
23
|
+
# @return [Symbol] name of the method built
|
24
|
+
#
|
25
|
+
# @raise NotImplementedYet
|
26
|
+
def build
|
27
|
+
raise 'Not implemented yet. this should be imlemented in subclasses'
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
attr_reader :klass, :definition, :type
|
33
|
+
# @method klass
|
34
|
+
# @private
|
35
|
+
# @api private
|
36
|
+
#
|
37
|
+
# Class to receive the methods
|
38
|
+
#
|
39
|
+
# @return [Class]
|
40
|
+
|
41
|
+
# @method definition
|
42
|
+
# @private
|
43
|
+
# @api private
|
44
|
+
#
|
45
|
+
# return a definition object
|
46
|
+
#
|
47
|
+
# @return [MethodDefinition]
|
48
|
+
|
49
|
+
# @method type
|
50
|
+
# @private
|
51
|
+
# @api private
|
52
|
+
#
|
53
|
+
# Type of method, class or instance
|
54
|
+
#
|
55
|
+
# @return [Symbol]
|
56
|
+
|
57
|
+
# @private
|
58
|
+
#
|
59
|
+
# Checks if builder will build an instance method
|
60
|
+
#
|
61
|
+
# @return [TrueClass,FalseClass]
|
62
|
+
def instance?
|
63
|
+
type == INSTANCE_METHOD
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Sinclair
|
4
|
+
class MethodBuilder
|
5
|
+
# @api private
|
6
|
+
# @author darthjee
|
7
|
+
#
|
8
|
+
# Class responsible to build methods from
|
9
|
+
# block definitions
|
10
|
+
#
|
11
|
+
# @see MethodDefinition::BlockDefinition
|
12
|
+
class BlockMethodBuilder < Base
|
13
|
+
# Builds the method
|
14
|
+
#
|
15
|
+
# @return (see Base#build)
|
16
|
+
def build
|
17
|
+
klass.send(method_definition, name, method_block)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
delegate :name, :method_block, to: :definition
|
23
|
+
|
24
|
+
# @private
|
25
|
+
#
|
26
|
+
# name of the method used to define a new method on class
|
27
|
+
#
|
28
|
+
# @return [Symbol]
|
29
|
+
def method_definition
|
30
|
+
instance? ? :define_method : :define_singleton_method
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Sinclair
|
4
|
+
class MethodBuilder
|
5
|
+
# @api private
|
6
|
+
# @author darthjee
|
7
|
+
#
|
8
|
+
# Class responsible to build methods from
|
9
|
+
# string definitions
|
10
|
+
#
|
11
|
+
# @see MethodDefinition::StringDefinition
|
12
|
+
class StringMethodBuilder < Base
|
13
|
+
# Builds the method
|
14
|
+
#
|
15
|
+
# @return (see Base#build)
|
16
|
+
def build
|
17
|
+
klass.module_eval(code_definition, __FILE__, __LINE__ + 1)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
# @private
|
23
|
+
#
|
24
|
+
# string used for method name definition
|
25
|
+
#
|
26
|
+
# the string changes depending if it is
|
27
|
+
# a class or instance method
|
28
|
+
#
|
29
|
+
# @return [String]
|
30
|
+
def definition_name
|
31
|
+
instance? ? name : "self.#{name}"
|
32
|
+
end
|
33
|
+
|
34
|
+
# @private
|
35
|
+
#
|
36
|
+
# string with the code to be defined
|
37
|
+
#
|
38
|
+
# @return [String]
|
39
|
+
def code_definition
|
40
|
+
<<-CODE
|
41
|
+
def #{definition_name}
|
42
|
+
#{code_line}
|
43
|
+
end
|
44
|
+
CODE
|
45
|
+
end
|
46
|
+
|
47
|
+
delegate :code_line, :name, to: :definition
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -8,20 +8,50 @@ class Sinclair
|
|
8
8
|
class MethodDefinition
|
9
9
|
include Sinclair::OptionsParser
|
10
10
|
|
11
|
-
autoload :
|
12
|
-
autoload :
|
13
|
-
autoload :
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
11
|
+
autoload :BlockHelper, 'sinclair/method_definition/block_helper'
|
12
|
+
autoload :BlockDefinition, 'sinclair/method_definition/block_definition'
|
13
|
+
autoload :StringDefinition, 'sinclair/method_definition/string_definition'
|
14
|
+
|
15
|
+
# @method name
|
16
|
+
#
|
17
|
+
# name of the method
|
18
|
+
#
|
19
|
+
# @return [String,Symbol]
|
20
|
+
attr_reader :name
|
19
21
|
|
20
22
|
# Default options of initialization
|
21
23
|
DEFAULT_OPTIONS = {
|
22
24
|
cached: false
|
23
25
|
}.freeze
|
24
26
|
|
27
|
+
# Builds a method that will return the same value always
|
28
|
+
#
|
29
|
+
# @return [Symbol]
|
30
|
+
def self.default_value(method_name, value)
|
31
|
+
define_method(method_name) { value }
|
32
|
+
end
|
33
|
+
|
34
|
+
# @param name [String,Symbol] name of the method
|
35
|
+
# @param code [String] code to be evaluated as method
|
36
|
+
# @param block [Proc] block with code to be added as method
|
37
|
+
# @param options [Hash] Options of construction
|
38
|
+
# @option options cached [Boolean] Flag telling to create a block
|
39
|
+
# with cache
|
40
|
+
#
|
41
|
+
# builds a method definition based on arguments
|
42
|
+
#
|
43
|
+
# when block is given, returns a {BlockDefinition} and
|
44
|
+
# returns a {StringDefinition} otherwise
|
45
|
+
#
|
46
|
+
# @return [Base]
|
47
|
+
def self.from(name, code = nil, **options, &block)
|
48
|
+
if block
|
49
|
+
BlockDefinition.new(name, **options, &block)
|
50
|
+
else
|
51
|
+
StringDefinition.new(name, code, **options)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
25
55
|
# @param name [String,Symbol] name of the method
|
26
56
|
# @param options [Hash] Options of construction
|
27
57
|
# @option options cached [Boolean] Flag telling to create
|
@@ -48,15 +78,6 @@ class Sinclair
|
|
48
78
|
"Use #{self.class}.from to initialize a proper object"
|
49
79
|
end
|
50
80
|
|
51
|
-
private
|
52
|
-
|
53
|
-
# @method name
|
54
|
-
# @private
|
55
|
-
#
|
56
|
-
# name of the method
|
57
|
-
#
|
58
|
-
# @return [String,Symbol]
|
59
|
-
attr_reader :name
|
60
81
|
delegate :cached, to: :options_object
|
61
82
|
|
62
83
|
# @private
|