enum_set 0.0.1 → 0.0.2
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 +4 -4
- data/README.md +3 -3
- data/lib/enum_set/version.rb +1 -1
- data/lib/enum_set.rb +10 -2
- data/spec/enum_many_spec.rb +14 -4
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 490f7a3dc76517686a72eac0e8ddb6d09bc44efc
|
4
|
+
data.tar.gz: 5eec2d3ed8f99f8cda35914b12ce88e1cab2b7a0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8bb1aa81251e9dd0d7a15e72ff75aaddaf988d5cddceda1cc6c8baf05a64f256ac034d8286c0dcc32ec62c8b15d1e42fc73e3c51975420b75286f3ec58c11222
|
7
|
+
data.tar.gz: 56f0f8a5cca344fca525be8189d2905b5c0472de361a40ff6e910da8602db8135b5eb6c0e567f2c2ab53ad140714a8dde93e3bdf5d42a520dc1b1c8a7fb07bbd
|
data/README.md
CHANGED
@@ -10,11 +10,11 @@ as well as an array setter.
|
|
10
10
|
|
11
11
|
* With bundler:
|
12
12
|
|
13
|
-
|
13
|
+
$ gem 'enum_set'
|
14
14
|
|
15
15
|
* Without bundler:
|
16
16
|
|
17
|
-
|
17
|
+
$ gem install enum_set
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
@@ -26,7 +26,7 @@ class User < ActiveRecord::Base
|
|
26
26
|
end
|
27
27
|
|
28
28
|
# elsewhere.rb
|
29
|
-
user = User.create(roles: [:super_user)
|
29
|
+
user = User.create(roles: [:super_user])
|
30
30
|
|
31
31
|
user.super_user? # => true
|
32
32
|
user.admin? # => false
|
data/lib/enum_set/version.rb
CHANGED
data/lib/enum_set.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
require "enum_set/version"
|
2
2
|
|
3
3
|
module EnumSet
|
4
|
+
EnumError = Class.new(NameError)
|
5
|
+
|
4
6
|
def self.included(base)
|
5
7
|
base.extend ClassMethods
|
6
8
|
end
|
@@ -26,12 +28,18 @@ module EnumSet
|
|
26
28
|
end
|
27
29
|
|
28
30
|
define_method :"#{column}=" do |array|
|
31
|
+
new_value = send("#{column}_bitfield")
|
32
|
+
|
33
|
+
array.each do |val|
|
34
|
+
raise EnumError.new("Unrecognized value for #{column}: #{val.inspect}") unless names.include?(val)
|
35
|
+
end
|
36
|
+
|
29
37
|
array.each do |val|
|
30
38
|
bit = names_with_bits.find { |name,_| name == val.to_sym }.last
|
31
|
-
new_value
|
32
|
-
self[column] = new_value
|
39
|
+
new_value |= bit
|
33
40
|
end
|
34
41
|
|
42
|
+
self[column] = new_value
|
35
43
|
send(column)
|
36
44
|
end
|
37
45
|
|
data/spec/enum_many_spec.rb
CHANGED
@@ -19,10 +19,20 @@ describe EnumSet do
|
|
19
19
|
expect(user.roles).to eq [:super_user]
|
20
20
|
end
|
21
21
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
22
|
+
describe 'array setters' do
|
23
|
+
it 'lets enum values be set' do
|
24
|
+
user.roles <<= :admin
|
25
|
+
user.save!
|
26
|
+
expect(user.reload).to be_admin
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'when a nonexistent enum value is provided' do
|
30
|
+
it 'raises a NameError' do
|
31
|
+
expect {
|
32
|
+
user.roles <<= :gender
|
33
|
+
}.to raise_error EnumSet::EnumError
|
34
|
+
end
|
35
|
+
end
|
26
36
|
end
|
27
37
|
|
28
38
|
it 'scopes by enum value' do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enum_set
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bree Stanwyck
|
@@ -140,3 +140,4 @@ summary: Allows using a single integer column as a set of booleans, with bitfiel
|
|
140
140
|
test_files:
|
141
141
|
- spec/database.rb
|
142
142
|
- spec/enum_many_spec.rb
|
143
|
+
has_rdoc:
|