sinclair 1.1.3 → 1.2.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.
@@ -3,19 +3,20 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  describe Sinclair do
6
- let(:options) { {} }
7
- let(:instance) { dummy_class.new }
8
- let(:dummy_class) { Class.new }
6
+ subject(:builder) { builder_class.new(dummy_class, options) }
7
+
8
+ let(:options) { {} }
9
+ let(:instance) { dummy_class.new }
10
+ let(:dummy_class) { Class.new }
9
11
  let(:builder_class) { described_class }
10
- subject { builder_class.new(dummy_class, options) }
11
12
 
12
13
  describe '#add_method' do
13
14
  context 'when extending the class' do
14
15
  let(:builder_class) { described_class::DummyBuilder }
15
16
 
16
17
  before do
17
- subject.init
18
- subject.build
18
+ builder.init
19
+ builder.build
19
20
  end
20
21
 
21
22
  context 'when describing a method with block' do
@@ -33,6 +34,7 @@ describe Sinclair do
33
34
 
34
35
  context 'when passing options' do
35
36
  let(:options) { { increment: 2 } }
37
+
36
38
  it 'parses the options' do
37
39
  expect(instance.defined).to eq(2)
38
40
  expect(instance.defined).to eq(4)
@@ -43,9 +45,9 @@ describe Sinclair do
43
45
  context 'when using the builder without extending' do
44
46
  context 'when declaring a method with a block' do
45
47
  before do
46
- subject.add_method(:blocked) { 1 }
47
- subject.add_method(:blocked) { 2 }
48
- subject.build
48
+ builder.add_method(:blocked) { 1 }
49
+ builder.add_method(:blocked) { 2 }
50
+ builder.build
49
51
  end
50
52
 
51
53
  it 'respect the order of method addtion' do
@@ -55,9 +57,9 @@ describe Sinclair do
55
57
 
56
58
  context 'when declaring a method string' do
57
59
  before do
58
- subject.add_method(:string, '1')
59
- subject.add_method(:string, '2')
60
- subject.build
60
+ builder.add_method(:string, '1')
61
+ builder.add_method(:string, '2')
62
+ builder.build
61
63
  end
62
64
 
63
65
  it 'respect the order of method addtion' do
@@ -65,29 +67,27 @@ describe Sinclair do
65
67
  end
66
68
  end
67
69
 
68
- context 'when declaring a method using string or block' do
69
- context 'when declaring the block first' do
70
- before do
71
- subject.add_method(:value) { 1 }
72
- subject.add_method(:value, '2')
73
- subject.build
74
- end
75
-
76
- it 'respect the order of method addtion' do
77
- expect(instance.value).to eq(2)
78
- end
70
+ context 'when declaring block and string' do
71
+ before do
72
+ builder.add_method(:value) { 1 }
73
+ builder.add_method(:value, '2')
74
+ builder.build
75
+ end
76
+
77
+ it 'respect the order of method addtion' do
78
+ expect(instance.value).to eq(2)
79
79
  end
80
+ end
80
81
 
81
- context 'when declaring the string first' do
82
- before do
83
- subject.add_method(:value, '1')
84
- subject.add_method(:value) { 2 }
85
- subject.build
86
- end
82
+ context 'when declaring string and block' do
83
+ before do
84
+ builder.add_method(:value, '1')
85
+ builder.add_method(:value) { 2 }
86
+ builder.build
87
+ end
87
88
 
88
- it 'respect the order of method addtion' do
89
- expect(instance.value).to eq(2)
90
- end
89
+ it 'respect the order of method addtion' do
90
+ expect(instance.value).to eq(2)
91
91
  end
92
92
  end
93
93
  end
@@ -96,11 +96,11 @@ describe Sinclair do
96
96
  describe '#eval_and_add_method' do
97
97
  context 'when defining the method once' do
98
98
  before do
99
- subject.add_method(:value, '@value ||= 0')
100
- subject.eval_and_add_method(:defined) do
99
+ builder.add_method(:value, '@value ||= 0')
100
+ builder.eval_and_add_method(:defined) do
101
101
  "@value = value + #{options_object.increment || 1}"
102
102
  end
103
- subject.build
103
+ builder.build
104
104
  end
105
105
 
106
106
  it 'creates a method using the string definition' do
@@ -120,12 +120,12 @@ describe Sinclair do
120
120
 
121
121
  context 'when redefining a method already added' do
122
122
  before do
123
- subject.add_method(:value, '@value ||= 0')
124
- subject.add_method(:defined, '100')
125
- subject.eval_and_add_method(:defined) do
123
+ builder.add_method(:value, '@value ||= 0')
124
+ builder.add_method(:defined, '100')
125
+ builder.eval_and_add_method(:defined) do
126
126
  "@value = value + #{options_object.increment || 1}"
127
127
  end
128
- subject.build
128
+ builder.build
129
129
  end
130
130
 
131
131
  it 'redefines it' do
@@ -136,12 +136,12 @@ describe Sinclair do
136
136
 
137
137
  context 'when readding it' do
138
138
  before do
139
- subject.add_method(:value, '@value ||= 0')
140
- subject.eval_and_add_method(:defined) do
139
+ builder.add_method(:value, '@value ||= 0')
140
+ builder.eval_and_add_method(:defined) do
141
141
  "@value = value + #{options_object.increment || 1}"
142
142
  end
