riveter 0.3.0 → 0.6.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: ac7829381fcfecbda57c395c23653edc8df876b3
4
- data.tar.gz: 57005498bcf3833e4b487f8d1e174cfd6adb5b77
3
+ metadata.gz: 0af369c290c92292bc05e43a0cbef0e2c351b472
4
+ data.tar.gz: 4f24145bf053b9dc42dff0b244a4fd98f082adbb
5
5
  SHA512:
6
- metadata.gz: 3cda2c9c10fa00fef707ed7d8bff72b40684ed8f3f088fb8fecb6879988d30a4ce02d0b57056da324bb8c1a77ac5a3fe9ba822bfa9082ad75ee767d00bbade38
7
- data.tar.gz: e0853c278c745da742bf13bd9e6d6b2a1319f0297b09b0a9864eccf77b7b397eaa979025f7848ddd683fbaf335f2ab4bbef621c63003747385a72f21722ddcd7
6
+ metadata.gz: 9f42fa9443a5faf0ca77e413b4e60ec6d1e1f912d8c20cf77d42691656da06575e36d7f70fa53cd22b9557b6d216f800f22ebea13662af4dafb0e92211ade58a
7
+ data.tar.gz: 1e2746430be6705fbe977132e98266bd1378bc9785bb70fa1ccbaadeaaee41cbed2f749ada5a7e7765cb6f9446d6d85532404845522d9a6f426f97a6dad2526c
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- riveter (0.3.0)
4
+ riveter (0.6.0)
5
5
  activemodel (>= 4.0.0)
6
6
  railties (>= 4.0.0)
7
7
  validates_timeliness (>= 3.0.0)
@@ -9,65 +9,64 @@ PATH
9
9
  GEM
10
10
  remote: https://rubygems.org/
11
11
  specs:
12
- actionmailer (4.2.1)
13
- actionpack (= 4.2.1)
14
- actionview (= 4.2.1)
15
- activejob (= 4.2.1)
12
+ actionmailer (4.2.7.1)
13
+ actionpack (= 4.2.7.1)
14
+ actionview (= 4.2.7.1)
15
+ activejob (= 4.2.7.1)
16
16
  mail (~> 2.5, >= 2.5.4)
17
17
  rails-dom-testing (~> 1.0, >= 1.0.5)
18
- actionpack (4.2.1)
19
- actionview (= 4.2.1)
20
- activesupport (= 4.2.1)
18
+ actionpack (4.2.7.1)
19
+ actionview (= 4.2.7.1)
20
+ activesupport (= 4.2.7.1)
21
21
  rack (~> 1.6)
22
22
  rack-test (~> 0.6.2)
23
23
  rails-dom-testing (~> 1.0, >= 1.0.5)
24
- rails-html-sanitizer (~> 1.0, >= 1.0.1)
25
- actionview (4.2.1)
26
- activesupport (= 4.2.1)
24
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
25
+ actionview (4.2.7.1)
26
+ activesupport (= 4.2.7.1)
27
27
  builder (~> 3.1)
28
28
  erubis (~> 2.7.0)
29
29
  rails-dom-testing (~> 1.0, >= 1.0.5)
30
- rails-html-sanitizer (~> 1.0, >= 1.0.1)
31
- activejob (4.2.1)
32
- activesupport (= 4.2.1)
30
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
31
+ activejob (4.2.7.1)
32
+ activesupport (= 4.2.7.1)
33
33
  globalid (>= 0.3.0)
34
- activemodel (4.2.1)
35
- activesupport (= 4.2.1)
34
+ activemodel (4.2.7.1)
35
+ activesupport (= 4.2.7.1)
36
36
  builder (~> 3.1)
37
- activerecord (4.2.1)
38
- activemodel (= 4.2.1)
39
- activesupport (= 4.2.1)
37
+ activerecord (4.2.7.1)
38
+ activemodel (= 4.2.7.1)
39
+ activesupport (= 4.2.7.1)
40
40
  arel (~> 6.0)
41
- activesupport (4.2.1)
41
+ activesupport (4.2.7.1)
42
42
  i18n (~> 0.7)
43
43
  json (~> 1.7, >= 1.7.7)
44
44
  minitest (~> 5.1)
45
45
  thread_safe (~> 0.3, >= 0.3.4)
46
46
  tzinfo (~> 1.1)
