artirix_data_models 0.11.2 → 0.12.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: 74bf1987943e0d0d547849475fa2c49e6680ba55
4
- data.tar.gz: b5cf434cc9e5a3f69da2c20b9f103140a76ac215
3
+ metadata.gz: e693f7a79fd0435a53c7315a66a0a87fddc00756
4
+ data.tar.gz: 1e2209be29d0228f38187d74079570b5d8326af6
5
5
  SHA512:
6
- metadata.gz: 8683e1313088cade6522eb60185217e42a2ea614abc490b6e39c541ca04b090a472510698b285dbfb2079a6326ee71f40bef43a048f9f00fb1a0c33367f9f1c1
7
- data.tar.gz: c2bf55255fb00cf74ba913aa25e23534809d07703c551fe40d7ae56187715575c51a3037932b28f68409571b17b2bdf2a15526ad4fcf28d690062c6b5d392fe7
6
+ metadata.gz: c51a77d2635bdbf0a2c32fb1a89caa6c63dc31d5e6980d388092287f7f31a5682b60c5f7e55636bfc8eee40c69372b7e7afb12a257c8a855bfe2827003149486
7
+ data.tar.gz: 29814e119cfe07b7b26390d7bf1132b0ed9f6b749d035303816b1c58b9b14aecf0a00226bdf06da2a40a249aa90af5598d346020327df1f1cde00e4c412d7f61
data/README.md CHANGED
@@ -52,6 +52,24 @@ end
52
52
 
53
53
  TODO:
54
54
 
55
+ ```ruby
56
+
57
+ class MyModel
58
+ include ArtirixDataModels::Model::OnlyData
59
+
60
+ attribute :id, :name
61
+
62
+ attribute :public_title, writer_visibility: :public
63
+ attribute :private_title, reader_visibility: :private
64
+
65
+ attribute :remember_me, :and_me, skip: :predicate
66
+ attribute :remember_me2, :and_me2, skip: :presence
67
+
68
+ end
69
+
70
+
71
+ ```
72
+
55
73
  ### DAO
56
74
 
57
75
  TODO:
@@ -250,6 +268,9 @@ end
250
268
 
251
269
  ## Changes
252
270
 
271
+ ### 0.12.0
272
+ - `attribute` call now can accept a hash of options as the last argument. This options include: `skip` (what to skip), `writer_visibility` and `reader_visibility`.
273
+
253
274
  ### 0.11.2
254
275
  - `ArtirixDataModels::ActiveNull` better acting like a model.
255
276
 
@@ -151,7 +151,8 @@ module ArtirixDataModels
151
151
 
152
152
  module ClassMethods
153
153
  def attribute(*attributes)
154
- attributes.each { |attribute| _define_attribute attribute }
154
+ options = attributes.extract_options!
155
+ attributes.each { |attribute| _define_attribute attribute, options }
155
156
  end
156
157
 
157
158
  def attribute_config
@@ -181,44 +182,56 @@ module ArtirixDataModels
181
182
  end
182
183
 
183
184
  private
184
- def _define_attribute(attribute)
185
+ def _define_attribute(attribute, options)
185
186
  at = attribute.to_sym
186
- _define_getter(at)
187
- _define_presence(at)
188
- _define_writer(at)
187
+ _define_getter(at, options)
188
+ _define_presence(at, options)
189
+ _define_writer(at, options)
189
190
 
190
191
  attribute_config.add_attribute at
191
192
  end
192
193
 
193
- def _define_writer(attribute)
194
+ def _define_writer(attribute, options)
195
+ skip_option = Array(options.fetch(:skip, []))
196
+ return nil if skip_option.include?(:writer) || skip_option.include?(:setter)
197
+
198
+ vis = options.fetch(:writer_visibility, writer_visibility)
199
+
194
200
  attr_writer attribute
195
201
  writer = "#{attribute}="
196
202
 
197
- if writer_visibility == :private
203
+ if vis == :private
198
204
  private writer
