eapi 0.6.0 → 0.7.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4673ef60bc4acd96d0ae165935fab2e1b901cf53
4
- data.tar.gz: d04635f645f8638fdb8a224dc5ae3320e5fe90fd
3
+ metadata.gz: 8185b8ffb82603d48217ba5da50433b6ed456692
4
+ data.tar.gz: ec4580aa551539d3dd3954af9370831fa81f8279
5
5
  SHA512:
6
- metadata.gz: 4da861d047452f5057a2cc1f778ea2073198afab91a926fd7cdc1c36e35222ca6f7e02aa547b5683099d21020d2d1171825562a66a94c47663316f2fcf65240d
7
- data.tar.gz: 8d9f6a3d3d5a8e18a07cf03f22b40724acd98a9a736985bf01093e2204f77a633b7d79c3a2603c4ab1ce1e5378dd32d4be47d3d9c82333aed375f4ff86971203
6
+ metadata.gz: 4e8c053064f3c3ea658a71791ade20c42b1f2b66e6e3ffbe38e8f1bc6d52a33eb191e1d8284b4e050a965f27ff87ad727794ee79797933d56d2bd8c2d82e05e4
7
+ data.tar.gz: d71bf06c7941f1218c9fa7486b9d7aa3ccae398e4fe4a09f235abe405477267cf4daa5ef8833f49c2c54ef7a246c067fe292fcb306fcf0b0ab8fbac7c56072db
data/README.md CHANGED
@@ -135,16 +135,16 @@ x = ExampleItem.new.add(1).add(2)
135
135
  x.render # => ["1", "2"]
136
136
  ```
137
137
 
138
- #### Convert values before validation using `convert_before_validation` option
138
+ #### Prepare values before validation using `prepare_with` option
139
139
 
140
- If `convert_before_validation` option is enabled, then the conversion will occur before validation
140
+ Same as `convert_with` but it is used before validation.
141
141
 
142
142
  ```ruby
143
143
  class ExampleItem
144
144
  include Eapi::Item
145
145
 
146
- property :something, convert_with: :to_i, convert_before_validation: true
147
- property :normal, convert_with: :to_i
146
+ property :something, prepare_with: to_i
147
+ property :normal,
148
148
 
149
149
  validates :something, inclusion: { in: [1, 2, 3] }
150
150
  validates :normal, inclusion: { in: [1, 2, 3] }
@@ -165,7 +165,7 @@ In `List` it will work on elements.
165
165
  class ExampleListConvertBeforeValidationEnabled
166
166
  include Eapi::List
167
167
 
168
- elements convert_with: :to_i, convert_before_validation: true, validate_with: ->(record, attr, value) do
168
+ elements prepare_with: :to_i, validate_with: ->(record, attr, value) do
169
169
  record.errors.add(attr, 'must pass my custom validation') unless value.kind_of?(Fixnum)
170
170
  end
171
171
  end
@@ -173,7 +173,7 @@ end
173
173
  class ExampleListConvertBeforeValidationDisabled
174
174
  include Eapi::List
175
175
 
176
- elements convert_with: :to_i, validate_with: ->(record, attr, value) do
176
+ elements validate_with: ->(record, attr, value) do
177
177
  record.errors.add(attr, 'must pass my custom validation') unless value.kind_of?(Fixnum)
178
178
  end
179
179
  end
@@ -27,8 +27,8 @@ module Eapi
27
27
 
28
28
  def perform_before_validation
29
29
  _properties.each do |property|
30
- if self.class.convert_before_validation?(property)
31
- self.set(property, converted_value_for(property))
30
+ if self.class.prepare_value_for?(property)
31
+ self.set(property, prepare_value_for(property))
32
32
  end
33
33
  end
34
34
  end
@@ -80,8 +80,8 @@ module Eapi
80
80
  end
81
81
 
82
82
  def perform_before_validation
83
- if self.class.elements_convert_before_validation?
84
- _list.map! { |v| convert_value_for_element(v) }
83
+ if self.class.prepare_value_for_elements?
84
+ _list.map! { |v| prepare_value_for_element(v) }
85
85
  end
86
86
  end
87
87
 
@@ -18,6 +18,17 @@ module Eapi
18
18
  send(setter, value)
19
19
  end
20
20
 
21
+ def prepare_value_for(prop)
22
+ value = get(prop)
23
+ prepare_with = self.class.defined_prepare_with_for(prop)
24
+
25
+ if prepare_with
26
+ convert_value(value, prepare_with)
27
+ else
28
+ value
29
+ end
30
+ end
31
+
21
32
  def converted_value_for(prop)