47
- ammeter (1.1.2)
47
+ ammeter (1.1.3)
48
48
  activesupport (>= 3.0)
49
49
  railties (>= 3.0)
50
50
  rspec-rails (>= 2.2)
51
- arel (6.0.0)
51
+ arel (6.0.3)
52
52
  builder (3.2.2)
53
53
  byebug (4.0.5)
54
54
  columnize (= 0.9.0)
55
- coderay (1.1.0)
55
+ coderay (1.1.1)
56
56
  columnize (0.9.0)
57
- coveralls (0.8.1)
58
- json (~> 1.8)
59
- rest-client (>= 1.6.8, < 2)
60
- simplecov (~> 0.10.0)
57
+ concurrent-ruby (1.0.2)
58
+ coveralls (0.8.15)
59
+ json (>= 1.8, < 3)
60
+ simplecov (~> 0.12.0)
61
61
  term-ansicolor (~> 1.3)
62
62
  thor (~> 0.19.1)
63
+ tins (>= 1.6.0, < 2)
63
64
  diff-lcs (1.2.5)
64
65
  docile (1.1.5)
65
- domain_name (0.5.24)
66
- unf (>= 0.0.5, < 1.0.0)
67
66
  erubis (2.7.0)
68
- globalid (0.3.5)
67
+ globalid (0.3.7)
69
68
  activesupport (>= 4.1.0)
70
- haml (4.0.6)
69
+ haml (4.0.7)
71
70
  tilt
72
71
  haml-rails (0.9.0)
73
72
  actionpack (>= 4.0.1)
@@ -80,60 +79,57 @@ GEM
80
79
  haml (~> 4.0.0)
81
80
  nokogiri (~> 1.6.0)
82
81
  ruby_parser (~> 3.5)
83
- http-cookie (1.0.2)
84
- domain_name (~> 0.5)
85
82
  i18n (0.7.0)
86
- json (1.8.2)
87
- loofah (2.0.2)
83
+ json (1.8.3)
84
+ loofah (2.0.3)
88
85
  nokogiri (>= 1.5.9)
89
- mail (2.6.3)
90
- mime-types (>= 1.16, < 3)
86
+ mail (2.6.4)
87
+ mime-types (>= 1.16, < 4)
91
88
  method_source (0.8.2)
92
- mime-types (2.6.1)
93
- mini_portile (0.6.2)
94
- minitest (5.7.0)
95
- netrc (0.10.3)
96
- nokogiri (1.6.6.2)
97
- mini_portile (~> 0.6.0)
98
- pry (0.10.1)
89
+ mime-types (3.1)
90
+ mime-types-data (~> 3.2015)
91
+ mime-types-data (3.2016.0521)
92
+ mini_portile2 (2.1.0)
93
+ minitest (5.9.0)
94
+ nokogiri (1.6.8)
95
+ mini_portile2 (~> 2.1.0)
96
+ pkg-config (~> 1.1.7)
97
+ pkg-config (1.1.7)
98
+ pry (0.10.4)
99
99
  coderay (~> 1.1.0)
100
100
  method_source (~> 0.8.1)
101
101
  slop (~> 3.4)
102
102
  pry-byebug (3.1.0)
103
103
  byebug (~> 4.0)
104
104
  pry (~> 0.10)
105
- rack (1.6.1)
105
+ rack (1.6.4)
106
106
  rack-test (0.6.3)
107
107
  rack (>= 1.0)
108
- rails (4.2.1)
109
- actionmailer (= 4.2.1)
110
- actionpack (= 4.2.1)
111
- actionview (= 4.2.1)
112
- activejob (= 4.2.1)
113
- activemodel (= 4.2.1)
114
- activerecord (= 4.2.1)
115
- activesupport (= 4.2.1)
108
+ rails (4.2.7.1)
109
+ actionmailer (= 4.2.7.1)
110
+ actionpack (= 4.2.7.1)
111
+ actionview (= 4.2.7.1)
112
+ activejob (= 4.2.7.1)
113
+ activemodel (= 4.2.7.1)
114
+ activerecord (= 4.2.7.1)
115
+ activesupport (= 4.2.7.1)
116
116
  bundler (>= 1.3.0, < 2.0)
117
- railties (= 4.2.1)
117
+ railties (= 4.2.7.1)
118
118
  sprockets-rails
119
119
  rails-deprecated_sanitizer (1.0.3)
120
120
  activesupport (>= 4.2.0.alpha)
