yinum 1.6.0 → 1.7.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/.gitignore +1 -0
- data/CHANGELOG.md +3 -0
- data/README.md +50 -6
- data/UNLICENSE +24 -0
- data/lib/enum/enum_value.rb +46 -15
- data/lib/enum/helpers/enum_attribute.rb +1 -1
- data/lib/enum/helpers/enum_column.rb +1 -1
- data/lib/enum/version.rb +3 -0
- metadata +6 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a6c73933a509d17c611e833780d494509c743d76
|
4
|
+
data.tar.gz: 08cbfb277952c6542c39b1dc5ebe9e9e331f2682
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6e2e98a5b4b03df21652b9539da8e29e7a7661fde7df110d155172ef4c05bad87a3e8de61ae042490487139bee7b6a2e46252ddd1739d3cf51ec6a54a568dff1
|
7
|
+
data.tar.gz: 467a1640a733bdef9a958c1c8b452cfbe50af55cd913395af3bccceade2b6111ab3eab2224d56cd141eebb6ca389ead9d05b3be52d601082c6e28e1e1ed21766
|
data/.gitignore
CHANGED
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -49,6 +49,12 @@ Getting enum values:
|
|
49
49
|
=> FRUITS.apple
|
50
50
|
FRUITS[1]
|
51
51
|
=> FRUITS.apple
|
52
|
+
FRUITS['orange']
|
53
|
+
=> FRUITS.orange
|
54
|
+
FRUITS['2']
|
55
|
+
=> FRUITS.orange
|
56
|
+
FRUITS['orange', '1']
|
57
|
+
=> [FRUITS.orange, FRUITS.apple]
|
52
58
|
|
53
59
|
Comparing enum values:
|
54
60
|
|
@@ -72,6 +78,33 @@ The enum value is actually a delegate to the value with a special inspect and co
|
|
72
78
|
fruit > FRUITS.apple
|
73
79
|
=> true
|
74
80
|
|
81
|
+
Generating enum attribute methods for your class:
|
82
|
+
|
83
|
+
class Car
|
84
|
+
attr_accessor :color
|
85
|
+
attr_enum :color, :COLORS, :red => 1, :black => 2
|
86
|
+
end
|
87
|
+
car = Car.new
|
88
|
+
car.color = :red
|
89
|
+
car.color
|
90
|
+
=> Car::COLORS.red
|
91
|
+
car.color = "2"
|
92
|
+
car.color
|
93
|
+
=> Car::COLORS.black
|
94
|
+
car.color.black?
|
95
|
+
=> false
|
96
|
+
|
97
|
+
If this is a defining attribute for the class, add `:qualifier => true` to generate question methods like so:
|
98
|
+
|
99
|
+
class Car
|
100
|
+
attr_accessor :color
|
101
|
+
attr_enum :color, :COLORS, { :qualifier => true }, :red => 1, :black => 2
|
102
|
+
end
|
103
|
+
car = Car.new
|
104
|
+
car.color = :red
|
105
|
+
car.red?
|
106
|
+
=> true
|
107
|
+
|
75
108
|
How the enum gets along with Rails, assuming the following model:
|
76
109
|
|
77
110
|
# == Schema Info
|
@@ -93,7 +126,7 @@ Now the `color` column is always read and written using the enum value feature:
|
|
93
126
|
Car.last.color.red?
|
94
127
|
=> true
|
95
128
|
|
96
|
-
This also creates a `
|
129
|
+
This also creates a `validates_inclusion_of` with `allow_nil => true` to prevent having bad values.
|
97
130
|
|
98
131
|
Adding a `:scoped => true` option before the enum values allows automatic generation of scopes and question
|
99
132
|
methods on the model as follows:
|
@@ -106,7 +139,7 @@ methods on the model as follows:
|
|
106
139
|
Car.last.red?
|
107
140
|
=> true
|
108
141
|
|
109
|
-
You can also use another class's enum for your class with enum\_column (including generated methods) like so:
|
142
|
+
You can also use another class's enum for your class with enum\_column and attr\_enum (including generated methods) like so:
|
110
143
|
|
111
144
|
class Motorcycle < ActiveRecord::Base
|
112
145
|
enum_column :color, Car::COLORS, { :scoped => true }
|
@@ -137,10 +170,12 @@ The following will occur:
|
|
137
170
|
=> "Green Apple"
|
138
171
|
FRUITS[2].t
|
139
172
|
=> "Orange Color"
|
140
|
-
Car::COLORS.red
|
173
|
+
Car::COLORS.red.t
|
141
174
|
=> "Shiny Red"
|
142
|
-
Car::COLORS.black
|
175
|
+
Car::COLORS.black.t
|
143
176
|
=> "Actually, white"
|
177
|
+
Car::COLORS.options # nice for options_for_select
|
178
|
+
=> { "Shiny Red" => :red, "Actually, white" => :black }
|
144
179
|
|
145
180
|
When the enum is given a parent, the class's name is used in the translation.
|
146
181
|
If the translation is missing, it fallbacks to the translation without the class's name.
|
@@ -152,13 +187,13 @@ All enum names (usually CONSTANT\_LIKE) and parent class names are converted to
|
|
152
187
|
Since the `===` operator is called on the when value, this syntax cannot be used:
|
153
188
|
|
154
189
|
case fruit
|
155
|
-
when :
|
190
|
+
when :apple then "This will never happen!"
|
156
191
|
end
|
157
192
|
|
158
193
|
The following should be used instead:
|
159
194
|
|
160
195
|
case fruit
|
161
|
-
when FRUITS.
|
196
|
+
when FRUITS.apple then "This is better..."
|
162
197
|
end
|
163
198
|
|
164
199
|
This is because I had trouble overriding the `===` operator of the Symbol class.
|
@@ -169,3 +204,12 @@ Another limitation is the following:
|
|
169
204
|
Car.where(:color => Car::COLORS.red) # good
|
170
205
|
|
171
206
|
You may use the EnumValue object for anything, but don't get smart using the key.
|
207
|
+
If the EnumValue doesn't work in a spot, use #name (in `options_for_select`) or #value (in `update_all`).
|
208
|
+
|
209
|
+
## Also
|
210
|
+
|
211
|
+
EnumValue also works well with [nil_or](https://github.com/toplex/nil_or):
|
212
|
+
|
213
|
+
fruit = FRUITS.apple
|
214
|
+
fruit.nil_or.t
|
215
|
+
=> "Green Apple"
|
data/UNLICENSE
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
This is free and unencumbered software released into the public domain.
|
2
|
+
|
3
|
+
Anyone is free to copy, modify, publish, use, compile, sell, or
|
4
|
+
distribute this software, either in source code form or as a compiled
|
5
|
+
binary, for any purpose, commercial or non-commercial, and by any
|
6
|
+
means.
|
7
|
+
|
8
|
+
In jurisdictions that recognize copyright laws, the author or authors
|
9
|
+
of this software dedicate any and all copyright interest in the
|
10
|
+
software to the public domain. We make this dedication for the benefit
|
11
|
+
of the public at large and to the detriment of our heirs and
|
12
|
+
successors. We intend this dedication to be an overt act of
|
13
|
+
relinquishment in perpetuity of all present and future rights to this
|
14
|
+
software under copyright law.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
19
|
+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
20
|
+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
21
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
|
24
|
+
For more information, please refer to <http://unlicense.org/>
|
data/lib/enum/enum_value.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
class Enum::EnumValue < BasicObject
|
2
2
|
attr_reader :enum, :name, :value
|
3
3
|
|
4
|
-
def initialize(enum, name, value)
|
4
|
+
def initialize(enum, name = nil, value)
|
5
5
|
@enum, @name, @value = enum, name, value
|
6
6
|
end
|
7
7
|
|
@@ -10,36 +10,67 @@ class Enum::EnumValue < BasicObject
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def inspect
|
13
|
+
return @value.inspect if @name.nil?
|
14
|
+
|
13
15
|
"#{@enum.to_s}.#{@name}"
|
14
16
|
end
|
15
17
|
|
16
18
|
def ==(other)
|
19
|
+
return @value == other if @name.nil?
|
20
|
+
|
17
21
|
@name == other or @value == other
|
18
22
|
end
|
19
23
|
|
20
24
|
def ===(other)
|
25
|
+
return @value === other if @name.nil?
|
26
|
+
|
21
27
|
@name === other or @value === other
|
22
28
|
end
|
23
29
|
|
24
|
-
def t(options={})
|
30
|
+
def t(options = {})
|
31
|
+
return @value.t(options) if @name.nil?
|
32
|
+
if not defined?(::I18n)
|
33
|
+
if @name.to_s.respond_to?(:titleize)
|
34
|
+
return @name.to_s.titleize
|
35
|
+
else
|
36
|
+
raise NotImplementedError, "I18n and String#titleize are not available"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
25
40
|
scope_without_klass = "enums.#{const_to_translation(@enum.name)}"
|
26
41
|
if @enum.klass
|
27
42
|
scope_with_klass = "enums.#{const_to_translation(@enum.klass.name)}.#{const_to_translation(@enum.name)}"
|
28
|
-
return "I18n not available: #{scope_with_klass}.#{@name}" unless defined?(::I18n)
|
29
43
|
|
30
|
-
::I18n.t(
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
44
|
+
::I18n.t(
|
45
|
+
@name,
|
46
|
+
# prefer scope with klass
|
47
|
+
options.reverse_merge(
|
48
|
+
scope: scope_with_klass
|
49
|
+
).merge( # our :default is a priority here
|
50
|
+
default: ::I18n.t(
|
51
|
+
@name,
|
52
|
+
# but if not, use without
|
53
|
+
options.reverse_merge(
|
54
|
+
scope: scope_without_klass,
|
55
|
+
# but! if not, return titleize or scope with klass error
|
56
|
+
default: @name.to_s.respond_to?(:titleize) ? @name.to_s.titleize : ::I18n.t(
|
57
|
+
@name,
|
58
|
+
options.reverse_merge(
|
59
|
+
scope: scope_with_klass
|
60
|
+
)
|
61
|
+
)
|
62
|
+
)
|
63
|
+
)
|
64
|
+
)
|
65
|
+
)
|
39
66
|
else
|
40
|
-
|
41
|
-
|
42
|
-
|
67
|
+
::I18n.t(
|
68
|
+
@name,
|
69
|
+
options.reverse_merge(
|
70
|
+
scope: scope_without_klass,
|
71
|
+
default: (@name.to_s.titleize if @name.to_s.respond_to?(:titleize))
|
72
|
+
)
|
73
|
+
)
|
43
74
|
end
|
44
75
|
end
|
45
76
|
|
@@ -19,7 +19,7 @@ module Enum::Helpers::EnumAttribute
|
|
19
19
|
e = const_get(name_or_enum)
|
20
20
|
end
|
21
21
|
# attribute reader
|
22
|
-
define_method(attr) { v = super(); (v.nil? or not e.values.include?(v)) ? v : e[v] }
|
22
|
+
define_method(attr) { v = super(); (v.nil? or not e.values.include?(v)) ? Enum::EnumValue.new(e, v) : e[v] }
|
23
23
|
# attribute writer
|
24
24
|
define_method("#{attr}=") { |v| v.nil? ? super(v) : super(e[v]) }
|
25
25
|
|
data/lib/enum/version.rb
ADDED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yinum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Oded Niv
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-02-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -33,16 +33,19 @@ extensions: []
|
|
33
33
|
extra_rdoc_files: []
|
34
34
|
files:
|
35
35
|
- .gitignore
|
36
|
+
- CHANGELOG.md
|
36
37
|
- Gemfile
|
37
38
|
- Gemfile.lock
|
38
39
|
- README.md
|
39
40
|
- Rakefile
|
41
|
+
- UNLICENSE
|
40
42
|
- lib/enum.rb
|
41
43
|
- lib/enum/enum_value.rb
|
42
44
|
- lib/enum/helpers.rb
|
43
45
|
- lib/enum/helpers/enum_attribute.rb
|
44
46
|
- lib/enum/helpers/enum_column.rb
|
45
47
|
- lib/enum/helpers/enum_generator.rb
|
48
|
+
- lib/enum/version.rb
|
46
49
|
- lib/yinum.rb
|
47
50
|
- spec/lib/enum/enum_value_spec.rb
|
48
51
|
- spec/lib/enum/helpers/enum_attribute_spec.rb
|
@@ -75,10 +78,4 @@ rubygems_version: 2.0.3
|
|
75
78
|
signing_key:
|
76
79
|
specification_version: 4
|
77
80
|
summary: Enum implementation
|
78
|
-
test_files:
|
79
|
-
- spec/lib/enum/enum_value_spec.rb
|
80
|
-
- spec/lib/enum/helpers/enum_attribute_spec.rb
|
81
|
-
- spec/lib/enum/helpers/enum_column_spec.rb
|
82
|
-
- spec/lib/enum/helpers/enum_generator_spec.rb
|
83
|
-
- spec/lib/enum_spec.rb
|
84
|
-
- spec/spec_helper.rb
|
81
|
+
test_files: []
|