ruby-enum 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +2 -2
- data/Gemfile +1 -0
- data/Gemfile.lock +27 -11
- data/README.md +12 -0
- data/Rakefile +5 -2
- data/lib/ruby-enum.rb +0 -1
- data/lib/ruby-enum/enum.rb +13 -11
- data/lib/ruby-enum/errors/base.rb +16 -19
- data/lib/ruby-enum/errors/uninitialized_constant_error.rb +2 -2
- data/lib/ruby-enum/version.rb +1 -1
- data/ruby-enum.gemspec +9 -9
- data/spec/ruby-enum/enum_spec.rb +38 -28
- data/spec/ruby-enum/version_spec.rb +1 -1
- data/spec/spec_helper.rb +0 -1
- metadata +9 -18
- data/pkg/ruby-enum-0.2.0.gem +0 -0
- data/pkg/ruby-enum-0.3.0.gem +0 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1e0ced60d6ed9e9fb379e0804f7483d97f8469b2
|
4
|
+
data.tar.gz: 8a67751a1fd7777b39e5c1be070ab498875872a7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f4978a831b5fe81cf79abb6c1568b681e4750a8f6b50a904c32f5d5e9fe2dd7116e6935046f30956d241a93f53e462e20bf66be4fdb72f77ffe47d2773bf41c5
|
7
|
+
data.tar.gz: 8f76f84efced4d354aa73bef8d3da875d2543cc4683bfe5db44adf23c1e748d62e5f5bdcfd0792e1e0b218f7f9c91a676782e9ff35dbb8b719c38df40c51d68c
|
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,23 +1,38 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
ruby-enum (0.
|
4
|
+
ruby-enum (0.3.0)
|
5
5
|
i18n
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: http://rubygems.org/
|
9
9
|
specs:
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
10
|
+
ast (2.0.0)
|
11
|
+
diff-lcs (1.2.5)
|
12
|
+
i18n (0.6.9)
|
13
|
+
json (1.8.1)
|
14
|
+
parser (2.1.9)
|
15
|
+
ast (>= 1.1, < 3.0)
|
16
|
+
slop (~> 3.4, >= 3.4.5)
|
17
|
+
powerpack (0.0.9)
|
18
|
+
rainbow (2.0.0)
|
19
|
+
rake (10.3.1)
|
20
|
+
rspec (2.14.1)
|
21
|
+
rspec-core (~> 2.14.0)
|
22
|
+
rspec-expectations (~> 2.14.0)
|
23
|
+
rspec-mocks (~> 2.14.0)
|
24
|
+
rspec-core (2.14.8)
|
25
|
+
rspec-expectations (2.14.5)
|
19
26
|
diff-lcs (>= 1.1.3, < 2.0)
|
20
|
-
rspec-mocks (2.
|
27
|
+
rspec-mocks (2.14.6)
|
28
|
+
rubocop (0.21.0)
|
29
|
+
json (>= 1.7.7, < 2)
|
30
|
+
parser (~> 2.1.9)
|
31
|
+
powerpack (~> 0.0.6)
|
32
|
+
rainbow (>= 1.99.1, < 3.0)
|
33
|
+
ruby-progressbar (~> 1.4)
|
34
|
+
ruby-progressbar (1.5.1)
|
35
|
+
slop (3.5.0)
|
21
36
|
|
22
37
|
PLATFORMS
|
23
38
|
ruby
|
@@ -25,4 +40,5 @@ PLATFORMS
|
|
25
40
|
DEPENDENCIES
|
26
41
|
rake
|
27
42
|
rspec
|
43
|
+
rubocop (= 0.21.0)
|
28
44
|
ruby-enum!
|
data/README.md
CHANGED
@@ -36,6 +36,18 @@ Colors.each do |key, enum|
|
|
36
36
|
end
|
37
37
|
```
|
38
38
|
|
39
|
+
### Mapping
|
40
|
+
|
41
|
+
``` ruby
|
42
|
+
Colors.map do |key, enum|
|
43
|
+
# key and enum.key is :RED, :GREEN
|
44
|
+
# enum.value is "red", "green"
|
45
|
+
[enum.value, key]
|
46
|
+
end
|
47
|
+
|
48
|
+
# => [ ['red', :RED], ['green', :GREEN] ]
|
49
|
+
```
|
50
|
+
|
39
51
|
## Contributing
|
40
52
|
|
41
53
|
You're encouraged to contribute to this gem.
|
data/Rakefile
CHANGED
@@ -7,7 +7,10 @@ require 'rspec/core'
|
|
7
7
|
require 'rspec/core/rake_task'
|
8
8
|
|
9
9
|
RSpec::Core::RakeTask.new(:spec) do |spec|
|
10
|
-
spec.pattern = FileList[
|
10
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
11
11
|
end
|
12
12
|
|
13
|
-
|
13
|
+
require 'rubocop/rake_task'
|
14
|
+
Rubocop::RakeTask.new(:rubocop)
|
15
|
+
|
16
|
+
task default: [:rubocop, :spec]
|
data/lib/ruby-enum.rb
CHANGED
data/lib/ruby-enum/enum.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
module Ruby
|
2
2
|
module Enum
|
3
|
-
|
4
3
|
attr_reader :key, :value
|
5
4
|
|
6
5
|
def initialize(key, value)
|
@@ -13,7 +12,6 @@ module Ruby
|
|
13
12
|
end
|
14
13
|
|
15
14
|
module ClassMethods
|
16
|
-
|
17
15
|
# Define an enumerated value.
|
18
16
|
#
|
19
17
|
# === Parameters
|
@@ -21,25 +19,33 @@ module Ruby
|
|
21
19
|
# [value] Enumerator value.
|
22
20
|
def define(key, value)
|
23
21
|
@_enum_hash ||= {}
|
24
|
-
@_enum_hash[key] =
|
22
|
+
@_enum_hash[key] = new(key, value)
|
25
23
|
end
|
26
24
|
|
27
25
|
def const_missing(key)
|
28
26
|
if @_enum_hash[key]
|
29
27
|
@_enum_hash[key].value
|
30
28
|
else
|
31
|
-
|
29
|
+
fail Ruby::Enum::Errors::UninitializedConstantError.new(name: name, key: key)
|
32
30
|
end
|
33
31
|
end
|
34
32
|
|
35
33
|
# Iterate over all enumerated values.
|
36
34
|
# Yields a key and an enumerated instance.
|
37
|
-
def each(&
|
35
|
+
def each(&_block)
|
38
36
|
@_enum_hash.each do |key, value|
|
39
37
|
yield key, value
|
40
38
|
end
|
41
39
|
end
|
42
40
|
|
41
|
+
# Map all enumerated values.
|
42
|
+
# Yields a key and an enumerated instance.
|
43
|
+
def map(&_block)
|
44
|
+
@_enum_hash.map do |key, value|
|
45
|
+
yield key, value
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
43
49
|
# Attempt to parse an enumerated value.
|
44
50
|
#
|
45
51
|
# === Parameters
|
@@ -49,9 +55,7 @@ module Ruby
|
|
49
55
|
def parse(s)
|
50
56
|
s = s.to_s.upcase
|
51
57
|
each do |key, enum|
|
52
|
-
if key.to_s.upcase == s
|
53
|
-
return enum.value
|
54
|
-
end
|
58
|
+
return enum.value if key.to_s.upcase == s
|
55
59
|
end
|
56
60
|
nil
|
57
61
|
end
|
@@ -68,11 +72,9 @@ module Ruby
|
|
68
72
|
|
69
73
|
def to_h
|
70
74
|
Hash[@_enum_hash.map do |key, enum|
|
71
|
-
[
|
75
|
+
[key, enum.value]
|
72
76
|
end]
|
73
77
|
end
|
74
|
-
|
75
78
|
end
|
76
|
-
|
77
79
|
end
|
78
80
|
end
|
@@ -2,7 +2,6 @@ module Ruby
|
|
2
2
|
module Enum
|
3
3
|
module Errors
|
4
4
|
class Base < StandardError
|
5
|
-
|
6
5
|
# Problem occurred.
|
7
6
|
attr_reader :problem
|
8
7
|
|
@@ -21,14 +20,13 @@ module Ruby
|
|
21
20
|
@summary = create_summary(key, attributes)
|
22
21
|
@resolution = create_resolution(key, attributes)
|
23
22
|
|
24
|
-
"\nProblem:\n #{@problem}"
|
25
|
-
"\nSummary:\n #{@summary}"+
|
26
|
-
"\nResolution:\n #{@resolution}"
|
23
|
+
"\nProblem:\n #{@problem}"\
|
24
|
+
"\nSummary:\n #{@summary}" + "\nResolution:\n #{@resolution}"
|
27
25
|
end
|
28
26
|
|
29
27
|
private
|
30
28
|
|
31
|
-
|
29
|
+
BASE_KEY = 'ruby.enum.errors.messages' #:nodoc:
|
32
30
|
|
33
31
|
# Given the key of the specific error and the options hash, translate the
|
34
32
|
# message.
|
@@ -38,9 +36,9 @@ module Ruby
|
|
38
36
|
# [options] The objects to pass to create the message.
|
39
37
|
#
|
40
38
|
# Returns a localized error message string.
|
41
|
-
|
42
|
-
|
43
|
-
|
39
|
+
def translate(key, options)
|
40
|
+
::I18n.translate("#{BASE_KEY}.#{key}", { locale: :en }.merge(options)).strip
|
41
|
+
end
|
44
42
|
|
45
43
|
# Create the problem.
|
46
44
|
#
|
@@ -49,9 +47,9 @@ module Ruby
|
|
49
47
|
# [attributes] The attributes to interpolate.
|
50
48
|
#
|
51
49
|
# Returns the problem.
|
52
|
-
|
53
|
-
|
54
|
-
|
50
|
+
def create_problem(key, attributes)
|
51
|
+
translate("#{key}.message", attributes)
|
52
|
+
end
|
55
53
|
|
56
54
|
# Create the summary.
|
57
55
|
#
|
@@ -60,9 +58,9 @@ module Ruby
|
|
60
58
|
# [attributes] The attributes to interpolate.
|
61
59
|
#
|
62
60
|
# Returns the summary.
|
63
|
-
|
64
|
-
|
65
|
-
|
61
|
+
def create_summary(key, attributes)
|
62
|
+
translate("#{key}.summary", attributes)
|
63
|
+
end
|
66
64
|
|
67
65
|
# Create the resolution.
|
68
66
|
#
|
@@ -71,11 +69,10 @@ module Ruby
|
|
71
69
|
# [attributes] The attributes to interpolate.
|
72
70
|
#
|
73
71
|
# Returns the resolution.
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
72
|
+
def create_resolution(key, attributes)
|
73
|
+
translate("#{key}.resolution", attributes)
|
74
|
+
end
|
78
75
|
end
|
79
76
|
end
|
80
77
|
end
|
81
|
-
end
|
78
|
+
end
|
data/lib/ruby-enum/version.rb
CHANGED
data/ruby-enum.gemspec
CHANGED
@@ -1,17 +1,17 @@
|
|
1
|
-
|
1
|
+
$LOAD_PATH.push File.expand_path('../lib', __FILE__)
|
2
2
|
require 'ruby-enum/version'
|
3
3
|
|
4
4
|
Gem::Specification.new do |s|
|
5
|
-
s.name =
|
5
|
+
s.name = 'ruby-enum'
|
6
6
|
s.version = Ruby::Enum::VERSION
|
7
|
-
s.authors = [
|
8
|
-
s.email =
|
7
|
+
s.authors = ['Daniel Doubrovkine']
|
8
|
+
s.email = 'dblock@dblock.org'
|
9
9
|
s.platform = Gem::Platform::RUBY
|
10
10
|
s.required_rubygems_version = '>= 1.3.6'
|
11
11
|
s.files = Dir['**/*']
|
12
|
-
s.require_paths = [
|
13
|
-
s.homepage =
|
14
|
-
s.licenses = [
|
15
|
-
s.summary =
|
16
|
-
s.add_dependency
|
12
|
+
s.require_paths = ['lib']
|
13
|
+
s.homepage = 'http://github.com/dblock/ruby-enum'
|
14
|
+
s.licenses = ['MIT']
|
15
|
+
s.summary = 'Enum-like behavior for Ruby.'
|
16
|
+
s.add_dependency 'i18n'
|
17
17
|
end
|
data/spec/ruby-enum/enum_spec.rb
CHANGED
@@ -3,20 +3,20 @@ require 'spec_helper'
|
|
3
3
|
class Colors
|
4
4
|
include Ruby::Enum
|
5
5
|
|
6
|
-
define :RED,
|
7
|
-
define :GREEN,
|
6
|
+
define :RED, 'red'
|
7
|
+
define :GREEN, 'green'
|
8
8
|
end
|
9
9
|
|
10
10
|
describe Ruby::Enum do
|
11
|
-
it
|
12
|
-
Colors::RED.should eq
|
13
|
-
Colors::GREEN.should eq
|
11
|
+
it 'returns an enum value' do
|
12
|
+
Colors::RED.should eq 'red'
|
13
|
+
Colors::GREEN.should eq 'green'
|
14
14
|
end
|
15
|
-
it
|
15
|
+
it 'raises UninitializedConstantError on an invalid constant' do
|
16
16
|
expect { Colors::ANYTHING }.to raise_error Ruby::Enum::Errors::UninitializedConstantError
|
17
17
|
end
|
18
|
-
context
|
19
|
-
it
|
18
|
+
context '#each' do
|
19
|
+
it 'iterates over constants' do
|
20
20
|
keys = []
|
21
21
|
enum_keys = []
|
22
22
|
enum_values = []
|
@@ -25,38 +25,48 @@ describe Ruby::Enum do
|
|
25
25
|
enum_keys << enum.key
|
26
26
|
enum_values << enum.value
|
27
27
|
end
|
28
|
-
keys.should
|
29
|
-
enum_keys.should
|
30
|
-
enum_values.should
|
28
|
+
keys.should eq [:RED, :GREEN]
|
29
|
+
enum_keys.should eq [:RED, :GREEN]
|
30
|
+
enum_values.should eq %w(red green)
|
31
31
|
end
|
32
32
|
end
|
33
|
-
context
|
34
|
-
it
|
35
|
-
Colors.
|
33
|
+
context '#map' do
|
34
|
+
it 'maps constants' do
|
35
|
+
key_key_values = Colors.map do |key, enum|
|
36
|
+
[key, enum.key, enum.value]
|
37
|
+
end
|
38
|
+
key_key_values.count.should eq 2
|
39
|
+
key_key_values[0].should eq [:RED, :RED, 'red']
|
40
|
+
key_key_values[1].should eq [:GREEN, :GREEN, 'green']
|
41
|
+
end
|
42
|
+
end
|
43
|
+
context '#parse' do
|
44
|
+
it 'parses exact value' do
|
45
|
+
Colors.parse('red').should == Colors::RED
|
36
46
|
end
|
37
|
-
it
|
38
|
-
Colors.parse(
|
47
|
+
it 'is case-insensitive' do
|
48
|
+
Colors.parse('ReD').should == Colors::RED
|
39
49
|
end
|
40
|
-
it
|
50
|
+
it 'returns nil for a null value' do
|
41
51
|
Colors.parse(nil).should be_nil
|
42
52
|
end
|
43
|
-
it
|
44
|
-
Colors.parse(
|
53
|
+
it 'returns nil for an invalid value' do
|
54
|
+
Colors.parse('invalid').should be_nil
|
45
55
|
end
|
46
56
|
end
|
47
|
-
context
|
48
|
-
it
|
49
|
-
Colors.keys.should == [
|
57
|
+
context '#keys' do
|
58
|
+
it 'returns keys' do
|
59
|
+
Colors.keys.should == [:RED, :GREEN]
|
50
60
|
end
|
51
61
|
end
|
52
|
-
context
|
53
|
-
it
|
54
|
-
Colors.values.should ==
|
62
|
+
context '#values' do
|
63
|
+
it 'returns values' do
|
64
|
+
Colors.values.should == %w(red green)
|
55
65
|
end
|
56
66
|
end
|
57
|
-
context
|
58
|
-
it
|
59
|
-
Colors.to_h.should == { :
|
67
|
+
context '#to_h' do
|
68
|
+
it 'returns a hash of key:values' do
|
69
|
+
Colors.to_h.should == { RED: 'red', GREEN: 'green' }
|
60
70
|
end
|
61
71
|
end
|
62
72
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,30 +1,27 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.3.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Daniel Doubrovkine
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2014-05-19 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: i18n
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - '>='
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - '>='
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '0'
|
30
27
|
description:
|
@@ -44,8 +41,6 @@ files:
|
|
44
41
|
- lib/ruby-enum.rb
|
45
42
|
- lib/ruby_enum.rb
|
46
43
|
- LICENSE.md
|
47
|
-
- pkg/ruby-enum-0.2.0.gem
|
48
|
-
- pkg/ruby-enum-0.3.0.gem
|
49
44
|
- Rakefile
|
50
45
|
- README.md
|
51
46
|
- ruby-enum.gemspec
|
@@ -55,29 +50,25 @@ files:
|
|
55
50
|
homepage: http://github.com/dblock/ruby-enum
|
56
51
|
licenses:
|
57
52
|
- MIT
|
53
|
+
metadata: {}
|
58
54
|
post_install_message:
|
59
55
|
rdoc_options: []
|
60
56
|
require_paths:
|
61
57
|
- lib
|
62
58
|
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
-
none: false
|
64
59
|
requirements:
|
65
|
-
- -
|
60
|
+
- - '>='
|
66
61
|
- !ruby/object:Gem::Version
|
67
62
|
version: '0'
|
68
|
-
segments:
|
69
|
-
- 0
|
70
|
-
hash: 4593317306755494398
|
71
63
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
-
none: false
|
73
64
|
requirements:
|
74
|
-
- -
|
65
|
+
- - '>='
|
75
66
|
- !ruby/object:Gem::Version
|
76
67
|
version: 1.3.6
|
77
68
|
requirements: []
|
78
69
|
rubyforge_project:
|
79
|
-
rubygems_version:
|
70
|
+
rubygems_version: 2.0.14
|
80
71
|
signing_key:
|
81
|
-
specification_version:
|
72
|
+
specification_version: 4
|
82
73
|
summary: Enum-like behavior for Ruby.
|
83
74
|
test_files: []
|
data/pkg/ruby-enum-0.2.0.gem
DELETED
Binary file
|
data/pkg/ruby-enum-0.3.0.gem
DELETED
Binary file
|