199
- elsif writer_visibility == :protected
205
+ elsif vis == :protected
200
206
  protected writer
201
207
  end
202
208
 
203
209
  writer
204
210
  end
205
211
 
206
- def _define_presence(attribute)
212
+ def _define_presence(attribute, options)
213
+ skip_option = Array(options.fetch(:skip, []))
214
+ return nil if skip_option.include?(:presence) || skip_option.include?(:predicate)
215
+
207
216
  presence_method = "#{attribute}?"
208
217
  define_method presence_method do
209
218
  send(attribute).present?
210
219
  end
211
220
  end
212
221
 
213
- def _define_getter(attribute)
222
+ def _define_getter(attribute, options)
223
+ skip_option = Array(options.fetch(:skip, []))
224
+ return nil if skip_option.include?(:reader) || skip_option.include?(:getter)
225
+
214
226
  variable_name = "@#{attribute}"
215
227
  dir_get_name = Attributes.direct_getter_method_name(attribute)
228
+ reader = attribute.to_s
216
229
 
217
230
  define_method dir_get_name do
218
231
  instance_variable_get variable_name
219
232
  end
220
233
 
221
- define_method(attribute) do
234
+ define_method(reader) do
222
235
  val = send dir_get_name
223
236
  if val.nil?
224
237
  nil_attribute(attribute)
@@ -226,6 +239,15 @@ module ArtirixDataModels
226
239
  val
227
240
  end
228
241
  end
242
+
243
+ vis = options.fetch(:reader_visibility, :public)
244
+
245
+ if vis == :private
246
+ private reader
247
+ elsif vis == :protected
248
+ protected reader
249
+ end
250
+
229
251
  end
230
252
  end
231
253
 
@@ -1,3 +1,3 @@
1
1
  module ArtirixDataModels
2
- VERSION = '0.11.2'
2
+ VERSION = '0.12.0'
3
3
  end