22
33
  convert_value get(prop), self.class.defined_convert_with_for(prop)
23
34
  end
@@ -116,8 +127,12 @@ module Eapi
116
127
  definition_for(property).fetch(:convert_with, nil)
117
128
  end
118
129
 
119
- def convert_before_validation?(property)
120
- definition_for(property).fetch(:convert_before_validation, false)
130
+ def defined_prepare_with_for(property)
131
+ definition_for(property).fetch(:prepare_with, nil)
132
+ end
133
+
134
+ def prepare_value_for?(property)
135
+ !!defined_prepare_with_for(property)
121
136
  end
122
137
 
123
138
  private :_property_allow_raw
@@ -144,6 +159,16 @@ module Eapi
144
159
  def convert_value_for_element(value)
145
160
  convert_value(value, self.class.elements_defined_convert_with_for)
146
161
  end
162
+
163
+ def prepare_value_for_element(value)
164
+ prepare_with = self.class.elements_defined_prepare_with_for
165
+
166
+ if prepare_with
167
+ convert_value(value, prepare_with)
168
+ else
169
+ value
170
+ end
171
+ end
147
172
  end
148
173
 
149
174
  module ListCLassMethods
@@ -163,19 +188,23 @@ module Eapi
163
188
  definition_for_elements.fetch(:ignore, :nil?)
164
189
  end
165
190
 
166
- def elements_convert_before_validation?
167
- definition_for_elements.fetch(:convert_before_validation, false)
191
+ def prepare_value_for_elements?
192
+ !!elements_defined_prepare_with_for
168
193
  end
169
194
 
170
- def elements(definition)
171
- run_list_definition definition
172
- store_list_definition definition
195
+ def elements_defined_prepare_with_for
196
+ definition_for_elements.fetch(:prepare_with, nil)
173
197
  end
174
198
 
175
199
  def elements_defined_convert_with_for
176
200
  definition_for_elements.fetch(:convert_with, nil)
177
201
  end
178
202
 
203
+ def elements(definition)
204
+ run_list_definition definition
205
+ store_list_definition definition
206
+ end
207
+
179
208
  def definition_for_elements
180
209
  @_list_definition ||= {}
181
210
  end
@@ -1,3 +1,3 @@
1
1
  module Eapi
2
- VERSION = "0.6.0"
2
+ VERSION = '0.7.0'
3
3
  end
@@ -60,8 +60,8 @@ RSpec.describe Eapi do
60
60
  class ExampleItemConvertBeforeValidation
61
61
  include Eapi::Item
62
62
 
63
- property :something, convert_with: :to_i, convert_before_validation: true
64
- property :normal, convert_with: :to_i
63
+ property :something, prepare_with: :to_i
64
+ property :normal
65
65
 
66
66
  validates :something, inclusion: {in: [1, 2, 3]}
67
67
  validates :normal, inclusion: {in: [1, 2, 3]}
@@ -86,7 +86,7 @@ RSpec.describe Eapi do
86
86
  class ExampleListConvertBeforeValidationEnabled
87
87
  include Eapi::List
88
88
 
89
- elements convert_with: :to_i, convert_before_validation: true, validate_with: ->(record, attr, value) do
89
+ elements prepare_with: :to_i, validate_with: ->(record, attr, value) do
90
90
  record.errors.add(attr, 'must pass my custom validation') unless value.kind_of?(Fixnum)
91
91
  end
92
92
  end
@@ -457,5 +457,32 @@ RSpec.describe Eapi do
457
457
  end
458
458
 
459
459
  end
460
+
461
+ describe 'render' do
462
+ class TestListItemRender
463
+ include Eapi::Item
464
+
465
+ property :something
466
+ property :other
467
+
468
+ def perform_render
469
+ {something => other}
470
+ end
471
+ end
472
+
473
+ class TestListRender
474
+ include Eapi::List
475
+
476
+ elements type: 'TestListItemRender'
477
+ end
478
+
479
+ let(:item) { TestListItemRender.new something: :f, other: :o }
480
+ subject { TestListRender.new.add(item) }
481
+ let(:expected) { [{f: :o}] }
482
+
483
+ it 'renders to an array, rendering each element' do
484
+ expect(subject.render).to eq expected
485
+ end
486
+ end
460
487
  end
461
488
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eduardo Turiño