str_enum 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/LICENSE.txt +22 -0
- data/README.md +27 -7
- data/lib/str_enum/version.rb +1 -1
- data/lib/str_enum.rb +6 -4
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 50458b9b15471d5a6656996aafd655245fba64fd
|
4
|
+
data.tar.gz: f04c3fa378db71e53714cd844fe99c776e0ca42f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f3eb2d10031cdf8a0b7967b02d2cf4624f0893480b05e8dbec893d1cc00e693a3c1acca73f4fc8fe361f4f4830f98e492f03eaf711bef7d29dfcadb67eae2414
|
7
|
+
data.tar.gz: 62c5e91bc1417967d641aec5865ce2120ad8b2fe01d2b09389bf205cc0778dc84b2a8401e386572f020e6aef9c4f5e5c7d1cc41083a9dcc5c12c2042bed8a46b
|
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2016 Andrew Kane
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
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
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# str_enum
|
2
2
|
|
3
|
-
Don’t
|
3
|
+
Don’t like storing enums as integers in your database? Introducing...
|
4
4
|
|
5
5
|
String enums for Rails!! :tada:
|
6
6
|
|
@@ -24,32 +24,32 @@ class User < ActiveRecord::Base
|
|
24
24
|
end
|
25
25
|
```
|
26
26
|
|
27
|
-
The first value will be the initial value. This gives you
|
27
|
+
The first value will be the initial value. This gives you:
|
28
28
|
|
29
|
-
|
29
|
+
#### Scopes
|
30
30
|
|
31
31
|
```ruby
|
32
32
|
User.active
|
33
33
|
User.archived
|
34
34
|
```
|
35
35
|
|
36
|
-
|
36
|
+
#### Validations
|
37
37
|
|
38
38
|
```ruby
|
39
39
|
user = User.new(status: "unknown")
|
40
40
|
user.valid? # false
|
41
41
|
```
|
42
42
|
|
43
|
-
|
43
|
+
#### Accessor Methods
|
44
44
|
|
45
45
|
```ruby
|
46
46
|
user.active?
|
47
47
|
user.archived?
|
48
48
|
```
|
49
49
|
|
50
|
-
##
|
50
|
+
## Options
|
51
51
|
|
52
|
-
Choose which features you want
|
52
|
+
Choose which features you want with:
|
53
53
|
|
54
54
|
```ruby
|
55
55
|
class User < ActiveRecord::Base
|
@@ -57,6 +57,26 @@ class User < ActiveRecord::Base
|
|
57
57
|
end
|
58
58
|
```
|
59
59
|
|
60
|
+
Prevent name collisions with the `prefix` option.
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
class User < ActiveRecord::Base
|
64
|
+
str_enum :address_status, [:active, :archived], prefix: :address
|
65
|
+
end
|
66
|
+
|
67
|
+
# scopes
|
68
|
+
User.address_active
|
69
|
+
User.address_archived
|
70
|
+
|
71
|
+
# accessor methods
|
72
|
+
user.address_active?
|
73
|
+
user.address_archived?
|
74
|
+
```
|
75
|
+
|
76
|
+
## History
|
77
|
+
|
78
|
+
View the [changelog](https://github.com/ankane/str_enum/blob/master/CHANGELOG.md)
|
79
|
+
|
60
80
|
## Contributing
|
61
81
|
|
62
82
|
Everyone is encouraged to help improve this project. Here are a few ways you can help:
|
data/lib/str_enum/version.rb
CHANGED
data/lib/str_enum.rb
CHANGED
@@ -2,13 +2,15 @@ require "str_enum/version"
|
|
2
2
|
require "active_support"
|
3
3
|
|
4
4
|
module StrEnum
|
5
|
-
def str_enum(column, values, validate: true, scopes: true, accessor_methods: true)
|
5
|
+
def str_enum(column, values, validate: true, scopes: true, accessor_methods: true, prefix: false)
|
6
6
|
values = values.map(&:to_s)
|
7
7
|
validates column, presence: true, inclusion: {in: values} if validate
|
8
8
|
values.each do |value|
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
prefix = column if prefix == true
|
10
|
+
method_name = prefix ? "#{prefix}_#{value}" : value
|
11
|
+
scope method_name, -> { where(column => value) } if scopes && !respond_to?(method_name)
|
12
|
+
if accessor_methods && !method_defined?("#{method_name}?")
|
13
|
+
define_method "#{method_name}?" do
|
12
14
|
read_attribute(column) == value
|
13
15
|
end
|
14
16
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: str_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kane
|
@@ -74,7 +74,9 @@ extensions: []
|
|
74
74
|
extra_rdoc_files: []
|
75
75
|
files:
|
76
76
|
- ".gitignore"
|
77
|
+
- CHANGELOG.md
|
77
78
|
- Gemfile
|
79
|
+
- LICENSE.txt
|
78
80
|
- README.md
|
79
81
|
- Rakefile
|
80
82
|
- lib/str_enum.rb
|