enumerated_type 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/enumerated_type.gemspec +24 -0
- data/lib/enumerated_type.rb +69 -0
- data/lib/enumerated_type/version.rb +3 -0
- data/test/enumerated_type_spec.rb +95 -0
- metadata +117 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Rafer Hazen
|
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
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Enumeration
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'enumerated_type'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install enumerated_type
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'enumerated_type/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "enumerated_type"
|
8
|
+
spec.version = EnumeratedType::VERSION
|
9
|
+
spec.authors = ["Rafer Hazen"]
|
10
|
+
spec.email = ["rafer@ralua.com"]
|
11
|
+
spec.description = %q{Simple enumerated types}
|
12
|
+
spec.summary = %q{Simple enumerated types}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
24
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require "enumerated_type/version"
|
2
|
+
|
3
|
+
module EnumeratedType
|
4
|
+
def self.included(base)
|
5
|
+
base.instance_eval do
|
6
|
+
@all = []
|
7
|
+
|
8
|
+
attr_reader :name, :value
|
9
|
+
|
10
|
+
private_class_method :new
|
11
|
+
|
12
|
+
extend Enumerable
|
13
|
+
extend ClassMethods
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def initialize(name, value, options = {})
|
20
|
+
@name = name
|
21
|
+
@value = value
|
22
|
+
|
23
|
+
options.each { |k, v| send(:"#{k}=", v) }
|
24
|
+
end
|
25
|
+
|
26
|
+
module ClassMethods
|
27
|
+
def each(&block)
|
28
|
+
@all.each(&block)
|
29
|
+
end
|
30
|
+
|
31
|
+
def by_value(value)
|
32
|
+
each { |e| return e if e.value == value }
|
33
|
+
raise ArgumentError, "Unrecognized #{self.name} value #{value.inspect}'"
|
34
|
+
end
|
35
|
+
|
36
|
+
def by_name(name)
|
37
|
+
each { |e| return e if e.name == name }
|
38
|
+
raise ArgumentError, "Unrecognized #{self.name} name #{name.inspect}'"
|
39
|
+
end
|
40
|
+
|
41
|
+
def [](name)
|
42
|
+
by_name(name)
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def declare(name, options = {})
|
48
|
+
value = options.delete(:value)
|
49
|
+
value ||= (map(&:value).max || 0) + 1
|
50
|
+
|
51
|
+
if map(&:name).include?(name)
|
52
|
+
raise(ArgumentError, "duplicate name #{name.inspect}")
|
53
|
+
end
|
54
|
+
|
55
|
+
if map(&:value).include?(value)
|
56
|
+
raise(ArgumentError, "duplicate :value #{value.inspect}")
|
57
|
+
end
|
58
|
+
|
59
|
+
define_method(:"#{name}?") do
|
60
|
+
self.name == name
|
61
|
+
end
|
62
|
+
|
63
|
+
enumerated = new(name, value, options)
|
64
|
+
|
65
|
+
@all << enumerated
|
66
|
+
const_set(name.to_s.upcase, enumerated)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
|
2
|
+
|
3
|
+
require "rubygems"
|
4
|
+
require "bundler/setup"
|
5
|
+
|
6
|
+
require "minitest"
|
7
|
+
require "minitest/pride"
|
8
|
+
|
9
|
+
require "enumerated_type"
|
10
|
+
|
11
|
+
describe EnumeratedType do
|
12
|
+
class Gender
|
13
|
+
include EnumeratedType
|
14
|
+
|
15
|
+
attr_accessor :planet
|
16
|
+
|
17
|
+
declare(:male, :planet => "mars")
|
18
|
+
declare(:female, :planet => "venus")
|
19
|
+
end
|
20
|
+
|
21
|
+
it "privatizes the constructor" do
|
22
|
+
lambda { Gender.new }.must_raise(NoMethodError, /private method `new' called/)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "is enumerable" do
|
26
|
+
Gender.must_be_kind_of Enumerable
|
27
|
+
end
|
28
|
+
|
29
|
+
it "enumerates over all the declared types" do
|
30
|
+
Gender.entries.must_equal [Gender[:male], Gender[:female]]
|
31
|
+
end
|
32
|
+
|
33
|
+
it "declares an actual constant for each type" do
|
34
|
+
Gender::MALE.must_be_same_as Gender[:male]
|
35
|
+
Gender::FEMALE.must_be_same_as Gender[:female]
|
36
|
+
end
|
37
|
+
|
38
|
+
it "defines a predicate method for each type based on the name" do
|
39
|
+
Gender.entries.map(&:male?).must_equal [true, false]
|
40
|
+
Gender.entries.map(&:female?).must_equal [false, true]
|
41
|
+
end
|
42
|
+
|
43
|
+
describe ".declare" do
|
44
|
+
it "is private" do
|
45
|
+
lambda { Gender.declare }.must_raise(NoMethodError, /private method `declare' called/)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "requires the name to be unique" do
|
49
|
+
duplicate_name = Gender.first.name
|
50
|
+
lambda { Gender.send(:declare, duplicate_name) }.must_raise(ArgumentError, /duplicate name/)
|
51
|
+
end
|
52
|
+
|
53
|
+
it "allows you to specify :value" do
|
54
|
+
gender = Class.new do
|
55
|
+
include EnumeratedType
|
56
|
+
declare :male, :value => 100
|
57
|
+
declare :female, :value => 101
|
58
|
+
end
|
59
|
+
|
60
|
+
gender.map(&:value).must_equal [100, 101]
|
61
|
+
end
|
62
|
+
|
63
|
+
it "requires :value to be unique" do
|
64
|
+
duplicate_value = Gender.first.value
|
65
|
+
|
66
|
+
lambda { Gender.send(:declare, :neuter, :value => duplicate_value ) }.must_raise(ArgumentError, /duplicate :value/)
|
67
|
+
end
|
68
|
+
|
69
|
+
it "assigns extra attributes from .declare" do
|
70
|
+
Gender.map(&:planet).must_equal ["mars", "venus"]
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe ".by_name" do
|
75
|
+
it "returns the type with the given name" do
|
76
|
+
gender = Gender.first
|
77
|
+
Gender.by_name(gender.name).must_equal gender
|
78
|
+
end
|
79
|
+
|
80
|
+
it "raises an error given an unrecognized name" do
|
81
|
+
lambda { Gender.by_name(:neuter) }.must_raise(ArgumentError)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe ".by_value" do
|
86
|
+
it "returns the type with the given value" do
|
87
|
+
gender = Gender.first
|
88
|
+
Gender.by_value(gender.value).must_equal gender
|
89
|
+
end
|
90
|
+
|
91
|
+
it "raises an error given an unrecognized value" do
|
92
|
+
lambda { Gender.by_value((Gender.map(&:value).max) + 1) }.must_raise(ArgumentError)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
metadata
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: enumerated_type
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Rafer Hazen
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2013-07-03 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: bundler
|
22
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ~>
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
hash: 9
|
28
|
+
segments:
|
29
|
+
- 1
|
30
|
+
- 3
|
31
|
+
version: "1.3"
|
32
|
+
prerelease: false
|
33
|
+
type: :development
|
34
|
+
requirement: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: rake
|
37
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 3
|
43
|
+
segments:
|
44
|
+
- 0
|
45
|
+
version: "0"
|
46
|
+
prerelease: false
|
47
|
+
type: :development
|
48
|
+
requirement: *id002
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: minitest
|
51
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ~>
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
hash: 31
|
57
|
+
segments:
|
58
|
+
- 5
|
59
|
+
- 0
|
60
|
+
version: "5.0"
|
61
|
+
prerelease: false
|
62
|
+
type: :development
|
63
|
+
requirement: *id003
|
64
|
+
description: Simple enumerated types
|
65
|
+
email:
|
66
|
+
- rafer@ralua.com
|
67
|
+
executables: []
|
68
|
+
|
69
|
+
extensions: []
|
70
|
+
|
71
|
+
extra_rdoc_files: []
|
72
|
+
|
73
|
+
files:
|
74
|
+
- .gitignore
|
75
|
+
- Gemfile
|
76
|
+
- LICENSE.txt
|
77
|
+
- README.md
|
78
|
+
- Rakefile
|
79
|
+
- enumerated_type.gemspec
|
80
|
+
- lib/enumerated_type.rb
|
81
|
+
- lib/enumerated_type/version.rb
|
82
|
+
- test/enumerated_type_spec.rb
|
83
|
+
homepage: ""
|
84
|
+
licenses:
|
85
|
+
- MIT
|
86
|
+
post_install_message:
|
87
|
+
rdoc_options: []
|
88
|
+
|
89
|
+
require_paths:
|
90
|
+
- lib
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
+
none: false
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
hash: 3
|
97
|
+
segments:
|
98
|
+
- 0
|
99
|
+
version: "0"
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
none: false
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
hash: 3
|
106
|
+
segments:
|
107
|
+
- 0
|
108
|
+
version: "0"
|
109
|
+
requirements: []
|
110
|
+
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 1.8.25
|
113
|
+
signing_key:
|
114
|
+
specification_version: 3
|
115
|
+
summary: Simple enumerated types
|
116
|
+
test_files:
|
117
|
+
- test/enumerated_type_spec.rb
|