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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b2b86144e974adbdb1935a9f3193663a3c7a1829
4
- data.tar.gz: 1c463a6a15cea4a74de5fd858e2f92718960d9a4
3
+ metadata.gz: 25b1c38a06b81fb3d48c8dccd8c7d2b0f524b766
4
+ data.tar.gz: 9f3ea5c4e31246049191a749f935b32057a6f32c
5
5
  SHA512:
6
- metadata.gz: 71102da522d8c229e9a502e384d0d746f498bf88f30725f15ad45a78b732ed495cbf668b69a13a054147a4fbf796c53e8c42b0710920a38548553189fbacbf8b
7
- data.tar.gz: 64fd7a139ce55f008d98cb83ac2b34d043687fcd84fa8796a2645b6f15e4ce5e787685643a0a905b23b5180c40a4fc157b6c462d45b3edfa428a006aec410ec8
6
+ metadata.gz: 8b34f8fa175c5b49ddc1e0d6e4a7d2b884bf22e919e32269cd5182e2d3f47eb027fa6126f046366279c7e87ccccbef5f690eea31d23f6241defc38fcab8b4a4d
7
+ data.tar.gz: 90b012ae6e20eebcd93a4d9836be4eca1c35e3112fe63d120911efc99a558851b382f0a692b9fddd8a87b2715718c15da1d82b6de5c57fffd505a249f7e01820
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- symbolic_enum (1.0.0)
4
+ symbolic_enum (1.0.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -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
- scope state_name, -> { where(field => state_value) }
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
- define_method("#{ state_name }!".to_sym) do
74
- self.update_attributes!(field => state_value)
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
@@ -1,3 +1,3 @@
1
1
  module SymbolicEnum
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: symbolic_enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ajith Hussain