121
- rails-dom-testing (1.0.6)
121
+ rails-dom-testing (1.0.7)
122
122
  activesupport (>= 4.2.0.beta, < 5.0)
123
123
  nokogiri (~> 1.6.0)
124
124
  rails-deprecated_sanitizer (>= 1.0.1)
125
- rails-html-sanitizer (1.0.2)
125
+ rails-html-sanitizer (1.0.3)
126
126
  loofah (~> 2.0)
127
- railties (4.2.1)
128
- actionpack (= 4.2.1)
129
- activesupport (= 4.2.1)
127
+ railties (4.2.7.1)
128
+ actionpack (= 4.2.7.1)
129
+ activesupport (= 4.2.7.1)
130
130
  rake (>= 0.8.7)
131
131
  thor (>= 0.18.1, < 2.0)
132
132
  rake (10.4.2)
133
- rest-client (1.8.0)
134
- http-cookie (>= 1.0.2, < 2.0)
135
- mime-types (>= 1.16, < 3.0)
136
- netrc (~> 0.7)
137
133
  rspec-core (3.2.3)
138
134
  rspec-support (~> 3.2.0)
139
135
  rspec-expectations (3.2.1)
@@ -142,7 +138,7 @@ GEM
142
138
  rspec-mocks (3.2.1)
143
139
  diff-lcs (>= 1.2.0, < 2.0)
144
140
  rspec-support (~> 3.2.0)
145
- rspec-rails (3.2.1)
141
+ rspec-rails (3.2.3)
146
142
  actionpack (>= 3.0, < 4.3)
147
143
  activesupport (>= 3.0, < 4.3)
148
144
  railties (>= 3.0, < 4.3)
@@ -151,35 +147,33 @@ GEM
151
147
  rspec-mocks (~> 3.2.0)
152
148
  rspec-support (~> 3.2.0)
153
149
  rspec-support (3.2.2)
154
- ruby_parser (3.7.0)
150
+ ruby_parser (3.8.2)
155
151
  sexp_processor (~> 4.1)
156
- sexp_processor (4.6.0)
152
+ sexp_processor (4.7.0)
157
153
  shoulda-matchers (2.8.0)
158
154
  activesupport (>= 3.0.0)
159
- simplecov (0.10.0)
155
+ simplecov (0.12.0)
160
156
  docile (~> 1.1.0)
161
- json (~> 1.8)
157
+ json (>= 1.8, < 3)
162
158
  simplecov-html (~> 0.10.0)
163
159
  simplecov-html (0.10.0)
164
160
  slop (3.6.0)
165
- sprockets (3.1.0)
166
- rack (~> 1.0)
167
- sprockets-rails (2.3.1)
168
- actionpack (>= 3.0)
169
- activesupport (>= 3.0)
170
- sprockets (>= 2.8, < 4.0)
171
- term-ansicolor (1.3.0)
161
+ sprockets (3.7.0)
162
+ concurrent-ruby (~> 1.0)
163
+ rack (> 1, < 3)
164
+ sprockets-rails (3.2.0)
165
+ actionpack (>= 4.0)
166
+ activesupport (>= 4.0)
167
+ sprockets (>= 3.0.0)
168
+ term-ansicolor (1.3.2)
172
169
  tins (~> 1.0)
173
170
  thor (0.19.1)
174
171
  thread_safe (0.3.5)
175
- tilt (2.0.1)
172
+ tilt (2.0.5)
176
173
  timeliness (0.3.8)
177
- tins (1.5.2)
174
+ tins (1.12.0)
178
175
  tzinfo (1.2.2)
179
176
  thread_safe (~> 0.1)
180
- unf (0.1.4)
181
- unf_ext
182
- unf_ext (0.0.7.1)
183
177
  validates_timeliness (4.0.2)
184
178
  timeliness (~> 0.3.7)
185
179
 
@@ -26,9 +26,13 @@ module Riveter
26
26
 
27
27
  module ClassMethods
28
28
  def attr_string(name, options={}, &block)
29
+ options = {
30
+ :validate => true
31
+ }.merge(options)
32
+
29
33
  converter = block_given? ? block : Converters.converter_for(:string)
30
34
 
31
- attr_reader_with_converter name, converter
35
+ attr_reader_with_converter name, converter, options
32
36
  attr_writer name
33
37
 
34
38
  add_attr(name, :string, converter, options)