143
- subject.add_method(:defined, '100')
144
- subject.build
143
+ builder.add_method(:defined, '100')
144
+ builder.build
145
145
  end
146
146
 
147
147
  it 'redefines it' do
data/spec/spec_helper.rb CHANGED
@@ -16,7 +16,6 @@ support_files = File.expand_path('spec/support/**/*.rb')
16
16
  Dir[support_files].each { |file| require file }
17
17
 
18
18
  RSpec.configure do |config|
19
- config.treat_symbols_as_metadata_keys_with_true_values = true
20
19
  config.run_all_when_everything_filtered = true
21
20
  config.filter_run :focus
22
21
  config.filter_run_excluding :integration unless ENV['ALL']
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DefaultValue
4
+ delegate :build, to: :builder
5
+ attr_reader :klass, :method, :value
6
+
7
+ def initialize(klass, method, value)
8
+ @klass = klass
9
+ @method = method
10
+ @value = value
11
+ end
12
+
13
+ private
14
+
15
+ def builder
16
+ @builder ||= Sinclair.new(klass).tap do |b|
17
+ b.add_method(method) { value }
18
+ end
19
+ end
20
+ end
@@ -1,20 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class DefaultValue
4
- delegate :build, to: :builder
5
- attr_reader :klass, :method, :value
6
-
7
- def initialize(klass, method, value)
8
- @klass = klass
9
- @method = method
10
- @value = value
3
+ class MyBuilder < Sinclair
4
+ def add_methods
5
+ if options_object.rescue_error
6
+ add_safe_method
7
+ else
8
+ add_method(:symbolize) { @variable.to_sym }
9
+ end
11
10
  end
12
11
 
13
- private
14
-
15
- def builder
16
- @builder ||= Sinclair.new(klass).tap do |b|
17
- b.add_method(method) { value }
12
+ def add_safe_method
13
+ add_method(:symbolize) do
14
+ begin
15
+ @variable.to_sym
16
+ rescue StandardError
17
+ :default
18
+ end
18
19
  end
19
20
  end
20
21
  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.1.3
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - DarthJee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-03-27 00:00:00.000000000 Z
11
+ date: 2019-03-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -180,13 +180,16 @@ files:
180
180
  - scripts/check_readme.sh
181
181
  - sinclair.gemspec
182
182
  - sinclair.jpg
183
- - spec/integration/matcher_spec.rb
184
- - spec/integration/readme/matcher_spec.rb
185
183
  - spec/integration/readme/my_class_spec.rb
186
- - spec/integration/readme_spec.rb
187
- - spec/integration/yard/matchers/add_method_spec.rb
188
- - spec/integration/yard/matchers/add_method_to_spec.rb
189
- - spec/integration/yard/options_parser_spec.rb
184
+ - spec/integration/readme/my_model_spec.rb
185
+ - spec/integration/readme/sinclair/matchers_spec.rb
186
+ - spec/integration/readme/sinclair_spec.rb
187
+ - spec/integration/sinclair/matchers_spec.rb
188
+ - spec/integration/yard/my_builder_spec.rb
189
+ - spec/integration/yard/sinclair/matchers/add_method_spec.rb
190
+ - spec/integration/yard/sinclair/matchers/add_method_to_spec.rb
191
+ - spec/integration/yard/sinclair/method_definition_spec.rb
192
+ - spec/integration/yard/sinclair/options_parser_spec.rb
190
193
  - spec/integration/yard/sinclair_spec.rb
191
194
  - spec/lib/sinclair/matchers/add_method_spec.rb
192
195
  - spec/lib/sinclair/matchers/add_method_to_spec.rb
@@ -196,6 +199,7 @@ files:
196
199
  - spec/lib/sinclair_spec.rb
197
200
  - spec/spec_helper.rb
198
201
  - spec/support/fixture_helpers.rb
202
+ - spec/support/models/default_value.rb
199
203
  - spec/support/models/dummy_builder.rb
200
204
  - spec/support/models/dummy_options_parser.rb
201
205
  - spec/support/models/initial_valuer.rb
@@ -1,27 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- RSpec.describe DefaultValue do
6
- let(:klass) { Class.new }
7
- let(:method) { :the_method }
8
- let(:value) { Random.rand(100) }
9
- let(:builder) { described_class.new(klass, method, value) }
10
- let(:instance) { klass.new }
11
-
12
- context 'when the builder runs' do
13
- it do
14
- expect do
15
- described_class.new(klass, method, value).build
16
- end.to add_method(method).to(instance)
17
- end
18
- end
19
-
20
- context 'when the builder runs' do
21
- it do
22
- expect do
23
- described_class.new(klass, method, value).build
24
- end.to add_method(method).to(klass)
25
- end
26
- end
27
- end
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe 'Stand Alone' do
6
- let(:instance) { klass.new }
7
- let(:klass) { Class.new }
8
- let(:builder) { Sinclair.new(klass) }
9
-
10
- before do
11
- builder.add_method(:twenty, '10 + 10')
12
- builder.add_method(:eighty) { 4 * twenty }
13
- builder.build
14
- end
15
-
16
- it 'knows how to add string defined methods' do
17
- expect("Twenty => #{instance.twenty}").to eq('Twenty => 20')
18
- end
19
-
20
- it 'knows how to add block defined methods' do
21
- expect("Eighty => #{instance.eighty}").to eq('Eighty => 80')
22
- end
23
- end