eapi 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c1be90f666a6468f4f4e574b85edcf706d7336a7
4
- data.tar.gz: c485bbe47dbd2ff849aad2b3c7dc4a9b8521cc0a
3
+ metadata.gz: 65b2149ec39bedde36ad3c6305a7eee7fb8b37b6
4
+ data.tar.gz: 6dc257fc779aeb132e289e7e870364e163104118
5
5
  SHA512:
6
- metadata.gz: da17b4c132b4f45b868bf64eb1fde65faafe04e34fea500694b372aa057f7c1c21076c222e45169c7f45bdcfe5b66fed65e21bcfec86d2578f45c3fca423951a
7
- data.tar.gz: 433e10ff4cd1bd992eef4a4779c003891d9c2f35d72e46ce4559ef83593c88b1e58731868519f538b13f63e10bf4a4f61b9e3d7b1e00f470144bdbab8ee9e46a
6
+ metadata.gz: 1e36790d62876f6cda906ed3f1cf4b1f29338b1acdbb70fadeac7718119ec4382f78655e7026b5cc289b9a8be5df560d467f912f817a34ec6bb58743322bd4ff
7
+ data.tar.gz: 440261fc694e382d0664f83f6ecf0f619e7f4922388adea99640b0a9ada69ce31b2f652fc8d376f96d5f59d480fb5a4dbb1ac216a4ec7e2a2bc50f9464dbe7e8
data/README.md CHANGED
@@ -336,6 +336,79 @@ x.one # => :fluent
336
336
  res.equal? x # => true
337
337
  ```
338
338
 
339
+ #### `converted_or_default_value_for` method
340
+
341
+ It will return the converted value for the property. If that value is to be ignored (following the rules described with the `ignore` option) then it will return the default one (defined by the `default` option), or `nil` if there is no default value.
342
+
343
+ ```ruby
344
+ class TestKlassWithDefault
345
+ include Eapi::Item
346
+
347
+ property :something, ignore: :blank?, default: 123
348
+ end
349
+
350
+ x = TestKlassWithDefault.new
351
+ x.something ''
352
+ x.converted_or_default_value_for(:something) # => 123
353
+
354
+ x.something 'not blank'
355
+ x.converted_or_default_value_for(:something) # => 'not blank'
356
+
357
+
358
+ class TestKlassWithoutDefault
359
+ include Eapi::Item
360
+
361
+ property :something, ignore: :blank?
362
+ end
363
+
364
+ x = TestKlassWithoutDefault.new
365
+ x.something ''
366
+ x.converted_or_default_value_for(:something) # => nil
367
+
368
+ x.something 'not blank'
369
+ x.converted_or_default_value_for(:something) # => 'not blank'
370
+ ```
371
+
372
+ #### `yield_final_value_for` method
373
+
374
+ It will yield the converted value for the property. If that value is to be ignored (following the rules described with the `ignore` option) then it will yield the default one (defined by the `default` option), or it won't yield anything if there is no default value.
375
+
376
+ ```ruby
377
+ class TestKlassWithDefault
378
+ include Eapi::Item
379
+
380
+ property :something, ignore: :blank?, default: 123
381
+ end
382
+
383
+ x = TestKlassWithDefault.new
384
+ x.something ''
385
+ check = :initial_check
386
+ x.yield_final_value_for(:something) { |v| check = v }
387
+ check # => 123
388
+
389
+ x.something 'not blank'
390
+ check = :initial_check
391
+ x.yield_final_value_for(:something) { |v| check = v }
392
+ check # => 'not blank'
393
+
394
+
395
+ class TestKlassWithoutDefault
396
+ include Eapi::Item
397
+
398
+ property :something, ignore: :blank?
399
+ end
400
+
401
+ x = TestKlassWithoutDefault.new
402
+ check = :initial_check
403
+ x.yield_final_value_for(:something) { |v| check = v }
404
+ check # => :initial_check
405
+
406
+ x.something 'not blank'
407
+ check = :initial_check
408
+ x.yield_final_value_for(:something) { |v| check = v }
409
+ check # => 'not blank'
410
+ ```
411
+
339
412
  ### Property definition
340
413
 
341
414
  When defining the property, we can specify some options to specify what values are expected in that property. This serves for validation and automatic initialisation.
@@ -379,6 +452,26 @@ eapi.errors.full_messages # => ["Something can't be blank"]
379
452
  eapi.errors.messages # => {something: ["can't be blank"]}
380
453
  ```