@@ -0,0 +1,124 @@
1
+ require 'spec_helper'
2
+
3
+ class MyModel
4
+ include ArtirixDataModels::Model::OnlyData
5
+
6
+ attribute :id, :name
7
+
8
+ attribute :public_title, writer_visibility: :public
9
+ attribute :private_title, reader_visibility: :private
10
+
11
+ attribute :remember_me, :and_me, skip: :predicate
12
+ attribute :remember_me2, :and_me2, skip: :presence
13
+
14
+ end
15
+
16
+ RSpec.describe MyModel, type: :model do
17
+
18
+ subject { described_class.new data }
19
+
20
+ let(:data) do
21
+ {
22
+ name: name,
23
+ remember_me: remember_me,
24
+ and_me: and_me,
25
+ remember_me2: remember_me2,
26
+ and_me2: and_me2,
27
+ }
28
+ end
29
+
30
+ let(:name) { 'Paco' }
31
+ let(:remember_me) { 'yeah' }
32
+ let(:and_me) { 'and' }
33
+ let(:remember_me2) { 'other' }
34
+ let(:and_me2) { 'stuff' }
35
+
36
+ describe '.attribute' do
37
+ it 'creates public getter' do
38
+ expect(subject).to respond_to :id
39
+ expect(subject).to respond_to :name
40
+ end
41
+
42
+ it 'loads info from hash passed in object creation' do
43
+ expect(subject.id).to be_nil
44
+ expect(subject.name).to eq name
45
+ end
46
+
47
+ it 'creates private setter' do
48
+ expect(subject.respond_to? :name=).to be_falsey
49
+ expect(subject.respond_to? :name=, true).to be_truthy
50
+
51
+ expect(subject.name).to eq name
52
+ subject.send :name=, 123
53
+ expect(subject.name).to eq 123
54
+ end
55
+
56
+ it 'creates predicate method' do
57
+ expect(subject).to respond_to :id?
58
+ expect(subject).to respond_to :name?
59
+
60
+ expect(subject.id?).to be_falsey
61
+ expect(subject.name?).to be_truthy
62
+ end
63
+
64
+ it 'creates private setter' do
65
+ expect(subject.respond_to? :name=).to be_falsey
66
+ expect(subject.respond_to? :name=, true).to be_truthy
67
+
68
+ expect(subject.name).to eq name
69
+ subject.send :name=, 123
70
+ expect(subject.name).to eq 123
71
+ end
72
+
73
+ context 'with option `writer_visibility: :public`' do
74
+ it 'writer is public' do
75
+ expect(subject.respond_to? :public_title=).to be_truthy
76
+ expect(subject.respond_to? :public_title=, true).to be_truthy
77
+
78
+ expect(subject.public_title).to be_nil
79
+ subject.public_title = 411
80
+ expect(subject.public_title).to eq 411
81
+ end
82
+ end
83
+
84
+ context 'with option `reader_visibility: :private`' do
85
+ it 'reader is private' do
86
+ expect(subject.respond_to? :private_title).to be_falsey
87
+ expect(subject.respond_to? :private_title, true).to be_truthy
88
+
89
+ expect(subject.send :private_title).to be_nil
90
+ subject.send :private_title=, 411
91
+ expect(subject.send :private_title).to eq 411
92
+ end
93
+ end
94
+
95
+ context 'with option `skip: :predicate` or `skip: :presence`' do
96
+
97
+ it 'does not create predicate method' do
98
+ expect(subject).to respond_to :remember_me
99
+ expect(subject).to respond_to :and_me
100
+ expect(subject).to respond_to :remember_me2
101
+ expect(subject).to respond_to :and_me2
102
+
103
+ #not public
104
+ expect(subject.respond_to? :remember_me?).to be_falsey
105
+ expect(subject.respond_to? :and_me?).to be_falsey
106
+ expect(subject.respond_to? :remember_me2?).to be_falsey
107
+ expect(subject.respond_to? :and_me2?).to be_falsey
108
+
109
+ #not private either
110
+ expect(subject.respond_to? :remember_me?, true).to be_falsey
111
+ expect(subject.respond_to? :and_me?, true).to be_falsey
112
+ expect(subject.respond_to? :remember_me2?, true).to be_falsey
113
+ expect(subject.respond_to? :and_me2?, true).to be_falsey
114
+
115
+ expect(subject.remember_me).to eq remember_me
116
+ expect(subject.and_me).to eq and_me
117
+ expect(subject.remember_me2).to eq remember_me2
118
+ expect(subject.and_me2).to eq and_me2
119
+ end
120
+
121
+ end
122
+ end
123
+
124
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: artirix_data_models
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.2
4
+ version: 0.12.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: 2016-02-22 00:00:00.000000000 Z
11
+ date: 2016-02-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -274,6 +274,7 @@ files:
274
274
  - spec/artirix_data_models/gateways/data_gateway_spec.rb
275
275
  - spec/artirix_data_models/gateways/gateway_response_adaptors/model_adaptor_spec.rb
276
276
  - spec/artirix_data_models/model_fields_dao_spec.rb
277
+ - spec/artirix_data_models/model_spec.rb
277
278
  - spec/fixtures/articles_search_dl.json
278
279
  - spec/fixtures/articles_search_multiple_nested_raw_es.json
279
280
  - spec/fixtures/articles_search_nested_raw_es.json
@@ -315,6 +316,7 @@ test_files:
315
316
  - spec/artirix_data_models/gateways/data_gateway_spec.rb
316
317
  - spec/artirix_data_models/gateways/gateway_response_adaptors/model_adaptor_spec.rb
317
318
  - spec/artirix_data_models/model_fields_dao_spec.rb
319
+ - spec/artirix_data_models/model_spec.rb
318
320
  - spec/fixtures/articles_search_dl.json
319
321
  - spec/fixtures/articles_search_multiple_nested_raw_es.json
320
322
  - spec/fixtures/articles_search_nested_raw_es.json