ruby-enum 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -2
- data/CONTRIBUTING.md +125 -0
- data/Gemfile +4 -4
- data/Gemfile.lock +33 -24
- data/LICENSE.md +1 -1
- data/README.md +55 -8
- data/RELEASING.md +67 -0
- data/Rakefile +1 -1
- data/lib/ruby-enum/enum.rb +54 -8
- data/lib/ruby-enum/errors/base.rb +30 -30
- data/lib/ruby-enum/version.rb +1 -1
- data/pkg/ruby-enum-0.4.0.gem +0 -0
- data/spec/ruby-enum/enum_spec.rb +56 -16
- data/spec/ruby-enum/version_spec.rb +1 -1
- data/spec/spec_helper.rb +2 -0
- metadata +14 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9c0c38cc0b3edc3052b171b5dc3aacdee8a3e2f0
|
4
|
+
data.tar.gz: e561a122f1dcdc6b5657d1d004d37d37a00e8108
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 474384b04e35b3ab7e0fb5145cb07c545c12f8dd37df44c1c0c75d465439df575f4aeeb82e0ca2cc71b55dea25af610c08b2b57fcb8760abb5b9cfc40e9ca6ab
|
7
|
+
data.tar.gz: e98254d96a8f3f1af22f1ee3c34ca5d2de116724a2cae4f4ab8ba337a05cd2bf78397a6ae3bbf0a2771f3b76a915865b1cbf8ccdd3b8cfe97ffe84bbae7abe92
|
data/CHANGELOG.md
CHANGED
@@ -1,10 +1,14 @@
|
|
1
|
+
### 0.5.0 (11/20/2015)
|
2
|
+
|
3
|
+
* [#8](https://github.com/dblock/ruby-enum/pull/8): Added `Ruby::Enum#key`, `Ruby::Enum#value`, `Ruby::Enum#key?`, and `Ruby::Enum#value?` - [@dmolesUC3](https://github.com/dmolesUC3).
|
4
|
+
|
1
5
|
### 0.4.0 (6/29/2014)
|
2
6
|
|
3
|
-
* Mixed in `Enumerable` - [@kgann](https://github.com/kgann).
|
7
|
+
* [#5](https://github.com/dblock/ruby-enum/pull/5): Mixed in `Enumerable` - [@kgann](https://github.com/kgann).
|
4
8
|
|
5
9
|
### 0.3.0 (5/19/2014)
|
6
10
|
|
7
|
-
* Added `Ruby::Enum#map` - [@ArnaudRinquin](https://github.com/ArnaudRinquin).
|
11
|
+
* [#4](https://github.com/dblock/ruby-enum/pull/4): Added `Ruby::Enum#map` - [@ArnaudRinquin](https://github.com/ArnaudRinquin).
|
8
12
|
|
9
13
|
### 0.2.1 (5/15/2013)
|
10
14
|
|
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
# Contributing to Ruby-Enum
|
2
|
+
|
3
|
+
This project is work of [many contributors](https://github.com/dblock/ruby-enum/graphs/contributors).
|
4
|
+
|
5
|
+
You're encouraged to submit [pull requests](https://github.com/dblock/ruby-enum/pulls), [propose features and discuss issues](https://github.com/dblock/ruby-enum/issues).
|
6
|
+
|
7
|
+
In the examples below, substitute your Github username for `contributor` in URLs.
|
8
|
+
|
9
|
+
### Fork the Project
|
10
|
+
|
11
|
+
Fork the [project on Github](https://github.com/dblock/ruby-enum) and check out your copy.
|
12
|
+
|
13
|
+
```
|
14
|
+
git clone https://github.com/contributor/ruby-enum.git
|
15
|
+
cd ruby-enum
|
16
|
+
git remote add upstream https://github.com/dblock/ruby-enum.git
|
17
|
+
```
|
18
|
+
|
19
|
+
### Bundle Install and Test
|
20
|
+
|
21
|
+
Ensure that you can build the project and run tests.
|
22
|
+
|
23
|
+
```
|
24
|
+
bundle install
|
25
|
+
bundle exec rake
|
26
|
+
```
|
27
|
+
|
28
|
+
## Contribute Code
|
29
|
+
|
30
|
+
### Create a Topic Branch
|
31
|
+
|
32
|
+
Make sure your fork is up-to-date and create a topic branch for your feature or bug fix.
|
33
|
+
|
34
|
+
```
|
35
|
+
git checkout master
|
36
|
+
git pull upstream master
|
37
|
+
git checkout -b my-feature-branch
|
38
|
+
```
|
39
|
+
|
40
|
+
### Write Tests
|
41
|
+
|
42
|
+
Try to write a test that reproduces the problem you're trying to fix or describes a feature that you want to build. Add tests to [spec](spec).
|
43
|
+
|
44
|
+
We definitely appreciate pull requests that highlight or reproduce a problem, even without a fix.
|
45
|
+
|
46
|
+
### Write Code
|
47
|
+
|
48
|
+
Implement your feature or bug fix.
|
49
|
+
|
50
|
+
Ruby style is enforced with [Rubocop](https://github.com/bbatsov/rubocop). Run `bundle exec rubocop` and fix any style issues highlighted, auto-correct issues when possible with `bundle exec rubocop -a`. To silence generally ingored issues, including line lengths or code complexity metrics, run `bundle exec rubocop --auto-gen-config`.
|
51
|
+
|
52
|
+
Make sure that `bundle exec rake` completes without errors.
|
53
|
+
|
54
|
+
### Write Documentation
|
55
|
+
|
56
|
+
Document any external behavior in the [README](README.md).
|
57
|
+
|
58
|
+
### Update Changelog
|
59
|
+
|
60
|
+
Add a line to [CHANGELOG](CHANGELOG.md) under *Next Release*. Don't remove *Your contribution here*.
|
61
|
+
|
62
|
+
Make it look like every other line, including a link to the issue being fixed, your name and link to your Github account.
|
63
|
+
|
64
|
+
### Commit Changes
|
65
|
+
|
66
|
+
Make sure git knows your name and email address:
|
67
|
+
|
68
|
+
```
|
69
|
+
git config --global user.name "Your Name"
|
70
|
+
git config --global user.email "contributor@example.com"
|
71
|
+
```
|
72
|
+
|
73
|
+
Writing good commit logs is important. A commit log should describe what changed and why.
|
74
|
+
|
75
|
+
```
|
76
|
+
git add ...
|
77
|
+
git commit
|
78
|
+
```
|
79
|
+
|
80
|
+
### Push
|
81
|
+
|
82
|
+
```
|
83
|
+
git push origin my-feature-branch
|
84
|
+
```
|
85
|
+
|
86
|
+
### Make a Pull Request
|
87
|
+
|
88
|
+
Go to https://github.com/contributor/ruby-enum and select your feature branch. Click the 'Pull Request' button and fill out the form. Pull requests are usually reviewed within a few days.
|
89
|
+
|
90
|
+
### Update CHANGELOG Again
|
91
|
+
|
92
|
+
Update the [CHANGELOG](CHANGELOG.md) with the pull request number. A typical entry looks as follows.
|
93
|
+
|
94
|
+
```
|
95
|
+
* [#123](https://github.com/dblock/ruby-enum/pull/123): Reticulated splines - [@contributor](https://github.com/contributor).
|
96
|
+
```
|
97
|
+
|
98
|
+
Amend your previous commit and force push the changes.
|
99
|
+
|
100
|
+
```
|
101
|
+
git commit --amend
|
102
|
+
git push origin my-feature-branch -f
|
103
|
+
```
|
104
|
+
|
105
|
+
### Rebase
|
106
|
+
|
107
|
+
If you've been working on a change for a while, rebase with upstream/master.
|
108
|
+
|
109
|
+
```
|
110
|
+
git fetch upstream
|
111
|
+
git rebase upstream/master
|
112
|
+
git push origin my-feature-branch -f
|
113
|
+
```
|
114
|
+
|
115
|
+
### Check on Your Pull Request
|
116
|
+
|
117
|
+
Go back to your pull request after a few minutes and see whether it passed muster with Travis-CI. Everything should look green, otherwise fix issues and amend your commit as described above.
|
118
|
+
|
119
|
+
### Be Patient
|
120
|
+
|
121
|
+
It's likely that your change will not be merged and that the nitpicky maintainers will ask you to do more, or fix seemingly benign problems. Hang on there!
|
122
|
+
|
123
|
+
## Thank You
|
124
|
+
|
125
|
+
Please do know that we really appreciate and value your time and work. We love you, really.
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,44 +1,53 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
ruby-enum (0.
|
4
|
+
ruby-enum (0.5.0)
|
5
5
|
i18n
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: http://rubygems.org/
|
9
9
|
specs:
|
10
|
-
ast (2.
|
10
|
+
ast (2.1.0)
|
11
|
+
astrolabe (1.3.1)
|
12
|
+
parser (~> 2.2)
|
11
13
|
diff-lcs (1.2.5)
|
12
|
-
i18n (0.
|
13
|
-
|
14
|
-
parser (2.1.9)
|
14
|
+
i18n (0.7.0)
|
15
|
+
parser (2.2.3.0)
|
15
16
|
ast (>= 1.1, < 3.0)
|
16
|
-
|
17
|
-
powerpack (0.0.9)
|
17
|
+
powerpack (0.1.1)
|
18
18
|
rainbow (2.0.0)
|
19
19
|
rake (10.3.1)
|
20
|
-
rspec (
|
21
|
-
rspec-core (~>
|
22
|
-
rspec-expectations (~>
|
23
|
-
rspec-mocks (~>
|
24
|
-
rspec-core (
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
20
|
+
rspec (3.4.0)
|
21
|
+
rspec-core (~> 3.4.0)
|
22
|
+
rspec-expectations (~> 3.4.0)
|
23
|
+
rspec-mocks (~> 3.4.0)
|
24
|
+
rspec-core (3.4.1)
|
25
|
+
rspec-support (~> 3.4.0)
|
26
|
+
rspec-expectations (3.4.0)
|
27
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
28
|
+
rspec-support (~> 3.4.0)
|
29
|
+
rspec-mocks (3.4.0)
|
30
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
31
|
+
rspec-support (~> 3.4.0)
|
32
|
+
rspec-support (3.4.0)
|
33
|
+
rubocop (0.35.1)
|
34
|
+
astrolabe (~> 1.3)
|
35
|
+
parser (>= 2.2.3.0, < 3.0)
|
36
|
+
powerpack (~> 0.1)
|
32
37
|
rainbow (>= 1.99.1, < 3.0)
|
33
|
-
ruby-progressbar (~> 1.
|
34
|
-
|
35
|
-
|
38
|
+
ruby-progressbar (~> 1.7)
|
39
|
+
tins (<= 1.6.0)
|
40
|
+
ruby-progressbar (1.7.5)
|
41
|
+
tins (1.6.0)
|
36
42
|
|
37
43
|
PLATFORMS
|
38
44
|
ruby
|
39
45
|
|
40
46
|
DEPENDENCIES
|
41
47
|
rake
|
42
|
-
rspec
|
43
|
-
rubocop (= 0.
|
48
|
+
rspec (~> 3.4.0)
|
49
|
+
rubocop (= 0.35.1)
|
44
50
|
ruby-enum!
|
51
|
+
|
52
|
+
BUNDLED WITH
|
53
|
+
1.10.6
|
data/LICENSE.md
CHANGED
data/README.md
CHANGED
@@ -1,7 +1,10 @@
|
|
1
1
|
Ruby::Enum
|
2
2
|
==========
|
3
3
|
|
4
|
-
[![
|
4
|
+
[![Gem Version](http://img.shields.io/gem/v/ruby-enum.svg)](http://badge.fury.io/rb/ruby-enum)
|
5
|
+
[![Build Status](http://img.shields.io/travis/dblock/ruby-enum.svg)](https://travis-ci.org/dblock/ruby-enum)
|
6
|
+
[![Dependency Status](https://gemnasium.com/dblock/ruby-enum.svg)](https://gemnasium.com/dblock/ruby-enum)
|
7
|
+
[![Code Climate](https://codeclimate.com/github/dblock/ruby-enum.svg)](https://codeclimate.com/github/dblock/ruby-enum)
|
5
8
|
|
6
9
|
Enum-like behavior for Ruby, heavily inspired by [this](http://www.rubyfleebie.com/enumerations-and-ruby) and improved upon [another blog post](http://code.dblock.org/how-to-define-enums-in-ruby).
|
7
10
|
|
@@ -73,17 +76,61 @@ end
|
|
73
76
|
# => [ [:GREEN, #<Colors:...>], [:RED, #<Colors:...>] ]
|
74
77
|
```
|
75
78
|
|
76
|
-
|
79
|
+
### Several hash-like methods are supported.
|
80
|
+
|
81
|
+
#### Retrieving keys and values
|
82
|
+
|
83
|
+
``` ruby
|
84
|
+
Colors.keys
|
85
|
+
# => [:RED, :GREEN]
|
86
|
+
|
87
|
+
Colors.values
|
88
|
+
# => ["red", "green"]
|
89
|
+
```
|
77
90
|
|
78
|
-
|
91
|
+
#### Mapping keys to values
|
92
|
+
|
93
|
+
``` ruby
|
94
|
+
Colors.key?(:RED)
|
95
|
+
# => true
|
96
|
+
|
97
|
+
Colors.value(:RED)
|
98
|
+
# => "red"
|
99
|
+
|
100
|
+
Colors.key?(:BLUE)
|
101
|
+
# => false
|
102
|
+
|
103
|
+
Colors.value(:BLUE)
|
104
|
+
# => nil
|
105
|
+
```
|
79
106
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
107
|
+
#### Mapping values to keys
|
108
|
+
|
109
|
+
``` ruby
|
110
|
+
Colors.value?('green')
|
111
|
+
# => true
|
112
|
+
|
113
|
+
Colors.key('green')
|
114
|
+
# => :GREEN
|
115
|
+
|
116
|
+
Colors.value?('yellow')
|
117
|
+
# => false
|
118
|
+
|
119
|
+
Colors.key('yellow')
|
120
|
+
# => nil
|
121
|
+
```
|
122
|
+
|
123
|
+
## Contributing
|
124
|
+
|
125
|
+
You're encouraged to contribute to this gem. See [CONTRIBUTING](CONTRIBUTING.md) for details.
|
84
126
|
|
85
127
|
## Copyright and License
|
86
128
|
|
87
|
-
Copyright (c) 2013-
|
129
|
+
Copyright (c) 2013-2015, Daniel Doubrovkine and [Contributors](CHANGELOG.md).
|
88
130
|
|
89
131
|
This project is licensed under the [MIT License](LICENSE.md).
|
132
|
+
|
133
|
+
## Related Projects
|
134
|
+
|
135
|
+
* [typesafe_enum](https://github.com/dmolesUC3/typesafe_enum): Typesafe enums, inspired by Java.
|
136
|
+
* [renum](https://github.com/duelinmarkers/renum): A readable, but terse enum.
|
data/RELEASING.md
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# Releasing Ruby-Enum
|
2
|
+
|
3
|
+
There're no hard rules about when to release ruby-enum. Release bug fixes frequenty, features not so frequently and breaking API changes rarely.
|
4
|
+
|
5
|
+
### Release
|
6
|
+
|
7
|
+
Run tests, check that all tests succeed locally.
|
8
|
+
|
9
|
+
```
|
10
|
+
bundle install
|
11
|
+
rake
|
12
|
+
```
|
13
|
+
|
14
|
+
Check that the last build succeeded in [Travis CI](https://travis-ci.org/dblock/ruby-enum) for all supported platforms.
|
15
|
+
|
16
|
+
Increment the version, modify [lib/ruby-enum/version.rb](lib/ruby-enum/version.rb).
|
17
|
+
|
18
|
+
* Increment the third number if the release has bug fixes and/or very minor features, only (eg. change `0.2.1` to `0.2.2`).
|
19
|
+
* Increment the second number if the release contains major features or breaking API changes (eg. change `0.2.1` to `0.3.0`).
|
20
|
+
|
21
|
+
Change "Next Release" in [CHANGELOG.md](CHANGELOG.md) to the new version.
|
22
|
+
|
23
|
+
```
|
24
|
+
### 0.2.2 (7/10/2015)
|
25
|
+
```
|
26
|
+
|
27
|
+
Remove the line with "Your contribution here.", since there will be no more contributions to this release.
|
28
|
+
|
29
|
+
Commit your changes.
|
30
|
+
|
31
|
+
```
|
32
|
+
git add README.md CHANGELOG.md lib/ruby-enum/version.rb
|
33
|
+
git commit -m "Preparing for release, 0.2.2."
|
34
|
+
git push origin master
|
35
|
+
```
|
36
|
+
|
37
|
+
Release.
|
38
|
+
|
39
|
+
```
|
40
|
+
$ rake release
|
41
|
+
|
42
|
+
ruby-enum 0.2.2 built to pkg/ruby-enum-0.2.2.gem.
|
43
|
+
Tagged v0.2.2.
|
44
|
+
Pushed git commits and tags.
|
45
|
+
Pushed ruby-enum 0.2.2 to rubygems.org.
|
46
|
+
```
|
47
|
+
|
48
|
+
### Prepare for the Next Version
|
49
|
+
|
50
|
+
Add the next release to [CHANGELOG.md](CHANGELOG.md).
|
51
|
+
|
52
|
+
```
|
53
|
+
Next Release
|
54
|
+
============
|
55
|
+
|
56
|
+
* Your contribution here.
|
57
|
+
```
|
58
|
+
|
59
|
+
Increment the third version number in [lib/ruby-enum/version.rb](lib/ruby-enum/version.rb).
|
60
|
+
|
61
|
+
Comit your changes.
|
62
|
+
|
63
|
+
```
|
64
|
+
git add CHANGELOG.md lib/ruby-enum/version.rb
|
65
|
+
git commit -m "Preparing for next development iteration, 0.2.3."
|
66
|
+
git push origin master
|
67
|
+
```
|
data/Rakefile
CHANGED
data/lib/ruby-enum/enum.rb
CHANGED
@@ -20,14 +20,17 @@ module Ruby
|
|
20
20
|
# [value] Enumerator value.
|
21
21
|
def define(key, value)
|
22
22
|
@_enum_hash ||= {}
|
23
|
-
@
|
23
|
+
@_enums_by_value ||= {}
|
24
|
+
new_instance = new(key, value)
|
25
|
+
@_enum_hash[key] = new_instance
|
26
|
+
@_enums_by_value[value] = new_instance
|
24
27
|
end
|
25
28
|
|
26
29
|
def const_missing(key)
|
27
30
|
if @_enum_hash[key]
|
28
31
|
@_enum_hash[key].value
|
29
32
|
else
|
30
|
-
fail Ruby::Enum::Errors::UninitializedConstantError
|
33
|
+
fail Ruby::Enum::Errors::UninitializedConstantError, name: name, key: key
|
31
34
|
end
|
32
35
|
end
|
33
36
|
|
@@ -37,20 +40,63 @@ module Ruby
|
|
37
40
|
@_enum_hash.each(&block)
|
38
41
|
end
|
39
42
|
|
40
|
-
# Attempt to parse an
|
43
|
+
# Attempt to parse an enum key and return the
|
44
|
+
# corresponding value.
|
41
45
|
#
|
42
46
|
# === Parameters
|
43
|
-
# [
|
47
|
+
# [k] The key string to parse.
|
44
48
|
#
|
45
|
-
# Returns
|
46
|
-
def parse(
|
47
|
-
|
49
|
+
# Returns the corresponding value or nil.
|
50
|
+
def parse(k)
|
51
|
+
k = k.to_s.upcase
|
48
52
|
each do |key, enum|
|
49
|
-
return enum.value if key.to_s.upcase ==
|
53
|
+
return enum.value if key.to_s.upcase == k
|
50
54
|
end
|
51
55
|
nil
|
52
56
|
end
|
53
57
|
|
58
|
+
# Whether the specified key exists in this enum.
|
59
|
+
#
|
60
|
+
# === Parameters
|
61
|
+
# [k] The string key to check.
|
62
|
+
#
|
63
|
+
# Returns true if the key exists, false otherwise.
|
64
|
+
def key?(k)
|
65
|
+
@_enum_hash.key?(k)
|
66
|
+
end
|
67
|
+
|
68
|
+
# Gets the string value for the specified key.
|
69
|
+
#
|
70
|
+
# === Parameters
|
71
|
+
# [k] The key symbol to get the value for.
|
72
|
+
#
|
73
|
+
# Returns the corresponding enum instance or nil.
|
74
|
+
def value(k)
|
75
|
+
enum = @_enum_hash[k]
|
76
|
+
enum.value if enum
|
77
|
+
end
|
78
|
+
|
79
|
+
# Whether the specified value exists in this enum.
|
80
|
+
#
|
81
|
+
# === Parameters
|
82
|
+
# [k] The string value to check.
|
83
|
+
#
|
84
|
+
# Returns true if the value exists, false otherwise.
|
85
|
+
def value?(v)
|
86
|
+
@_enums_by_value.key?(v)
|
87
|
+
end
|
88
|
+
|
89
|
+
# Gets the key symbol for the specified value.
|
90
|
+
#
|
91
|
+
# === Parameters
|
92
|
+
# [v] The string value to parse.
|
93
|
+
#
|
94
|
+
# Returns the corresponding key symbol or nil.
|
95
|
+
def key(v)
|
96
|
+
enum = @_enums_by_value[v]
|
97
|
+
enum.key if enum
|
98
|
+
end
|
99
|
+
|
54
100
|
# Returns all enum keys.
|
55
101
|
def keys
|
56
102
|
@_enum_hash.values.map(&:key)
|
@@ -21,54 +21,54 @@ module Ruby
|
|
21
21
|
@resolution = create_resolution(key, attributes)
|
22
22
|
|
23
23
|
"\nProblem:\n #{@problem}"\
|
24
|
-
"\nSummary:\n #{@summary}" +
|
24
|
+
"\nSummary:\n #{@summary}" + "\nResolution:\n #{@resolution}"
|
25
25
|
end
|
26
26
|
|
27
27
|
private
|
28
28
|
|
29
29
|
BASE_KEY = 'ruby.enum.errors.messages' #:nodoc:
|
30
30
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
31
|
+
# Given the key of the specific error and the options hash, translate the
|
32
|
+
# message.
|
33
|
+
#
|
34
|
+
# === Parameters
|
35
|
+
# [key] The key of the error in the locales.
|
36
|
+
# [options] The objects to pass to create the message.
|
37
|
+
#
|
38
|
+
# Returns a localized error message string.
|
39
39
|
def translate(key, options)
|
40
40
|
::I18n.translate("#{BASE_KEY}.#{key}", { locale: :en }.merge(options)).strip
|
41
41
|
end
|
42
42
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
43
|
+
# Create the problem.
|
44
|
+
#
|
45
|
+
# === Parameters
|
46
|
+
# [key] The error key.
|
47
|
+
# [attributes] The attributes to interpolate.
|
48
|
+
#
|
49
|
+
# Returns the problem.
|
50
50
|
def create_problem(key, attributes)
|
51
51
|
translate("#{key}.message", attributes)
|
52
52
|
end
|
53
53
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
54
|
+
# Create the summary.
|
55
|
+
#
|
56
|
+
# === Parameters
|
57
|
+
# [key] The error key.
|
58
|
+
# [attributes] The attributes to interpolate.
|
59
|
+
#
|
60
|
+
# Returns the summary.
|
61
61
|
def create_summary(key, attributes)
|
62
62
|
translate("#{key}.summary", attributes)
|
63
63
|
end
|
64
64
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
65
|
+
# Create the resolution.
|
66
|
+
#
|
67
|
+
# === Parameters
|
68
|
+
# [key] The error key.
|
69
|
+
# [attributes] The attributes to interpolate.
|
70
|
+
#
|
71
|
+
# Returns the resolution.
|
72
72
|
def create_resolution(key, attributes)
|
73
73
|
translate("#{key}.resolution", attributes)
|
74
74
|
end
|
data/lib/ruby-enum/version.rb
CHANGED
Binary file
|
data/spec/ruby-enum/enum_spec.rb
CHANGED
@@ -9,11 +9,11 @@ end
|
|
9
9
|
|
10
10
|
describe Ruby::Enum do
|
11
11
|
it 'returns an enum value' do
|
12
|
-
Colors::RED.
|
13
|
-
Colors::GREEN.
|
12
|
+
expect(Colors::RED).to eq 'red'
|
13
|
+
expect(Colors::GREEN).to eq 'green'
|
14
14
|
end
|
15
15
|
it 'raises UninitializedConstantError on an invalid constant' do
|
16
|
-
expect { Colors::ANYTHING }.to raise_error Ruby::Enum::Errors::UninitializedConstantError
|
16
|
+
expect { Colors::ANYTHING }.to raise_error Ruby::Enum::Errors::UninitializedConstantError, /The constant Colors::ANYTHING has not been defined./
|
17
17
|
end
|
18
18
|
context '#each' do
|
19
19
|
it 'iterates over constants' do
|
@@ -25,9 +25,9 @@ describe Ruby::Enum do
|
|
25
25
|
enum_keys << enum.key
|
26
26
|
enum_values << enum.value
|
27
27
|
end
|
28
|
-
keys.
|
29
|
-
enum_keys.
|
30
|
-
enum_values.
|
28
|
+
expect(keys).to eq [:RED, :GREEN]
|
29
|
+
expect(enum_keys).to eq [:RED, :GREEN]
|
30
|
+
expect(enum_values).to eq %w(red green)
|
31
31
|
end
|
32
32
|
end
|
33
33
|
context '#map' do
|
@@ -35,38 +35,78 @@ describe Ruby::Enum do
|
|
35
35
|
key_key_values = Colors.map do |key, enum|
|
36
36
|
[key, enum.key, enum.value]
|
37
37
|
end
|
38
|
-
key_key_values.count.
|
39
|
-
key_key_values[0].
|
40
|
-
key_key_values[1].
|
38
|
+
expect(key_key_values.count).to eq 2
|
39
|
+
expect(key_key_values[0]).to eq [:RED, :RED, 'red']
|
40
|
+
expect(key_key_values[1]).to eq [:GREEN, :GREEN, 'green']
|
41
41
|
end
|
42
42
|
end
|
43
43
|
context '#parse' do
|
44
44
|
it 'parses exact value' do
|
45
|
-
Colors.parse('red').
|
45
|
+
expect(Colors.parse('red')).to eq(Colors::RED)
|
46
46
|
end
|
47
47
|
it 'is case-insensitive' do
|
48
|
-
Colors.parse('ReD').
|
48
|
+
expect(Colors.parse('ReD')).to eq(Colors::RED)
|
49
49
|
end
|
50
50
|
it 'returns nil for a null value' do
|
51
|
-
Colors.parse(nil).
|
51
|
+
expect(Colors.parse(nil)).to be_nil
|
52
52
|
end
|
53
53
|
it 'returns nil for an invalid value' do
|
54
|
-
Colors.parse('invalid').
|
54
|
+
expect(Colors.parse('invalid')).to be_nil
|
55
|
+
end
|
56
|
+
end
|
57
|
+
context '#key?' do
|
58
|
+
it 'returns true for valid keys' do
|
59
|
+
Colors.keys.each do |key|
|
60
|
+
expect(Colors.key?(key)).to eq(true)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
it 'returns false for invalid keys' do
|
64
|
+
expect(Colors.key?(:NOT_A_KEY)).to eq(false)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
context '#value' do
|
68
|
+
it 'returns string values for keys' do
|
69
|
+
Colors.each do |key, enum|
|
70
|
+
expect(Colors.value(key)).to eq(enum.value)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
it 'returns nil for an invalid key' do
|
74
|
+
expect(Colors.value(:NOT_A_KEY)).to be_nil
|
75
|
+
end
|
76
|
+
end
|
77
|
+
context '#value?' do
|
78
|
+
it 'returns true for valid values' do
|
79
|
+
Colors.values.each do |value|
|
80
|
+
expect(Colors.value?(value)).to eq(true)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
it 'returns false for invalid values' do
|
84
|
+
expect(Colors.value?('I am not a value')).to eq(false)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
context '#key' do
|
88
|
+
it 'returns enum instances for values' do
|
89
|
+
Colors.each do |_, enum|
|
90
|
+
expect(Colors.key(enum.value)).to eq(enum.key)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
it 'returns nil for an invalid value' do
|
94
|
+
expect(Colors.key('invalid')).to be_nil
|
55
95
|
end
|
56
96
|
end
|
57
97
|
context '#keys' do
|
58
98
|
it 'returns keys' do
|
59
|
-
Colors.keys.
|
99
|
+
expect(Colors.keys).to eq([:RED, :GREEN])
|
60
100
|
end
|
61
101
|
end
|
62
102
|
context '#values' do
|
63
103
|
it 'returns values' do
|
64
|
-
Colors.values.
|
104
|
+
expect(Colors.values).to eq(%w(red green))
|
65
105
|
end
|
66
106
|
end
|
67
107
|
context '#to_h' do
|
68
108
|
it 'returns a hash of key:values' do
|
69
|
-
Colors.to_h.
|
109
|
+
expect(Colors.to_h).to eq(RED: 'red', GREEN: 'green')
|
70
110
|
end
|
71
111
|
end
|
72
112
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,27 +1,27 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Doubrovkine
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-11-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: i18n
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
description:
|
@@ -31,19 +31,22 @@ extensions: []
|
|
31
31
|
extra_rdoc_files: []
|
32
32
|
files:
|
33
33
|
- CHANGELOG.md
|
34
|
+
- CONTRIBUTING.md
|
34
35
|
- Gemfile
|
35
36
|
- Gemfile.lock
|
37
|
+
- LICENSE.md
|
38
|
+
- README.md
|
39
|
+
- RELEASING.md
|
40
|
+
- Rakefile
|
36
41
|
- lib/config/locales/en.yml
|
42
|
+
- lib/ruby-enum.rb
|
37
43
|
- lib/ruby-enum/enum.rb
|
38
44
|
- lib/ruby-enum/errors/base.rb
|
39
45
|
- lib/ruby-enum/errors/uninitialized_constant_error.rb
|
40
46
|
- lib/ruby-enum/version.rb
|
41
|
-
- lib/ruby-enum.rb
|
42
47
|
- lib/ruby_enum.rb
|
43
|
-
- LICENSE.md
|
44
48
|
- pkg/ruby-enum-0.3.0.gem
|
45
|
-
-
|
46
|
-
- README.md
|
49
|
+
- pkg/ruby-enum-0.4.0.gem
|
47
50
|
- ruby-enum.gemspec
|
48
51
|
- spec/ruby-enum/enum_spec.rb
|
49
52
|
- spec/ruby-enum/version_spec.rb
|
@@ -58,17 +61,17 @@ require_paths:
|
|
58
61
|
- lib
|
59
62
|
required_ruby_version: !ruby/object:Gem::Requirement
|
60
63
|
requirements:
|
61
|
-
- -
|
64
|
+
- - ">="
|
62
65
|
- !ruby/object:Gem::Version
|
63
66
|
version: '0'
|
64
67
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
68
|
requirements:
|
66
|
-
- -
|
69
|
+
- - ">="
|
67
70
|
- !ruby/object:Gem::Version
|
68
71
|
version: 1.3.6
|
69
72
|
requirements: []
|
70
73
|
rubyforge_project:
|
71
|
-
rubygems_version: 2.
|
74
|
+
rubygems_version: 2.4.8
|
72
75
|
signing_key:
|
73
76
|
specification_version: 4
|
74
77
|
summary: Enum-like behavior for Ruby.
|