381
454
 
455
+ #### Establish a default value for the property in case of to-be-ignored value with `default` option
456
+
457
+ A property whose value is ignored (according with the `ignore` option), will use the value specifie in the `default` option.
458
+
459
+ example:
460
+
461
+ ```ruby
462
+ class TestKlass
463
+ include Eapi::Item
464
+
465
+ property :something, ignored: :blank?, default: 123
466
+ end
467
+
468
+ eapi = TestKlass.new
469
+
470
+ # setting a value that will be ignored (in this example, an empty string, that will respond trythy to `.blank?`)
471
+ eapi.something ""
472
+ eapi.render # => {something: 123}
473
+ ```
474
+
382
475
  #### Specify the property's Type with `type` option
383
476
 
384
477
  If a property is defined to be of a specific type, the value will be validated to meet that criteria. It means that the value must be of the specified type. It will use `value.kind_of?(type)` (if type represents an actual class), and if that fails it will use `value.is?(type)` if defined.
@@ -19,15 +19,9 @@ module Eapi
19
19
  def perform_render
20
20
  {}.tap do |hash|
21
21
  _properties.each do |prop|
22
- val = converted_value_for(prop)
23
- hash[prop] = val unless to_be_ignored?(val, prop)
22
+ set_value_in_final_hash(hash, prop)
24
23
  end
25
24
  end
26
25
  end
27
-
28
- private
29
- def to_be_ignored?(value, property)
30
- Eapi::ValueIgnoreChecker.to_be_ignored? value, self.class.ignore_definition(property)
31
- end
32
26
  end
33
27
  end
@@ -8,6 +8,16 @@ module Eapi
8
8
  self.class.properties
9
9
  end
10
10
 
11
+ def get(field)
12
+ getter = Eapi::Methods::Names.getter field
13
+ send(getter)
14
+ end
15
+
16
+ def set(field, value)
17
+ setter = Eapi::Methods::Names.fluent_setter field
18
+ send(setter, value)
19
+ end
20
+
11
21
  def converted_value_for(prop)
12
22
  convert_value get(prop)
13
23
  end
@@ -16,14 +26,33 @@ module Eapi
16
26
  Eapi::ValueConverter.convert_value(value)
17
27
  end
18
28
 
19
- def get(field)
20
- getter = Eapi::Methods::Names.getter field
21
- send(getter)
29
+ def converted_or_default_value_for(property)
30
+ yield_final_value_for(property) { |val| return val }
22
31
  end
23
32
 
24
- def set(field, value)
25
- setter = Eapi::Methods::Names.fluent_setter field
26
- send(setter, value)
33
+ # will yield the converted value if it is not to be ignored,
34
+ # will yield the default value if it is set and the converted value is to be ignored
35
+ # will not yield anything otherwise
36
+ def yield_final_value_for(property)
37
+ val = converted_value_for(property)
38
+ accepted = !to_be_ignored?(val, property)
39
+
40
+ if accepted
41
+ yield val
42
+ elsif self.class.default_value_for?(property)
43
+ yield self.class.default_value_for(property)
44
+ end
45
+ end
46
+
47
+ def to_be_ignored?(value, property)
48
+ Eapi::ValueIgnoreChecker.to_be_ignored? value, self.class.ignore_definition(property)
49
+ end
50
+
51
+ private
52
+ def set_value_in_final_hash(hash, property)
53
+ yield_final_value_for(property) do |val|
54
+ hash[property] = val
55
+ end
27
56
  end
28
57
  end
29
58
 
