ruby-enum 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2 @@
1
+ Gemfile.lock
2
+ pkg
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,6 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - jruby-19mode
6
+ - rbx-19mode
@@ -0,0 +1,3 @@
1
+ ### 0.1.0 (5/14/2013)
2
+
3
+ * Initial public release - [@dblock](https://github.com/dblock).
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem "rspec"
6
+ gem "rake"
@@ -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.
@@ -0,0 +1,33 @@
1
+ Ruby::Enum
2
+ ==========
3
+
4
+ [![Build Status](https://travis-ci.org/dblock/ruby-enum.png)](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
+
@@ -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
@@ -0,0 +1,3 @@
1
+ require 'ruby-enum/version'
2
+ require 'ruby-enum/enum'
3
+
@@ -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
@@ -0,0 +1,5 @@
1
+ module Ruby
2
+ module Enum
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ require 'ruby-enum'
@@ -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
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ruby::Enum do
4
+ it "has a version" do
5
+ Ruby::Enum::VERSION.should_not be_nil
6
+ end
7
+ end
@@ -0,0 +1,6 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+
3
+ require 'rubygems'
4
+ require 'rspec'
5
+ require 'ruby-enum'
6
+
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: []