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.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +4 -1
  3. data/Dockerfile +3 -3
  4. data/README.md +1 -1
  5. data/config/check_specs.yml +5 -0
  6. data/config/yardstick.yml +3 -0
  7. data/lib/sinclair.rb +26 -9
  8. data/lib/sinclair/config/methods_builder.rb +3 -1
  9. data/lib/sinclair/config_factory.rb +4 -2
  10. data/lib/sinclair/matchers/add_class_method_to.rb +1 -0
  11. data/lib/sinclair/method_builder.rb +61 -0
  12. data/lib/sinclair/method_builder/base.rb +67 -0
  13. data/lib/sinclair/method_builder/block_method_builder.rb +34 -0
  14. data/lib/sinclair/method_builder/string_method_builder.rb +50 -0
  15. data/lib/sinclair/method_definition.rb +38 -17
  16. data/lib/sinclair/method_definition/block_definition.rb +8 -94
  17. data/lib/sinclair/method_definition/block_helper.rb +46 -0
  18. data/lib/sinclair/method_definition/string_definition.rb +7 -66
  19. data/lib/sinclair/method_definitions.rb +16 -4
  20. data/lib/sinclair/version.rb +1 -1
  21. data/sinclair.gemspec +1 -1
  22. data/spec/lib/sinclair/method_builder/block_method_builder_spec.rb +55 -0
  23. data/spec/lib/sinclair/method_builder/string_method_builder_spec.rb +54 -0
  24. data/spec/lib/sinclair/method_builder_spec.rb +53 -0
  25. data/spec/lib/sinclair/method_definition/block_definition_spec.rb +66 -0
  26. data/spec/lib/sinclair/method_definition/block_helper_spec.rb +111 -0
  27. data/spec/lib/sinclair/method_definition/string_definition_spec.rb +70 -0
  28. data/spec/lib/sinclair/method_definition_spec.rb +23 -88
  29. data/spec/lib/sinclair/method_definitions_spec.rb +45 -0
  30. data/spec/support/models/random_generator.rb +13 -0
  31. metadata +18 -20
  32. data/lib/sinclair/method_definition/class_block_definition.rb +0 -22
  33. data/lib/sinclair/method_definition/class_method_definition.rb +0 -50
  34. data/lib/sinclair/method_definition/class_string_definition.rb +0 -24
  35. data/lib/sinclair/method_definition/instance_block_definition.rb +0 -22
  36. data/lib/sinclair/method_definition/instance_method_definition.rb +0 -48
  37. data/lib/sinclair/method_definition/instance_string_definition.rb +0 -24
  38. data/spec/integration/yard/sinclair/method_definition/class_block_definition_spec.rb +0 -34
  39. data/spec/integration/yard/sinclair/method_definition/class_method_definition_spec.rb +0 -28
  40. data/spec/integration/yard/sinclair/method_definition/class_string_definition_spec.rb +0 -23
  41. data/spec/integration/yard/sinclair/method_definition/instance_block_definition_spec.rb +0 -25
  42. data/spec/integration/yard/sinclair/method_definition/instance_method_definition_spec.rb +0 -35
  43. data/spec/integration/yard/sinclair/method_definition/instance_string_definition_spec.rb +0 -32
  44. data/spec/lib/sinclair/method_definition/class_block_definition_spec.rb +0 -29
  45. data/spec/lib/sinclair/method_definition/class_string_definition_spec.rb +0 -27
  46. data/spec/lib/sinclair/method_definition/instance_block_definition_spec.rb +0 -30
  47. data/spec/lib/sinclair/method_definition/instance_string_definition_spec.rb +0 -28
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Sinclair
4
- class MethodDefinition
5
- # @api private
6
- # @author darthjee
7
- #
8
- # Define an class method from block
9
- class ClassBlockDefinition < BlockDefinition
10
- private
11
-
12
- # @private
13
- #
14
- # Method used to define a class method
15
- #
16
- # @return [Symbol] Always :define_singleton_method
17
- def method_definer
18
- :define_singleton_method
19
- end
20
- end
21
- end
22
- end
@@ -1,50 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Sinclair
4
- class MethodDefinition
5
- # @api private
6
- # @author darthjee
7
- #
8
- # Module responsible for building class method definitions
9
- module ClassMethodDefinition
10
- # Returns an instance method definition
11
- #
12
- # When block is given returns an instance of
13
- # {InstanceBlockDefinition}, and when not, returns
14
- # {InstanceStringDefinition}
15
- #
16
- # @param name [String,Symbol] name of the method
17
- # @param code [String] code to be evaluated as method
18
- # @param block [Proc] block with code to be added as method
19
- # @param options [Hash] Options of construction
20
- # @option options cached [Boolean] Flag telling to create
21
- # a method with cache
22
- #
23
- # @example With cache
24
- # klass = Class.new
25
- #
26
- # method_definition = Sinclair::MethodDefinition::InstanceMethodDefinition.from(
27
- # :sequence, cached: true
28
- # ) do
29
- # @x = @x.to_i ** 2 + 1
30
- # end
31
- #
32
- # method_definition.build(klass)
33
- #
34
- # klass.sequence # returns 1
35
- # klass.sequence # returns 1
36
- #
37
- # klass.instance_variable_get(:@x) # returns 1
38
- # klass.instance_variable_get(:@sequence) # returns 1
39
- #
40
- # @return MethodDefinition
41
- def self.from(name, code = nil, **options, &block)
42
- if block
43
- ClassBlockDefinition.new(name, **options, &block)
44
- else
45
- ClassStringDefinition.new(name, code, **options)
46
- end
47
- end
48
- end
49
- end
50
- end
@@ -1,24 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Sinclair
4
- class MethodDefinition
5
- # @api private
6
- # @author darthjee
7
- #
8
- # Define a method from string
9
- class ClassStringDefinition < StringDefinition
10
- private
11
-
12
- # @private
13
- #
14
- # String used when defining method
15
- #
16
- # Class definition appends +self.+ to method name
17
- #
18
- # @return [String]
19
- def method_name
20
- "self.#{name}"
21
- end
22
- end
23
- end
24
- end
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Sinclair
4
- class MethodDefinition
5
- # @api private
6
- # @author darthjee
7
- #
8
- # Define a method from block
9
- class InstanceBlockDefinition < BlockDefinition
10
- private
11
-
12
- # @private
13
- #
14
- # Method used to define an instance method
15
- #
16
- # @return [Symbol] Always :define_method
17
- def method_definer
18
- :define_method
19
- end
20
- end
21
- end
22
- end
@@ -1,48 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Sinclair
4
- class MethodDefinition
5
- # @api private
6
- # @author darthjee
7
- #
8
- # Module responsible for building instance method definitions
9
- module InstanceMethodDefinition
10
- # Returns an instance method definition
11
- #
12
- # When block is given returns an instance of
13
- # {InstanceBlockDefinition}, and when not, returns
14
- # {InstanceStringDefinition}
15
- #
16
- # @param name [String,Symbol] name of the method
17
- # @param code [String] code to be evaluated as method
18
- # @param block [Proc] block with code to be added as method
19
- # @param options [Hash] Options of construction
20
- # @option options cached [Boolean] Flag telling to create
21
- # a method with cache
22
- #
23
- # @example
24
- # klass = Class.new
25
- #
26
- # method_definition = Sinclair::MethodDefinition::InstanceMethodDefinition.from(
27
- # :sequence, '@x = @x.to_i ** 2 + 1'
28
- # )
29
- #
30
- # method_definition.build(klass)
31
- #
32
- # instance = klass.new
33
- #
34
- # instance.sequence # returns 1
35
- # instance.sequence # returns 2
36
- # instance.sequence # returns 5
37
- #
38
- # @return MethodDefinition
39
- def self.from(name, code = nil, **options, &block)
40
- if block
41
- InstanceBlockDefinition.new(name, **options, &block)
42
- else
43
- InstanceStringDefinition.new(name, code, **options)
44
- end
45
- end
46
- end
47
- end
48
- end
@@ -1,24 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Sinclair
4
- class MethodDefinition
5
- # @api private
6
- # @author darthjee
7
- #
8
- # Define an instance method from string
9
- class InstanceStringDefinition < StringDefinition
10
- private
11
-
12
- # @private
13
- #
14
- # String used when defining method
15
- #
16
- # Instance definition always returns +name+
17
- #
18
- # @return [String]
19
- def method_name
20
- name
21
- end
22
- end
23
- end
24
- end
@@ -1,34 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe Sinclair::MethodDefinition::ClassBlockDefinition do
6
- describe 'yard' do
7
- describe '#build' do
8
- subject(:method_definition) do
9
- described_class.new(name) do
10
- @x = @x.to_i**2 + 1
11
- end
12
- end
13
-
14
- let(:klass) { Class.new }
15
- let(:name) { :sequence }
16
-
17
- it 'adds a dynamic method' do
18
- expect { method_definition.build(klass) }.to add_class_method(name).to(klass)
19
- expect { klass.sequence }
20
- .to change { klass.instance_variable_get(:@x) }.from(nil).to 1
21
- expect(klass.sequence).to eq(2)
22
- expect(klass.sequence).to eq(5)
23
- end
24
-
25
- it 'changes instance variable' do
26
- method_definition.build(klass)
27
-
28
- expect { klass.sequence }
29
- .to change { klass.instance_variable_get(:@x) }
30
- .from(nil).to 1
31
- end
32
- end
33
- end
34
- end
@@ -1,28 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe Sinclair::MethodDefinition::ClassMethodDefinition do
6
- describe 'yard' do
7
- describe '#build' do
8
- describe 'using block with cache option' do
9
- subject(:method_definition) do
10
- described_class.from(name, cached: true) do
11
- @x = @x.to_i**2 + 1
12
- end
13
- end
14
-
15
- let(:klass) { Class.new }
16
- let(:name) { :sequence }
17
-
18
- it 'adds a dynamic method' do
19
- expect { method_definition.build(klass) }.to add_class_method(name).to(klass)
20
- expect { klass.sequence }
21
- .to change { klass.instance_variable_get(:@x) }.from(nil).to 1
22
- expect { klass.sequence }.not_to change(klass, :sequence)
23
- expect(klass.instance_variable_get(:@sequence)).to eq(1)
24
- end
25
- end
26
- end
27
- end
28
- end
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe Sinclair::MethodDefinition::ClassStringDefinition do
4
- describe 'yard' do
5
- describe '#build' do
6
- subject(:method_definition) do
7
- described_class.new(name, code, cached: true)
8
- end
9
-
10
- let(:klass) { Class.new }
11
- let(:code) { '@x = @x.to_i ** 2 + 1' }
12
- let(:name) { :sequence }
13
-
14
- it 'adds a dynamic method' do
15
- expect { method_definition.build(klass) }.to add_class_method(name).to(klass)
16
- expect { klass.sequence }
17
- .to change { klass.instance_variable_get(:@x) }.from(nil).to(1)
18
- expect { klass.sequence }.not_to change(klass, :sequence)
19
- expect(klass.instance_variable_get(:@sequence)).to eq(1)
20
- end
21
- end
22
- end
23
- end
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe Sinclair::MethodDefinition::InstanceBlockDefinition do
4
- describe 'yard' do
5
- describe '#build' do
6
- subject(:method_definition) do
7
- described_class.new(name, cached: true) do
8
- @x = @x.to_i**2 + 1
9
- end
10
- end
11
-
12
- let(:klass) { Class.new }
13
- let(:instance) { klass.new }
14
- let(:name) { :sequence }
15
-
16
- it 'adds a dynamic method' do
17
- expect { method_definition.build(klass) }.to add_method(name).to(instance)
18
- expect { instance.sequence }
19
- .to change { instance.instance_variable_get(:@x) }.from(nil).to(1)
20
- expect { instance.sequence }.not_to change(instance, :sequence)
21
- expect(instance.instance_variable_get(:@sequence)).to eq(1)
22
- end
23
- end
24
- end
25
- end
@@ -1,35 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe Sinclair::MethodDefinition::InstanceMethodDefinition do
6
- describe 'yard' do
7
- describe '#from' do
8
- describe 'using string method with no options' do
9
- subject(:method_definition) do
10
- described_class.from(name, code)
11
- end
12
-
13
- let(:klass) { Class.new }
14
- let(:instance) { klass.new }
15
- let(:code) { '@x = @x.to_i ** 2 + 1' }
16
- let(:name) { :sequence }
17
-
18
- it 'adds a dynamic method' do
19
- expect { method_definition.build(klass) }.to add_method(name).to(instance)
20
- expect(instance.sequence).to eq(1)
21
- expect(instance.sequence).to eq(2)
22
- expect(instance.sequence).to eq(5)
23
- end
24
-
25
- it 'changes instance variable' do
26
- method_definition.build(klass)
27
-
28
- expect { instance.sequence }
29
- .to change { instance.instance_variable_get(:@x) }
30
- .from(nil).to 1
31
- end
32
- end
33
- end
34
- end
35
- end
@@ -1,32 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe Sinclair::MethodDefinition::InstanceStringDefinition do
4
- describe 'yard' do
5
- describe '#build' do
6
- subject(:method_definition) do
7
- described_class.new(name, code)
8
- end
9
-
10
- let(:klass) { Class.new }
11
- let(:instance) { klass.new }
12
- let(:code) { '@x = @x.to_i ** 2 + 1' }
13
- let(:name) { :sequence }
14
-
15
- it 'adds a dynamic method' do
16
- expect { method_definition.build(klass) }.to add_method(name).to(instance)
17
- expect { instance.sequence }
18
- .to change { instance.instance_variable_get(:@x) }.from(nil).to 1
19
- expect(instance.sequence).to eq(2)
20
- expect(instance.sequence).to eq(5)
21
- end
22
-
23
- it 'changes instance variable' do
24
- method_definition.build(klass)
25
-
26
- expect { instance.sequence }
27
- .to change { instance.instance_variable_get(:@x) }
28
- .from(nil).to 1
29
- end
30
- end
31
- end
32
- end
@@ -1,29 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe Sinclair::MethodDefinition::ClassBlockDefinition do
6
- let(:klass) { Class.new }
7
-
8
- describe '#build' do
9
- subject(:method_definition) do
10
- described_class.new(method_name) do
11
- @x = @x.to_i + 1
12
- end
13
- end
14
-
15
- let(:method_name) { :the_method }
16
-
17
- it_behaves_like 'ClassMethodDefinition#build without cache'
18
-
19
- context 'with cached options' do
20
- subject(:method_definition) do
21
- described_class.new(method_name, cached: cached_option) do
22
- @x = @x.to_i + 1
23
- end
24
- end
25
-
26
- it_behaves_like 'ClassMethodDefinition#build with cache options'
27
- end
28
- end
29
- end
@@ -1,27 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe Sinclair::MethodDefinition::ClassStringDefinition do
6
- let(:klass) { Class.new }
7
-
8
- describe '#build' do
9
- subject(:method_definition) do
10
- described_class.new(method_name, code)
11
- end
12
-
13
- let(:method_name) { :the_method }
14
-
15
- let(:code) { '@x = @x.to_i + 1' }
16
-
17
- it_behaves_like 'ClassMethodDefinition#build without cache'
18
-
19
- context 'with cached options' do
20
- subject(:method_definition) do
21
- described_class.new(method_name, code, cached: cached_option)
22
- end
23
-
24
- it_behaves_like 'ClassMethodDefinition#build with cache options'
25
- end
26
- end
27
- end
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe Sinclair::MethodDefinition::InstanceBlockDefinition do
6
- let(:klass) { Class.new }
7
- let(:instance) { klass.new }
8
-
9
- describe '#build' do
10
- subject(:method_definition) do
11
- described_class.new(method_name) do
12
- @x = @x.to_i + 1
13
- end
14
- end
15
-
16
- let(:method_name) { :the_method }
17
-
18
- it_behaves_like 'MethodDefinition#build without cache'
19
-
20
- context 'with cached options' do
21
- subject(:method_definition) do
22
- described_class.new(method_name, cached: cached_option) do
23
- @x = @x.to_i + 1
24
- end
25
- end
26
-
27
- it_behaves_like 'MethodDefinition#build with cache options'
28
- end
29
- end
30
- end