enumerize 1.0.0 → 1.1.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: 4a571fc9e3b62621d40cfddd9ba755389dda8115
4
- data.tar.gz: e6a8a7e2043a36b8072a2f28cc547efab248770a
3
+ metadata.gz: a5f9d4b829c7ef4c4f41e2a96e041a4a66771268
4
+ data.tar.gz: b961d9d0ebe14b4d0f2eadc334fef61cc348283f
5
5
  SHA512:
6
- metadata.gz: bda0563e282a121e0b5e0a26144a81efc8bde072ebf1a56a357da909421f272cc02b5c7dd85dac6cf7780b42a88e302f7aef020434255682dc9a7d7840e21e9a
7
- data.tar.gz: 5c0d858c82b33958edd3a3c8e2b347822641966e6765ad87c7fae7158b1fee0ddaac08ecbc5b88849e392b928c474435e372ceeb2beec7a9c0b3052f937073a3
6
+ metadata.gz: 53ee726338ce9d36c152691073e5bae144e6fbb665c8d03511a7e66772e116d90dd1a74c8d7b1fa100e7855e9336d45ef5d3afdeb05053f398ee3a073bd854c2
7
+ data.tar.gz: f3ac55edcb00daeca98564a1da87c4df44c5c32a76e422ad206d50ee7fa0821964c572eefc030b4fec4edb7710582730f43435a57ec452284a235bb39e7e7db5
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/CHANGELOG.md CHANGED
@@ -4,6 +4,15 @@
4
4
 
5
5
  ### bug fix
6
6
 