@@ -65,8 +69,15 @@ module Riveter
65
69
  limit_value = options.delete(limit)
66
70
 
67
71
  define_method :"#{name}_#{limit}" do
68
- limit_value.respond_to?(:call) ? limit_value.call : limit_value
72
+ instance_variable_get("@#{name}_#{limit}") ||
73
+ (limit_value.respond_to?(:call) ? instance_exec(&limit_value) : limit_value)
74
+ end
75
+
76
+ # can manually assign the min/max
77
+ define_method :"#{name}_#{limit}=" do |value|
78
+ instance_variable_set("@#{name}_#{limit}", value)
69
79
  end
80
+
70
81
  end
71
82
 
72
83
  converter = block_given? ? block : Converters.converter_for(:date)
@@ -105,7 +116,7 @@ module Riveter
105
116
 
106
117
  # break down into parts
107
118
  [:from, :to].each do |part|
108
- attr_reader_with_converter :"#{name}_#{part}", converter
119
+ attr_reader_with_converter :"#{name}_#{part}", converter, options
109
120
 
110
121
  define_method :"#{name}_#{part}?" do
111
122
  send(:"#{name}_#{part}").present?
@@ -127,22 +138,6 @@ module Riveter
127
138
  date_from && date_to
128
139
  end
129
140
 
130
- # return from and to as range in UTC
131
- define_method :"#{name}_utc" do
132
- date_from = send(:"#{name}_from")
133
- date_to = send(:"#{name}_to")
134
- if date_from && date_to
135
- if date_from == date_to
136
- date = date_from.to_utc_date
137
- DateTime.new(date.year, date.month, date.day, 0, 0, 0)..DateTime.new(date.year, date.month, date.day, 23, 59, 59)
138
- else
139
- date_from.to_utc_date..date_to.to_utc_date
140
- end
141
- else
142
- nil
143
- end
144
- end
145
-
146
141
  end
147
142
 
148
143
  def attr_boolean(name, options={}, &block)
@@ -152,7 +147,7 @@ module Riveter
152
147
 
153
148
  converter = block_given? ? block : Converters.converter_for(:boolean)
154
149
 
155
- attr_reader_with_converter name, converter
150
+ attr_reader_with_converter name, converter, options
156
151
  alias_method "#{name}?", name
157
152
 
158
153
  attr_writer name
@@ -160,7 +155,10 @@ module Riveter
160
155
  add_attr(name, :boolean, converter, options)
161
156
  end
162
157
 
158
+ # NB: enum must respond to values
163
159
  def attr_enum(name, enum, options={}, &block)