@@ -75,6 +104,14 @@ module Eapi
75
104
  definition_for(field).fetch(:ignore, :nil?)
76
105
  end
77
106
 
107
+ def default_value_for?(property)
108
+ definition_for(property).key? :default
109
+ end
110
+
111
+ def default_value_for(property)
112
+ definition_for(property).fetch(:default, nil)
113
+ end
114
+
78
115
  private :_property_allow_raw
79
116
  private :_property_definitions
80
117
  private :run_property_definition
@@ -1,3 +1,3 @@
1
1
  module Eapi
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -0,0 +1,81 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Eapi do
4
+
5
+ class IgnoreDefaultSpecTestItem
6
+ include Eapi::Item
7
+ property :something, ignore: :blank?, default: 123
8
+ end
9
+
10
+ class IgnoreDefaultSpecTestItemWithoutDefault
11
+ include Eapi::Item
12
+ property :something, ignore: :blank?
13
+ end
14
+
15
+
16
+ describe 'on render: ignored values with default option' do
17
+ describe 'Item' do
18
+ subject { IgnoreDefaultSpecTestItem.new }
19
+
20
+ it 'use the default if value is to be ignored' do
21
+ subject.something ""
22
+ expected = {something: 123}
23
+ expect(subject.render).to eq expected
24
+ end
25
+
26
+ it 'the default value is not used if the actual value is not to be ignored' do
27
+ subject.something 1
28
+ expected = {something: 1}
29
+ expect(subject.render).to eq expected
30
+ end
31
+
32
+ describe '#converted_or_default_value_for' do
33
+ it 'returns the converted value if it is not ignored' do
34
+ subject.something 1
35
+ expect(subject.converted_or_default_value_for :something).to eq 1
36
+ end
37
+
38
+ it 'returns the default value if it is ignored' do
39
+ subject.something ""
40
+ expect(subject.converted_or_default_value_for :something).to eq 123
41
+ end
42
+
43
+ describe 'without default value' do
44
+ subject { IgnoreDefaultSpecTestItemWithoutDefault.new }
45
+
46
+ it 'returns nil if the converted value is ignored' do
47
+ subject.something ""
48
+ expect(subject.converted_or_default_value_for :something).to be_nil
49
+ end
50
+ end
51
+ end
52
+
53
+ describe '#yield_final_value_for' do
54
+ it 'yields the converted value if it is not ignored' do
55
+ subject.something 1
56
+ init = :initial_value
57
+ subject.yield_final_value_for(:something) { |v| init = v }
58
+ expect(init).to eq 1
59
+ end
60
+
61
+ it 'yields the default value if it is ignored' do
62
+ subject.something ""
63
+ init = :initial_value
64
+ subject.yield_final_value_for(:something) { |v| init = v }
65
+ expect(init).to eq 123
66
+ end
67
+
68
+ describe 'without default value' do
69
+ subject { IgnoreDefaultSpecTestItemWithoutDefault.new }
70
+
71
+ it 'does not yield anything if the converted value is ignored' do
72
+ subject.something ""
73
+ init = :initial_value
74
+ subject.yield_final_value_for(:something) { |v| init = v }
75
+ expect(init).to eq :initial_value
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eduardo Turiño
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-22 00:00:00.000000000 Z
11
+ date: 2014-07-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -217,6 +217,7 @@ files:
217
217
  - spec/definition_spec.rb
218
218
  - spec/extension_spec.rb
219
219
  - spec/function_spec.rb
220
+ - spec/ignore_default_spec.rb
220
221
  - spec/ignore_spec.rb
221
222
  - spec/list_elements_spec.rb
222
223
  - spec/list_spec.rb
@@ -254,6 +255,7 @@ test_files:
254
255
  - spec/definition_spec.rb
255
256
  - spec/extension_spec.rb
256
257
  - spec/function_spec.rb
258
+ - spec/ignore_default_spec.rb
257
259
  - spec/ignore_spec.rb
258
260
  - spec/list_elements_spec.rb
259
261
  - spec/list_spec.rb