7
+ ## 1.1.0 (November 15, 2015)
8
+
9
+ ### enhancements
10
+ * Add Sequel support. (by [@mrbrdo](https://github.com/mrbrdo))
11
+ * Add qualifiers to RSpec matcher. (by [@maurogeorge](https://github.com/maurogeorge))
12
+ * Support hash in the RSpec matcher. (by [@maurogeorge](https://github.com/maurogeorge))
13
+
14
+ ### bug fix
15
+
7
16
  ## 1.0.0 (August 2, 2015)
8
17
 
9
18
  ### enhancements
data/Gemfile CHANGED
@@ -1,22 +1,5 @@
1
- source 'https://rubygems.org'
2
-
3
- gemspec
4
-
5
- gem 'rake'
6
- gem 'minitest', '~> 5.5.1'
7
- gem 'rspec', :require => false
8
-
9
- gem 'rails', '4.2.0', :require => false
10
- gem 'sqlite3', :platform => [:ruby, :mswin, :mingw]
11
- gem 'activerecord-jdbcsqlite3-adapter', :platform => :jruby
12
-
13
- platforms :rbx do
14
- gem 'rubysl', '~> 2.0'
15
- gem 'psych'
16
- gem 'rubinius-developer_tools'
17
- gem 'rubysl-test-unit'
18
- end
1
+ eval_gemfile('Gemfile.global')
19
2
 
3
+ gem 'minitest', '~> 5.8'
4
+ gem 'rails', '4.2.4', :require => false
20
5
  gem 'mongoid'
21
- gem 'simple_form'
22
- gem 'formtastic'
data/Gemfile.global ADDED
@@ -0,0 +1,20 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'rake'
6
+ gem 'rspec', :require => false
7
+
8
+ gem 'sqlite3', :platform => [:ruby, :mswin, :mingw]
9
+ gem 'activerecord-jdbcsqlite3-adapter', :platform => :jruby
10
+ gem 'sequel'
11
+
12
+ platforms :rbx do
13
+ gem 'rubysl', '~> 2.0'
14
+ gem 'psych'
15
+ gem 'rubinius-developer_tools'
16
+ gem 'rubysl-test-unit'
17
+ end
18
+
19
+ gem 'simple_form'
20
+ gem 'formtastic'
data/Gemfile.mongo_mapper CHANGED
@@ -1,22 +1,5 @@
1
- source 'https://rubygems.org'
2
-
3
- gemspec
4
-
5
- gem 'rake'
6
- gem 'minitest', '~> 5.5.1'
7
- gem 'rspec', :require => false
8
-
9
- gem 'rails', '4.2.0', :require => false
10
- gem 'sqlite3', :platform => [:ruby, :mswin, :mingw]
11
- gem 'activerecord-jdbcsqlite3-adapter', :platform => :jruby
12
-
13
- platforms :rbx do
14
- gem 'rubysl', '~> 2.0'
15
- gem 'psych'
16
- gem 'rubinius-developer_tools'
17
- gem 'rubysl-test-unit'
18
- end
1
+ eval_gemfile('Gemfile.global')
19
2
 
3
+ gem 'minitest', '~> 5.8'
4
+ gem 'rails', '4.2.4', :require => false
20
5
  gem 'mongo_mapper'
21
- gem 'simple_form'
22
- gem 'formtastic'
data/Gemfile.rails40 CHANGED
@@ -1,22 +1,5 @@
1
- source 'https://rubygems.org'
1
+ eval_gemfile('Gemfile.global')
2
2
 
3
- gemspec
4
-
5
- gem 'rake'
6
3
  gem 'minitest', '~> 4.1'
7
- gem 'rspec', :require => false
8
-
9
- gem 'rails', '4.0.1', :require => false
10
- gem 'sqlite3', :platform => [:ruby, :mswin, :mingw]
11
- gem 'activerecord-jdbcsqlite3-adapter', :platform => :jruby
12
-
13
- platforms :rbx do
14
- gem 'rubysl', '~> 2.0'
15
- gem 'psych'
16
- gem 'rubinius-developer_tools'
17
- gem 'rubysl-test-unit'
18
- end
19
-
4
+ gem 'rails', '4.0.13', :require => false
20
5
  gem 'mongoid'
21
- gem 'simple_form'
22
- gem 'formtastic'
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Enumerize [![TravisCI](https://secure.travis-ci.org/brainspec/enumerize.png?branch=master)](http://travis-ci.org/brainspec/enumerize) [![Gemnasium](https://gemnasium.com/brainspec/enumerize.png)](https://gemnasium.com/brainspec/enumerize)
2
2
 
3
- Enumerated attributes with I18n and ActiveRecord/Mongoid/MongoMapper support
3
+ Enumerated attributes with I18n and ActiveRecord/Mongoid/MongoMapper/Sequel support
4
4
 
5
5
  ## Installation
6
6
 
@@ -100,7 +100,7 @@ en:
100
100
  female: "Female"
101
101
  ```
102
102
 
103
- You can also pass `i18n_scope` option to specify scope (or array of scopes) storring the translations. Note that `i18n_scope` option does not accept scope as array:
103
+ You can also pass `i18n_scope` option to specify scope (or array of scopes) storing the translations.
104
104
 
105
105
 
106
106
  ```ruby
@@ -322,15 +322,155 @@ Also you can use builtin RSpec matcher:
322
322
  class User
323
323
  extend Enumerize
324
324
 
325
- enumerize :sex, in: [:male, :female], default: :male
325
+ enumerize :sex, in: [:male, :female]
326
326
  end
327
327
 
328
328
  describe User do
329
- it { should enumerize(:sex).in(:male, :female) }
330
- it { should enumerize(:sex).in(:male, :female).with_default(:male) }
329
+ it { should enumerize(:sex) }
331
330
 
332
331
  # or with RSpec 3 expect syntax
333
- it { is_expected.to enumerize(:sex).in(:male, :female) }
332
+ it { is_expected.to enumerize(:sex) }
333
+ end
334
+ ```
335
+
336
+ #### Qualifiers
337
+
338
+ ##### in
339
+
340
+ Use `in` to test usage of the `:in` option.
341
+
342
+ ```ruby
343
+ class User
344
+ extend Enumerize
345
+
346
+ enumerize :sex, in: [:male, :female]
347
+ end
348
+
349
+ describe User do
350
+ it { should enumerize(:sex).in(:male, :female) }
351
+ end
352
+ ```
353
+
354
+ You can test enumerized attribute value using custom values with the `in`
355
+ qualifier.
356
+
357
+ ```ruby
358
+ class User
359
+ extend Enumerize
360
+
361
+ enumerize :sex, in: { male: 0, female: 1 }
362
+ end
363
+
364
+ describe User do
365
+ it { should enumerize(:sex).in(male: 0, female: 1) }
366
+ end
367
+ ```
368
+
369
+ ##### with_default
370
+
371
+ Use `with_default` to test usage of the `:default` option.
372
+
373
+ ```ruby
374
+ class User
375
+ extend Enumerize
376
+
377
+ enumerize :sex, in: [:male, :female], default: :female
378
+ end
379
+
380
+ describe User do
381
+ it { should enumerize(:sex).in(:male, :female).with_default(:female) }
382
+ end
383
+ ```
384
+
385
+ ##### with_i18n_scope
386
+
387
+ Use `with_i18n_scope` to test usage of the `:i18n_scope` option.
388
+
389
+ ```ruby
390
+ class User
391
+ extend Enumerize
392
+
393
+ enumerize :sex, in: [:male, :female], i18n_scope: 'sex'
394
+ end
395
+
396
+ describe User do
397
+ it { should enumerize(:sex).in(:male, :female).with_i18n_scope('sex') }
398
+ end
399
+ ```
400
+
401
+ ##### with_predicates
402
+
403
+ Use `with_predicates` to test usage of the `:predicates` option.
404
+
405
+ ```ruby
406
+ class User
407
+ extend Enumerize
408
+
409
+ enumerize :sex, in: [:male, :female], predicates: true
410
+ end
411
+
412
+ describe User do
413
+ it { should enumerize(:sex).in(:male, :female).with_predicates(true) }
414
+ end
415
+ ```
416
+
417
+ You can text prefixed predicates with the `with_predicates` qualifiers.
418
+
419
+ ```ruby
420
+ class User
421
+ extend Enumerize
422
+
423
+ enumerize :sex, in: [:male, :female], predicates: { prefix: true }
424
+ end
425
+
426
+ describe User do
427
+ it { should enumerize(:sex).in(:male, :female).with_predicates(prefix: true) }
428
+ end
429
+ ```
430
+
431
+ ##### with_scope
432
+
433
+ Use `with_scope` to test usage of the `:scope` option.
434
+
435
+ ```ruby
436
+ class User
437
+ extend Enumerize
438
+
439
+ enumerize :sex, in: [:male, :female], scope: true
440
+ end
441
+
442
+ describe User do
443
+ it { should enumerize(:sex).in(:male, :female).with_scope(true) }
444
+ end
445
+ ```
446
+
447
+ You can text custom scope with the `with_scope` qualifiers.
448
+
449
+ ```ruby
450
+ class User
451
+ extend Enumerize
452
+
453
+ enumerize :sex, in: [:male, :female], scope: :having_sex
454
+ end
455
+
456
+ describe User do
457
+ it { should enumerize(:sex).in(:male, :female).with_scope(scope: :having_sex) }
458
+ end
459
+ ```
460
+
461
+ ##### with_multiple
462
+
463
+ Use `with_multiple` to test usage of the `:multiple` option.
464
+
465
+ ```ruby
466
+ class User
467
+ extend Enumerize
468
+
469
+ enumerize :sex, in: [:male, :female], multiple: true
470
+ end
471
+
472
+ describe User do
473
+ it { should enumerize(:sex).in(:male, :female).with_multiple(true) }
334
474
  end
335
475
  ```
336
476
 
data/Rakefile CHANGED
@@ -10,8 +10,6 @@ Rake::TestTask.new do |t|
10
10
  t.verbose = true
11
11
  end
12
12
 
13
- RSpec::Core::RakeTask.new('default') do |t|
14
- t.pattern = 'test/rspec_spec.rb'
15
- end
13
+ RSpec::Core::RakeTask.new
16
14
 
17
- task :default => :test
15
+ task :default => [:test, :spec]
data/lib/enumerize.rb CHANGED
@@ -13,10 +13,12 @@ module Enumerize
13
13
  autoload :ModuleAttributes, 'enumerize/module_attributes'
14
14
 
15
15
  autoload :ActiveRecordSupport, 'enumerize/activerecord'
16
+ autoload :SequelSupport, 'enumerize/sequel'
16
17
  autoload :MongoidSupport, 'enumerize/mongoid'
17
18
 
18
19
  module Scope
19
20
  autoload :ActiveRecord, 'enumerize/scope/activerecord'
21
+ autoload :Sequel, 'enumerize/scope/sequel'
20
22
  autoload :Mongoid, 'enumerize/scope/mongoid'
21
23
  end
22
24
 
@@ -39,6 +41,11 @@ module Enumerize
39
41
  base.extend Enumerize::Scope::Mongoid
40
42
  end
41
43
 
44
+ if defined?(::Sequel::Model)
45
+ base.extend Enumerize::SequelSupport
46
+ base.extend Enumerize::Scope::Sequel
47
+ end
48
+
42
49
  if defined?(::RailsAdmin)
43
50
  require 'enumerize/integrations/rails_admin'
44
51
  base.extend Enumerize::Integrations::RailsAdmin
@@ -65,8 +65,10 @@ module Enumerize
65
65
 
66
66
  if _enumerized_values_for_validation.has_key?(key)
67
67
  _enumerized_values_for_validation[key]
68
- else
68
+ elsif defined?(super)
69
69
  super
70
+ else
71
+ send(key)
70
72
  end
71
73
  end
72
74
 
@@ -0,0 +1,19 @@
1
+ module Enumerize
2
+ module Hooks
3
+ module SequelDataset
4
+ def self.included(klass)
5
+ klass.alias_method_chain :literal_append, :enumerize
6
+ end
7
+
8
+ def literal_append_with_enumerize(sql, v)
9
+ if v.is_a?(Enumerize::Value)
10
+ literal_append(sql, v.value)
11
+ else
12
+ literal_append_without_enumerize(sql, v)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ ::Sequel::Dataset.send :include, Enumerize::Hooks::SequelDataset
@@ -2,37 +2,57 @@ module Enumerize
2
2
  module Integrations
3
3
  module RSpec
4
4
  class Matcher
5
- attr_accessor :attr, :values, :subject, :default
6
5
 
7
- def initialize(attr)
8
- self.attr = attr
6
+ def initialize(expected_attr)
7
+ self.expected_attr = expected_attr
9
8
  end
10
9
 
11
- def in(*values)
12
- self.values = values.map(&:to_s).sort
10
+ def in(*expected_values)
11
+ self.expected_values = expected_values
13
12
  self
14
13
  end
15
14
 
16
- def with_default(default)
17
- self.default = default.to_s
15
+ def with_default(expected_default)
16
+ self.expected_default = expected_default.to_s
18
17
  self
19
18
  end
20
19
 
21
- def failure_message
22
- message = " expected :#{attr} to allow value#{values.size == 1 ? nil : 's'}: #{quote_values(values)},"
23
- message += " but it allows #{quote_values(enumerized_values)} instead"
20
+ def with_i18n_scope(expected_i18n_scope)
21
+ self.expected_i18n_scope = expected_i18n_scope
22
+ self
23
+ end
24
24
 
25
- if default && !matches_default_value?
26
- message = " expected :#{attr} to have #{default.inspect} as default value,"
27
- message += " but it sets #{enumerized_default.inspect} instead"
28
- end
25
+ def with_predicates(expected_predicates)
26
+ self.expected_predicates = expected_predicates
27
+ self
28
+ end
29
+
30
+ def with_multiple(expected_multiple)
31
+ self.expected_multiple = expected_multiple
32
+ self
33
+ end
34
+
35
+ def with_scope(expected_scope)
36
+ self.expected_scope = expected_scope
37
+ self
38
+ end
39
+
40
+ def failure_message
41
+ "Expected #{expectation}"
42
+ end
29
43
 
30
- message
44
+ def failure_message_when_negated
45
+ "Did not expect #{expectation}"
31
46
  end
32
47
 
33
48
  def description
34
- description = "enumerize :#{attr} in: #{quote_values(values)}"
35
- description += " with #{default.inspect} as default value" if default
49
+ description = "define enumerize :#{expected_attr}"
50
+ description += " in: #{quote_values(expected_values)}" if expected_values
51
+ description += " with #{expected_default.inspect} as default value" if expected_default
52
+ description += " i18n_scope: #{expected_i18n_scope.inspect}" if expected_i18n_scope
53
+ description += " predicates: #{expected_predicates.inspect}" if expected_predicates
54
+ description += " multiple: #{expected_multiple.inspect}" if expected_multiple
55
+ description += " scope: #{expected_scope.inspect}" if expected_scope
36
56
 
37
57
  description
38
58
  end
@@ -41,36 +61,97 @@ module Enumerize
41
61
  self.subject = subject
42
62
  matches = true
43
63
 
44
- matches &= matches_attributes?
45
- matches &= matches_default_value? if default
64
+ matches &= matches_attribute?
65
+ matches &= matches_values? if expected_values
66
+ matches &= matches_default_value? if expected_default
67
+ matches &= matches_i18n_scope? if expected_i18n_scope
68
+ matches &= matches_predicates? if expected_predicates
69
+ matches &= matches_multiple? if expected_multiple
70
+ matches &= matches_scope? if expected_scope
46
71
 
47
72
  matches
48
73
  end
49
74
 
50
75
  private
76
+ attr_accessor :expected_attr, :expected_values, :subject, :expected_default,
77
+ :expected_i18n_scope, :expected_predicates, :expected_multiple,
78
+ :expected_scope
51
79
 
52
- def matches_attributes?
53
- values == enumerized_values
80
+ def expectation
81
+ "#{subject.class.name} to #{description}"
82
+ end
83
+
84
+ def matches_attribute?
85
+ attributes.present?
86
+ end
87
+
88
+ def matches_values?
89
+ matches_array_values? || matches_hash_values?
90
+ end
91
+
92
+ def matches_array_values?
93
+ sorted_values == enumerized_values
94
+ end
95
+
96
+ def matches_hash_values?
97
+ return unless expected_values.first.is_a?(Hash)
98
+ expected_values.first.all? { |k, v| enumerized_value_hash[k.to_s] == v; }
54
99
  end
55
100
 
56
101
  def matches_default_value?
57
- default == enumerized_default
102
+ expected_default == enumerized_default
103
+ end
104
+
105
+ def matches_i18n_scope?
106
+ attributes.i18n_scope == expected_i18n_scope
107
+ end
108
+
109
+ def matches_predicates?
110
+ if expected_predicates.is_a?(TrueClass)
111
+ subject.respond_to?("#{sorted_values.first}?")
112
+ else
113
+ subject.respond_to?("#{expected_attr}_#{attributes.values.first}?")
114
+ end
115
+ end
116
+
117
+ def matches_multiple?
118
+ subject.instance_variable_defined?("@_#{expected_attr}_enumerized_set")
119
+ end
120
+
121
+ def matches_scope?
122
+ if expected_scope.is_a?(TrueClass)
123
+ subject_class.respond_to?("with_#{expected_attr}")
124
+ else
125
+ subject_class.respond_to?(expected_scope[:scope])
126
+ end
127
+ end
128
+
129
+ def sorted_values
130
+ @sorted_values ||=expected_values.map(&:to_s).sort
58
131
  end
59
132
 
60
133
  def enumerized_values
61
- @enumerized_values ||= attributes[attr.to_s].values.sort
134
+ @enumerized_values ||= attributes.values.sort
62
135
  end
63
136
 
64
137
  def enumerized_default
65
- @enumerized_default ||= attributes[attr.to_s].default_value
138
+ @enumerized_default ||= attributes.default_value
139
+ end
140
+
141
+ def enumerized_value_hash
142
+ @enumerized_value_hash ||= attributes.instance_variable_get('@value_hash')
66
143
  end
67
144
 
68
145
  def attributes
69
- subject.class.enumerized_attributes.attributes
146
+ subject_class.enumerized_attributes.attributes[expected_attr.to_s]
147
+ end
148
+
149
+ def subject_class
150
+ @subject_class ||= subject.class
70
151
  end
71
152
 
72
153
  def quote_values(values)
73
- values.map(&:inspect).join(', ')
154
+ sorted_values.map(&:inspect).join(', ')
74
155
  end
75
156
  end
76
157
  end