160
+ raise ArgumentError, 'enum' unless enum && enum.respond_to?(:values)
161
+
164
162
  options = {
165
163
  :enum => enum,
166
164
  :validate => true
@@ -169,18 +167,7 @@ module Riveter
169
167
  required = options[:required] == true
170
168
  converter = block_given? ? block : Converters.converter_for(:enum, options)
171
169
 
172
- attr_reader_with_converter name, converter
173
-
174
- # helpers
175
- # TODO: would be nicer to emulate an association
176
-
177
- define_singleton_method "#{name}_enum" do
178
- enum
179
- end
180
-
181
- define_singleton_method name.to_s.pluralize do
182
- enum.collection
183
- end
170
+ attr_reader_with_converter name, converter, options
184
171
 
185
172
  validates name,
186
173
  :allow_blank => !required,
@@ -203,7 +190,7 @@ module Riveter
203
190
 
204
191
  define_method name do
205
192
  array = instance_variable_get("@#{name}") || []
206
- array.map {|v| converter.call(v) }
193
+ array.map {|v| converter.call(v, options) }
207
194
  end
208
195
 
209
196
  attr_writer name
@@ -222,7 +209,7 @@ module Riveter
222
209
 
223
210
  define_method name do
224
211
  hash = instance_variable_get("@#{name}") || {}
225
- hash.merge(hash) {|k, v| converter.call(v) }
212
+ hash.merge(hash) {|k, v| converter.call(v, options) }
226
213
  end
227
214
 
228
215
  attr_writer name
@@ -230,65 +217,46 @@ module Riveter
230
217
  add_attr(name, :hash, converter, options)
231
218
  end
232
219
 
233
- ##
234
- # FIXME: this doesn't work as expected
235
- #
236
- def attr_model(name, model_or_scope, options={}, &block)
220
+ def attr_object(name, options={}, &block)
237
221
  options = {
238
- :model => model_or_scope,
239
- :validate => true,
240
- :find_by => :id
222
+ :validate => true
241
223
  }.merge(options)
242
224
 
243
- required = options[:required] == true
244
- converter = if block_given?
245
- block
246
- elsif model_or_scope.respond_to?(:find_by)
247
- Converters.converter_for(:model, options)
248
- else
249
- Converters.converter_for(:object, options)
250
- end
251
-
252
- # helpers
253
- define_singleton_method "#{name}_model" do
254
- model_or_scope
255
- end
256
-
257
- attr_reader_with_converter name, converter
258
-
259
- # only add validation of the model instance if supported
260
- if model_or_scope.instance_methods.include?(:valid?) && options[:validate]
261
- validate :"validate_#{name}"
225
+ converter = block_given? ? block : Converters.converter_for(:object, options)
262
226
 
263
- # need a "custom" associated validation since
264
- # we don't reference active record...
265
- define_method :"validate_#{name}" do
266
- instance = self.send(name)
267
- return unless required && instance.present?
268
- self.errors.add(name, :invalid) unless instance.valid?
269
- end
270
- end
227
+ attr_reader_with_converter name, converter, options
271
228
 
272
229
  attr_writer name
273
230
 
274
- add_attr(name, :model, converter, options)
231
+ add_attr(name, :object, converter, options)
275
232
  end
276
233
 
277
- def attr_object(name, options={}, &block)
278
- converter = block_given? ? block : Converters.converter_for(:object, options)
234
+ def attr_class(name, classes, options={}, &block)
235
+ options = {
236
+ :validate => true,
237
+ :classes => classes
238
+ }.merge(options)
239
+
240
+ required = options[:required] == true
241
+ converter = block_given? ? block : Converters.converter_for(:class, options)
279
242
 
280
- attr_reader_with_converter name, converter
243
+ attr_reader_with_converter name, converter, options
244
+
245
+ validates name,
246
+ :allow_blank => !required,
247
+ :allow_nil => !required,
248
+ :inclusion => { :in => classes } if options[:validate]
281
249
 
282
250
  attr_writer name
283
251
 
284
- add_attr(name, :object, converter, options)
252
+ add_attr(name, :class, converter, options)
285
253
  end
286
254
 
287
255
  private
288
256
 
289
- def attr_reader_with_converter(name, converter)
257
+ def attr_reader_with_converter(name, converter, options={})
290
258
  define_method name do
291
- converter.call instance_variable_get("@#{name}")
259
+ converter.call(instance_variable_get("@#{name}"), options)
292
260
  end
293
261
  end
294
262
 
@@ -300,7 +268,7 @@ module Riveter
300
268
  required = options[:required] == true
301
269
  converter = block_given? ? block : Converters.converter_for(type)
302
270
 
303
- attr_reader_with_converter name, converter
271
+ attr_reader_with_converter name, converter, options
304
272
 
305
273
  validates name,
306
274
  :allow_blank => !required,
@@ -320,7 +288,7 @@ module Riveter
320
288
  required = options[:required] == true
321
289
  converter = block_given? ? block : Converters.converter_for(type)
322
290
 
323
- attr_reader_with_converter name, converter
291
+ attr_reader_with_converter name, converter, options
324
292
 
325
293
  validates name,
326
294
  :allow_blank => !required,
@@ -333,25 +301,28 @@ module Riveter
333
301
  end
334
302
 
335
303
  def add_attr(name, type, converter=nil, options={})
336
- self._attributes[name] = attribute_info = AttributeInfo.new(name, type, converter, options)
304
+ self._attributes[name] = attribute_info = AttributeInfo.new(self, name, type, converter, options)
337
305
  validates name, :presence => true if attribute_info.required?
338
306
  attribute_info
339
307
  end
340
308
  end
341
309
 
342
- class AttributeInfo < Struct.new(:name, :type, :converter, :options)
310
+ class AttributeInfo < Struct.new(:target, :name, :type, :converter, :options)
343
311
  def required?
344
312
  @required ||= (options[:required] == true)
345
313
  end
346
314
 
347
315
  def default
348
316
  @default ||= options[:default]
349
- @default.respond_to?(:call) ? @default.call : @default
350
317
  end
351
318
 
352
319
  def default?
353
320
  !self.default.nil?
354
321
  end
322
+
323
+ def evaluate_default(scope)
324
+ default? && default.respond_to?(:call) ? scope.instance_exec(&default) : default
325
+ end
355
326
  end
356
327
 
357
328
  attr_reader :options
@@ -360,8 +331,7 @@ module Riveter
360
331
  # assign default values
361
332
  self.class._attributes.each do |name, attribute_info|
362
333
  next unless attribute_info.default?
363
- value = attribute_info.default
364
- send("#{name}=", value.respond_to?(:call) ? value.call : value)
334
+ send("#{name}=", attribute_info.evaluate_default(self))
365
335
  end
366
336
 
367
337
  @options = options.freeze
@@ -461,35 +431,35 @@ module Riveter
461
431
  def self.converter_for(data_type, options={})
462
432
  case data_type
463
433
  when :string
464
- lambda {|v| v.to_s }
434
+ lambda {|v, opt| v.to_s }
465
435
 
466
436
  when :boolean
467
- lambda {|v| v.to_b }
437
+ lambda {|v, opt| v.to_b }
468
438
 
469
439
  when :integer
470
- lambda {|v| Integer(v) rescue v }
440
+ lambda {|v, opt| Integer(v) rescue v }
471
441
 
472
442
  when :decimal, :float
473
- lambda {|v| Float(v) rescue v }
443
+ lambda {|v, opt| Float(v) rescue v }
474
444
 
475
445
  when :date
476
- lambda {|v| Date.parse(v) rescue v }
446
+ lambda {|v, opt| Date.parse(v) rescue v }
477
447
 
478
448
  when :time
479
- lambda {|v| Time.parse(v) rescue v }
449
+ lambda {|v, opt| Time.parse(v) rescue v }
480
450
 
481
451
  when :enum
482
- lambda {|enum, v|
452
+ lambda {|enum, v, opt|
483
453
  enum.values.include?(v) ? v : enum.value_for(v)
484
454
  }.curry[options[:enum]]
485
455
 
486
- when :model
487
- lambda {|model, attrib, v|
488
- v.is_a?(model) ? v : model.find_by(attrib => v)
489
- }.curry[options[:model], options[:find_by]]
456
+ when :class
457
+ lambda {|models, v, opt|
458
+ models[v] || v
459
+ }.curry[options[:classes].inject({}) {|list, klass| list[klass.name] = klass; list }]
490
460
 
491
461
  else # object etc...
492
- lambda {|v| v }
462
+ lambda {|v, opt| v }
493
463
  end
494
464
  end
495
465
 
@@ -1,3 +1,3 @@
1
1
  module Riveter
2
- VERSION = "0.3.0"
2
+ VERSION = "0.6.0"
3
3
  end
@@ -13,7 +13,7 @@ shared_examples_for "an attribute" do |type, default_value, *args, &block|
13
13
 
14
14
  describe "with" do
15
15
  before do
16
- subject.send :"attr_#{type}", name, *args
16
+ subject.send :"attr_#{type}", name, *args, options
17
17
  end
18
18
 
19
19
  it { subject.attributes.should include(name.to_s) }
@@ -60,7 +60,7 @@ shared_examples_for "an attribute" do |type, default_value, *args, &block|
60
60
  end
61
61
 
62
62
  it {
63
- expect(mock_block).to receive(:call).at_least(:once)
63
+ expect(mock_block).to receive(:call).at_least(:once).with(anything, instance_of(Hash))
64
64
  instance.send(name)
65
65
  }
66
66
  end
@@ -23,11 +23,19 @@ describe Riveter::AttributeDefaultValues do
23
23
 
24
24
  it "should have own registration" do
25
25
  expect {
26
- Class.new(ActiveRecord::Base).class_eval do |klass|
26
+ Class.new.class_eval do |klass|
27
+
28
+ # provide mock method for after_initialize
29
+ class << self
30
+ def after_initialize(*args)
31
+ end
32
+ end
33
+
27
34
  include Riveter::AttributeDefaultValues
28
35
 
29
36
  attr_accessor :attr_name
30
37
  default_value_for :attr_name, 1
38
+
31
39
  end
32
40
  }.to_not change { subject.attribute_defaults.length }
33
41
  end
@@ -84,6 +84,8 @@ describe Riveter::Attributes do
84
84
 
85
85
  it { instance.should respond_to(:an_attribute_to?) }
86
86
 
87
+ it { instance.should respond_to(:an_attribute_present?) }
88
+
87
89
  it {
88
90
  instance.an_attribute = nil
89
91
  instance.an_attribute_to?.should be_falsey
@@ -93,7 +95,85 @@ describe Riveter::Attributes do
93
95
  instance.an_attribute_to = Date.today
94
96
  instance.an_attribute_to?.should be_truthy
95
97
  }
98
+
99
+ it {
100
+ instance.an_attribute_from = Date.today
101
+ instance.an_attribute_to = Date.today
102
+ instance.an_attribute_present?.should be_truthy
103
+ }
104
+
105
+ it {
106
+ instance.an_attribute_from = nil
107
+ instance.an_attribute_to = Date.today
108
+ instance.an_attribute_present?.should be_falsey
109
+ }
110
+
111
+ it {
112
+ instance.an_attribute_from = Date.today
113
+ instance.an_attribute_to = nil
114
+ instance.an_attribute_present?.should be_falsey
115
+ }
116
+
117
+ it {
118
+ instance.an_attribute_from = Date.today
119
+ instance.an_attribute_to = Date.today
120
+ instance.an_attribute_present?.should be_truthy
121
+ }
122
+
123
+ end
124
+
125
+ describe "#min" do
126
+ before do
127
+ subject.attr_date_range :an_attribute_value, :min => 1
128
+ subject.attr_date_range :an_attribute_block, :min => lambda { self.some_method }
129
+ end
130
+
131
+ let(:instance) { subject.new() }
132
+
133
+ it { instance.should respond_to(:an_attribute_value_min) }
134
+ it { instance.should respond_to(:an_attribute_value_min=) }
135
+
136
+ it {
137
+ instance.an_attribute_value_min.should eq(1)
138
+ }
139
+
140
+ it {
141
+ instance.an_attribute_value_min = 2
142
+ instance.an_attribute_value_min.should eq(2)
143
+ }
144
+
145
+ it "invokes the lambda in the context of self" do
146
+ expect(instance).to receive(:some_method)
147
+ instance.an_attribute_block_min
148
+ end
96
149
  end
150
+
151
+ describe "#max" do
152
+ before do
153
+ subject.attr_date_range :an_attribute_value, :max => 1
154
+ subject.attr_date_range :an_attribute_block, :max => lambda { self.some_method }
155
+ end
156
+
157
+ let(:instance) { subject.new() }
158
+
159
+ it { instance.should respond_to(:an_attribute_value_max) }
160
+ it { instance.should respond_to(:an_attribute_value_max=) }
161
+
162
+ it {
163
+ instance.an_attribute_value_max.should eq(1)
164
+ }
165
+
166
+ it {
167
+ instance.an_attribute_value_max = 2
168
+ instance.an_attribute_value_max.should eq(2)
169
+ }
170
+
171
+ it "invokes the lambda in the context of self" do
172
+ expect(instance).to receive(:some_method)
173
+ instance.an_attribute_block_max
174
+ end
175
+ end
176
+
97
177
  end
98
178
 
99
179
  it_should_behave_like "an attribute", :time, Time.new(2010, 1, 12, 8, 4, 45) do
@@ -125,12 +205,7 @@ describe Riveter::Attributes do
125
205
  end
126
206
  let(:instance) { subject.new() }
127
207
 
128
- it { instance.should ensure_inclusion_of(:product_type).in_array(TestEnum.values) }
129
-
130
- it { should respond_to(:product_type_enum)}
131
- it { subject.product_type_enum.should eq(TestEnum) }
132
- it { should respond_to(:product_types)}
133
- it { subject.product_types.should eq(TestEnum.collection)}
208
+ it { instance.should validate_inclusion_of(:product_type).in_array(TestEnum.values) }
134
209
  end
135
210
  end
136
211
 
@@ -144,44 +219,23 @@ describe Riveter::Attributes do
144
219
  let(:expected_value) { {:c => 1, :d => 2} }
145
220
  end
146
221
 
147
- it_should_behave_like "an attribute", :model, TestModel.new(), TestModel do
148
- let(:assigned_value) { TestModel.new() }
222
+ it_should_behave_like "an attribute", :object, Object.new() do
223
+ let(:assigned_value) { Object.new() }
224
+ end
149
225
 
150
- before do
151
- allow_any_instance_of(TestModel).to receive(:valid?) { true }
152
- allow(TestModel).to receive(:find_by)
153
- end
226
+ it_should_behave_like "an attribute", :class, TestModel, [TestModel, TestModelWithAttributeDefaultValues] do
227
+ let(:assigned_value) { "TestModelWithAttributeDefaultValues" }
228
+ let(:expected_value) { TestModelWithAttributeDefaultValues }
154
229
 
155
230
  describe "additional" do
156
231
  before do
157
- subject.attr_model :product, TestModel, :required => true
232
+ subject.attr_class :an_attribute, [TestModel, TestModelWithAttributeDefaultValues], :required => true
158
233
  end
234
+ let(:instance) { subject.new() }
159
235
 
160
- it { should respond_to(:product_model)}
161
- it { subject.product_model.should eq(TestModel) }
162
-
163
- it {
164
- allow(TestModel).to receive(:find_by).with(:id => 1) { assigned_value }
165
-
166
- instance = subject.new()
167
- instance.product = 1
168
- instance.product.should eq(assigned_value)
169
- }
170
-
171
- it {
172
- allow(TestModel).to receive(:find_by).with(:id => 1) { assigned_value }
173
- expect(assigned_value).to receive(:valid?) { true }
174
-
175
- instance = subject.new()
176
- instance.product = 1
177
- instance.valid?
178
- }
236
+ it { instance.should validate_inclusion_of(:an_attribute).in_array([TestModel, TestModelWithAttributeDefaultValues]) }
179
237
  end
180
238
  end
181
-
182
- it_should_behave_like "an attribute", :object, Object.new() do
183
- let(:assigned_value) { Object.new() }
184
- end
185
239
  end
186
240
 
187
241
  describe "instance" do
@@ -202,7 +256,6 @@ describe Riveter::Attributes do
202
256
  subject.enum.should eq(TestEnum::Member1)
203
257
  subject.array.should eq([1, 2, 3])
204
258
  subject.hash.should eq({:a => :b})
205
- subject.model.should eq(TestModel)
206
259
  subject.object.should eq('whatever')
207
260
  end
208
261
  end
@@ -225,8 +278,8 @@ describe Riveter::Attributes do
225
278
  'enum' => TestEnum::Member1,
226
279
  'array' => [1, 2, 3],
227
280
  'hash' => {:a => :b},
228
- 'model' => TestModel,
229
- 'object' => 'whatever'
281
+ 'object' => 'whatever',
282
+ 'with_lambda_default' => subject
230
283
  })
