ruby-enum 0.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.
- data/.gitignore +2 -0
- data/.rspec +2 -0
- data/.travis.yml +6 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +6 -0
- data/LICENSE.md +22 -0
- data/README.md +33 -0
- data/Rakefile +13 -0
- data/lib/ruby-enum.rb +3 -0
- data/lib/ruby-enum/enum.rb +49 -0
- data/lib/ruby-enum/version.rb +5 -0
- data/lib/ruby_enum.rb +1 -0
- data/ruby-enum.gemspec +16 -0
- data/spec/ruby-enum/enum_spec.rb +29 -0
- data/spec/ruby-enum/version_spec.rb +7 -0
- data/spec/spec_helper.rb +6 -0
- metadata +64 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2013 Daniel Doubrovkine.
|
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
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
Ruby::Enum
|
2
|
+
==========
|
3
|
+
|
4
|
+
[](https://travis-ci.org/dblock/ruby-enum)
|
5
|
+
|
6
|
+
A handy library for defining enums in Ruby.
|
7
|
+
|
8
|
+
### Usage
|
9
|
+
|
10
|
+
``` ruby
|
11
|
+
class Colors
|
12
|
+
include Ruby::Enum
|
13
|
+
|
14
|
+
define :RED, "red"
|
15
|
+
define :GREEN, "green"
|
16
|
+
end
|
17
|
+
```
|
18
|
+
|
19
|
+
### Contributing
|
20
|
+
|
21
|
+
You're encouraged to contribute to this gem.
|
22
|
+
|
23
|
+
* Fork this project.
|
24
|
+
* Make changes, write tests.
|
25
|
+
* Updated CHANGELOG.
|
26
|
+
* Make a pull request, bonus points for topic branches.
|
27
|
+
|
28
|
+
### Copyright and License
|
29
|
+
|
30
|
+
Copyright Daniel Doubrovkine and Contributors, 2013
|
31
|
+
|
32
|
+
[MIT License](LICENSE.md)
|
33
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/gem_tasks'
|
3
|
+
|
4
|
+
Bundler.setup :default, :development
|
5
|
+
|
6
|
+
require 'rspec/core'
|
7
|
+
require 'rspec/core/rake_task'
|
8
|
+
|
9
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
10
|
+
spec.pattern = FileList["spec/**/*_spec.rb"]
|
11
|
+
end
|
12
|
+
|
13
|
+
task :default => :spec
|
data/lib/ruby-enum.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
module Ruby
|
2
|
+
module Enum
|
3
|
+
|
4
|
+
attr_reader :key, :value
|
5
|
+
|
6
|
+
def initialize(key, value)
|
7
|
+
@key = key
|
8
|
+
@value = value
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.included(base)
|
12
|
+
base.extend ClassMethods
|
13
|
+
end
|
14
|
+
|
15
|
+
module ClassMethods
|
16
|
+
|
17
|
+
def define(key, value)
|
18
|
+
@hash ||= {}
|
19
|
+
@hash[key] = self.new(key, value)
|
20
|
+
end
|
21
|
+
|
22
|
+
def const_missing(key)
|
23
|
+
if @hash[key]
|
24
|
+
@hash[key].value
|
25
|
+
else
|
26
|
+
raise Ruby::Enum::Errors::UninitializedConstantError.new({ :name => name, :key => key })
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def each(&block)
|
31
|
+
@hash.each do |key, value|
|
32
|
+
yield key, value
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def parse(s)
|
37
|
+
s = s.to_s.upcase
|
38
|
+
each do |key, value|
|
39
|
+
if key.to_s.upcase == s
|
40
|
+
return value.value
|
41
|
+
end
|
42
|
+
end
|
43
|
+
nil
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
end
|
data/lib/ruby_enum.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'ruby-enum'
|
data/ruby-enum.gemspec
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
require 'ruby-enum/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "ruby-enum"
|
6
|
+
s.version = Ruby::Enum::VERSION
|
7
|
+
s.authors = [ "Daniel Doubrovkine" ]
|
8
|
+
s.email = "dblock@dblock.org"
|
9
|
+
s.platform = Gem::Platform::RUBY
|
10
|
+
s.required_rubygems_version = '>= 1.3.6'
|
11
|
+
s.files = `git ls-files`.split("\n")
|
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
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class Colors
|
4
|
+
include Ruby::Enum
|
5
|
+
|
6
|
+
define :RED, "red"
|
7
|
+
define :GREEN, "green"
|
8
|
+
end
|
9
|
+
|
10
|
+
describe Ruby::Enum do
|
11
|
+
it "returns an enum value" do
|
12
|
+
Colors::RED.should eq "red"
|
13
|
+
Colors::GREEN.should eq "green"
|
14
|
+
end
|
15
|
+
context "parse" do
|
16
|
+
it "parses exact value" do
|
17
|
+
Colors.parse("red").should == Colors::RED
|
18
|
+
end
|
19
|
+
it "is case-insensitive" do
|
20
|
+
Colors.parse("ReD").should == Colors::RED
|
21
|
+
end
|
22
|
+
it "returns nil for a null value" do
|
23
|
+
Colors.parse(nil).should be_nil
|
24
|
+
end
|
25
|
+
it "returns nil for an invalid value" do
|
26
|
+
Colors.parse("invalid").should be_nil
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby-enum
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Daniel Doubrovkine
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-05-14 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description:
|
15
|
+
email: dblock@dblock.org
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- .gitignore
|
21
|
+
- .rspec
|
22
|
+
- .travis.yml
|
23
|
+
- CHANGELOG.md
|
24
|
+
- Gemfile
|
25
|
+
- LICENSE.md
|
26
|
+
- README.md
|
27
|
+
- Rakefile
|
28
|
+
- lib/ruby-enum.rb
|
29
|
+
- lib/ruby-enum/enum.rb
|
30
|
+
- lib/ruby-enum/version.rb
|
31
|
+
- lib/ruby_enum.rb
|
32
|
+
- ruby-enum.gemspec
|
33
|
+
- spec/ruby-enum/enum_spec.rb
|
34
|
+
- spec/ruby-enum/version_spec.rb
|
35
|
+
- spec/spec_helper.rb
|
36
|
+
homepage: http://github.com/dblock/ruby-enum
|
37
|
+
licenses:
|
38
|
+
- MIT
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
segments:
|
50
|
+
- 0
|
51
|
+
hash: 388465617
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ! '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: 1.3.6
|
58
|
+
requirements: []
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.8.24
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: Enum-like behavior for Ruby.
|
64
|
+
test_files: []
|