symbolic_enum 1.0.1 → 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 +4 -4
- data/Gemfile.lock +1 -1
- data/lib/symbolic_enum.rb +19 -9
- data/lib/symbolic_enum/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 25b1c38a06b81fb3d48c8dccd8c7d2b0f524b766
|
4
|
+
data.tar.gz: 9f3ea5c4e31246049191a749f935b32057a6f32c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8b34f8fa175c5b49ddc1e0d6e4a7d2b884bf22e919e32269cd5182e2d3f47eb027fa6126f046366279c7e87ccccbef5f690eea31d23f6241defc38fcab8b4a4d
|
7
|
+
data.tar.gz: 90b012ae6e20eebcd93a4d9836be4eca1c35e3112fe63d120911efc99a558851b382f0a692b9fddd8a87b2715718c15da1d82b6de5c57fffd505a249f7e01820
|
data/Gemfile.lock
CHANGED
data/lib/symbolic_enum.rb
CHANGED
@@ -12,12 +12,6 @@ module SymbolicEnum
|
|
12
12
|
field = params.keys.first
|
13
13
|
mapping = params[field]
|
14
14
|
|
15
|
-
mapping.keys.each do |enum_name|
|
16
|
-
raise ArgumentError.new("'#{enum_name}' clashes with existing methods") if self.instance_methods.include?(:"#{enum_name}!")
|
17
|
-
raise ArgumentError.new("'#{enum_name}' clashes with existing methods") if self.instance_methods.include?(:"#{enum_name}?")
|
18
|
-
raise ArgumentError.new("'#{enum_name}' clashes with existing methods") if self.singleton_methods.include?(:"#{enum_name}!")
|
19
|
-
end
|
20
|
-
|
21
15
|
options = params.reject{ |k,v| k == field }
|
22
16
|
|
23
17
|
raise ArgumentError.new("argument has to be a Hash of field and mapping of unique Symbols to numbers, with optional configuration params") unless mapping.keys.count == mapping.keys.uniq.count && mapping.values.count == mapping.values.uniq.count && mapping.keys.map(&:class).uniq == [Symbol] && (mapping.values.map(&:class).uniq == [Integer] || mapping.values.map(&:class).uniq == [Fixnum])
|
@@ -25,12 +19,24 @@ module SymbolicEnum
|
|
25
19
|
case key
|
26
20
|
when :array
|
27
21
|
raise ArgumentError.new("'array' option can be only true/false") unless [true, false].include?(value)
|
22
|
+
when :disable_scopes
|
23
|
+
raise ArgumentError.new("'disable_scopes' option can be only true/false") unless [true, false].include?(value)
|
24
|
+
when :disable_setters
|
25
|
+
raise ArgumentError.new("'disable_setters' option can be only true/false") unless [true, false].include?(value)
|
28
26
|
else
|
29
27
|
raise ArgumentError.new("'#{ key }' is not a valid option")
|
30
28
|
end
|
31
29
|
end
|
32
30
|
|
33
31
|
is_array = options[:array]
|
32
|
+
disable_scopes = options[:disable_scopes]
|
33
|
+
disable_setters = options[:disable_setters]
|
34
|
+
|
35
|
+
mapping.keys.each do |enum_name|
|
36
|
+
raise ArgumentError.new("'#{enum_name}' clashes with existing methods") if self.instance_methods.include?(:"#{enum_name}!") unless disable_setters
|
37
|
+
raise ArgumentError.new("'#{enum_name}' clashes with existing methods") if self.instance_methods.include?(:"#{enum_name}?")
|
38
|
+
raise ArgumentError.new("'#{enum_name}' clashes with existing methods") if self.singleton_methods.include?(enum_name) unless disable_scopes
|
39
|
+
end
|
34
40
|
|
35
41
|
# Replicating enum functionality (partially)
|
36
42
|
define_singleton_method("#{ field.to_s.pluralize }") do
|
@@ -64,14 +70,18 @@ module SymbolicEnum
|
|
64
70
|
end
|
65
71
|
|
66
72
|
mapping.each_pair do |state_name, state_value|
|
67
|
-
|
73
|
+
unless disable_scopes
|
74
|
+
scope state_name, -> { where(field => state_value) }
|
75
|
+
end
|
68
76
|
|
69
77
|
define_method("#{ state_name }?".to_sym) do
|
70
78
|
self[field] == state_value
|
71
79
|
end
|
72
80
|
|
73
|
-
|
74
|
-
|
81
|
+
unless disable_setters
|
82
|
+
define_method("#{ state_name }!".to_sym) do
|
83
|
+
self.update_attributes!(field => state_value)
|
84
|
+
end
|
75
85
|
end
|
76
86
|
end
|
77
87
|
end
|