231
284
  }
232
285
  end
data/spec/spec_helper.rb CHANGED
@@ -15,7 +15,6 @@ SimpleCov.start do
15
15
  end
16
16
 
17
17
  require 'pry'
18
- require 'active_record'
19
18
 
20
19
  require 'riveter'
21
20
  require 'riveter/spec_helper'
@@ -25,8 +24,17 @@ require 'riveter/form_builder_extensions'
25
24
  require 'riveter/command_routes'
26
25
  require 'riveter/enquiry_routes'
27
26
 
28
- require 'rails/all'
27
+ # require 'rails/all'
28
+
29
+ # require 'active_record/railtie'
30
+ require 'action_controller/railtie'
31
+ require 'action_view/railtie'
32
+ # require 'action_mailer/railtie'
33
+ # require 'active_job/railtie'
34
+ # require 'rails/test_unit/railtie'
35
+ # require 'sprockets/railtie'
29
36
  require 'rails/generators'
37
+
30
38
  require 'haml'
31
39
 
32
40
  require 'shoulda/matchers'
@@ -50,6 +58,7 @@ RSpec.configure do |config|
50
58
 
51
59
  # config.order = :random
52
60
  config.fail_fast = (ENV["FAIL_FAST"] == 1)
61
+
53
62
  end
54
63
 
55
64
  # require examples, must happen after configure for RSpec 3
@@ -15,7 +15,8 @@ class TestClassWithAttributes
15
15
  attr_enum :enum, TestEnum, :default => TestEnum::Member1
16
16
  attr_array :array, :default => [1, 2, 3]
17
17
  attr_hash :hash, :default => {:a => :b}
18
- attr_model :model, TestModel, :default => TestModel
19
18
  attr_object :object, :default => 'whatever'
20
19
 
20
+ attr_object :with_lambda_default, :default => lambda { self }
21
+
21
22
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: riveter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Stefano
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-07 00:00:00.000000000 Z
11
+ date: 2016-09-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties