enumb 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in Renum.gemspec
4
+
5
+ gem 'rspec'
6
+ gem 'simplecov'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Manther
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,177 @@
1
+ # Renum
2
+
3
+ Authors: Jason Young
4
+
5
+ Renum helps developers create an enum class in Ruby that closely resembles commonly found enum behavior in most popular languages.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'Renum'
12
+
13
+ Install as Ruby Gem:
14
+
15
+ gem install renums
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install Renum
24
+
25
+ ## Usage
26
+
27
+ Simply extend the Enum module and define a class with as many enumerators as desired.
28
+
29
+ Example:
30
+ ```ruby
31
+ class TestEnumByte
32
+ extend Renum
33
+ enumerator :Nones => 0x0000
34
+ enumerator :Somes => 0x0001
35
+ enumerator :Anys => 0x0002
36
+ enumerator :Each => 0x0004
37
+ end
38
+ ```
39
+
40
+ Further examples can be seen in spec test:
41
+
42
+ ```ruby
43
+ require 'Renum'
44
+ describe 'Renum' do
45
+ context 'Given a class extends Renum' do
46
+ it 'if not passed a hash expect exception' do
47
+ class TestEnumNoHash
48
+ extend Renum
49
+
50
+ def init_enumerators
51
+ enumerator 'None'
52
+ end
53
+ end
54
+ expect { puts TestEnumNoHash.whatever }.to raise_error(Exception)
55
+ end
56
+ it 'if class has enumerator undefined expect exception' do
57
+ class TestEnumUndefined
58
+ extend Renum
59
+ enumerator 'None' => '123'
60
+ end
61
+ expect { puts TestEnumUndefined.whatever }.to raise_error(Exception)
62
+ end
63
+ it 'if class has multiple nodes defined for enumerator hash expect exception' do
64
+ expect {
65
+ class TestEnumMultiple
66
+ extend Renum
67
+ enumerator :None => '123', :Stuff => 456
68
+ end
69
+ TestEnumMultiple.None
70
+ }.to raise_error(Exception)
71
+ end
72
+ it 'if class does not have referenced enumerator defined expect exception' do
73
+ class TestEnumMissing
74
+ extend Renum
75
+ #nothing here
76
+ end
77
+ expect { puts TestEnumMissing.whatever }.to raise_error(Exception)
78
+ end
79
+ it 'if enum class enumerator keys are strings, values should evaluate correctly' do
80
+ class TestEnumStringKeys
81
+ extend Renum
82
+ enumerator 'None' => '123'
83
+ enumerator 'Some' => '456'
84
+ end
85
+ expect(TestEnumStringKeys.None).to eq('123')
86
+ end
87
+ it 'if enum class enumerator keys are symbols, values should evaluate correctly' do
88
+ class TestEnumSyms
89
+ extend Renum
90
+ enumerator :Nones => '123'
91
+ enumerator :Somes => '456'
92
+ end
93
+ expect(TestEnumSyms.Nones).to eq('123')
94
+ end
95
+ it 'if enum class has byte valued enumerators, bitwise comparisons should evaluate correctly' do
96
+ class TestEnumByte
97
+ extend Renum
98
+ enumerator :Nones => 0x0000
99
+ enumerator :Somes => 0x0001
100
+ enumerator :Anys => 0x0002
101
+ enumerator :Each => 0x0004
102
+ end
103
+
104
+ testItem = TestEnumByte.Nones
105
+ testItem = testItem | TestEnumByte.Anys
106
+ expect(((testItem & TestEnumByte.Each) == TestEnumByte.Each)).to eq(false)
107
+ expect(((testItem & TestEnumByte.Anys) == TestEnumByte.Anys)).to eq(true)
108
+ expect(((testItem & TestEnumByte.Anys) != 0)).to eq(true)
109
+ expect(((testItem & TestEnumByte.Somes) != 0)).to eq(false)
110
+ end
111
+ it 'if enum values are ints values should evaluate correctly' do
112
+ class TestEnumInts
113
+ extend Renum
114
+ enumerator :Nones => 1
115
+ enumerator :Somes => 2
116
+ enumerator :Anys => 3
117
+ enumerator :Each => 4
118
+ end
119
+ expect(TestEnumInts.Nones).to eq(1)
120
+ expect(TestEnumInts.Somes).to eq(2)
121
+ expect(TestEnumInts.Anys).to eq(3)
122
+ expect(TestEnumInts.Each).to eq(4)
123
+ end
124
+ it 'if enum values are bools values should evaluate correctly' do
125
+ class Tristate
126
+ extend Renum
127
+ enumerator :True => true
128
+ enumerator :False => false
129
+ enumerator :Undef => 'Undefined'
130
+ end
131
+ expect(Tristate.True).to eq(true)
132
+ expect(Tristate.False).to eq(false)
133
+ expect(Tristate.Undef).to eq('Undefined')
134
+ end
135
+ it 'if to_descriptor called, should evaluate correctly' do
136
+ class TestEnumDescriptor
137
+ extend Renum
138
+ enumerator :Nones => '123'
139
+ enumerator :Somes => '456'
140
+ end
141
+ expect(TestEnumDescriptor.to_descriptor('123')).to eq('Nones')
142
+ end
143
+ it 'if to_value called, should evaluate correctly' do
144
+ class TestEnumToValue
145
+ extend Renum
146
+ enumerator :Nones => '123'
147
+ enumerator :Somes => '456'
148
+ end
149
+ expect(TestEnumToValue.to_value(:Nones)).to eq('123')
150
+ end
151
+ it 'if iterate over enum correct values yielded' do
152
+ class TestEnumToIterate
153
+ extend Renum
154
+ enumerator :Nones => 0x0000
155
+ enumerator :Somes => 0x0001
156
+ enumerator :Anys => 0x0002
157
+ enumerator :Each => 0x0004
158
+ end
159
+
160
+ TestEnumToIterate.each do |x|
161
+ expect((((TestEnumToIterate.Nones |
162
+ TestEnumToIterate.Somes |
163
+ TestEnumToIterate.Anys |
164
+ TestEnumToIterate.Each) & x) == x)).to eq(true)
165
+ end
166
+ end
167
+ end
168
+ end
169
+ ```
170
+
171
+ ## Contributing
172
+
173
+ 1. Fork it
174
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
175
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
176
+ 4. Push to the branch (`git push origin my-new-feature`)
177
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/Renum.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'Renum/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "enumb"
8
+ spec.version = Renum::VERSION
9
+ spec.authors = ["Manther"]
10
+ spec.email = ["jason.manther.young@gmail.com"]
11
+ spec.description = %q{Renum helps developers create an enum like object in Ruby that closely resembles commonly found enum behavior of popular statically typed languages.}
12
+ spec.summary = %q{Enum module}
13
+ spec.homepage = "http://jasonwyoung.blogspot.com/"
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
+ end
@@ -0,0 +1,3 @@
1
+ module Renum
2
+ VERSION = "0.0.1"
3
+ end
data/lib/Renum.rb ADDED
@@ -0,0 +1,59 @@
1
+ #
2
+ # ***********************************************************************
3
+ # Copyright (c) 2014, Jason Young and Contributors. All Rights Reserved.
4
+ # ***********************************************************************
5
+ #
6
+
7
+ require "Renum/version"
8
+
9
+ module Renum
10
+ Enumerator
11
+ def enumerator(hash)
12
+ raise 'Parameter must be hash' unless hash.is_a?(Hash)
13
+ raise 'Multiple key/value pairs passed. Only single pair accepted.' unless hash.length == 1
14
+ key, val = hash.first
15
+ raise 'Enumerator key needs to be convertible to string' unless key.respond_to?(:to_s)
16
+ create_class_method(key.to_s) {
17
+ self.class_variable_get(String('@@__enum__' + key.to_s).to_sym)
18
+ }
19
+ self.class_variable_set(String('@@__enum__' + key.to_s).to_sym, val)
20
+ end
21
+
22
+ def to_descriptor(value)
23
+ self.methods.each do |x|
24
+ if ((self.send(x) rescue nil) == value)
25
+ return x.to_s
26
+ end
27
+ end
28
+ end
29
+
30
+ def parse(descriptor)
31
+ raise Exception('Descriptor needs to be convertible to string') unless descriptor.respond_to?(:to_s)
32
+ self.class_variables.each do |x|
33
+ if x.to_s.downcase == '@@__enum__' + descriptor.to_s.downcase
34
+ return self.class_variable_get(x)
35
+ end
36
+ end
37
+ end
38
+
39
+ def each
40
+ self.class_variables.each do |e|
41
+ if (e.to_s.include? '__enum__')
42
+ yield(self.class_variable_get(e))
43
+ end
44
+ end
45
+ end
46
+
47
+ private
48
+ def create_class_method(name, &block)
49
+ self.class.send(:define_method, name, &block)
50
+ end
51
+
52
+ #decided not to limit in these ways, but;
53
+ #if you want your enum sealed implement something similar
54
+ #def inherited subclass
55
+ # raise "class #{subclass} cannot be derived from sealed class #{self}"
56
+ #end
57
+ #you may also want to add self.freeze if you are heading down this restrictive path.
58
+
59
+ end
@@ -0,0 +1,133 @@
1
+ #
2
+ # ***********************************************************************
3
+ # Copyright (c) 2014, Jason Young and Contributors. All Rights Reserved.
4
+ # ***********************************************************************
5
+ #
6
+
7
+ require 'Renum'
8
+ describe 'Renum' do
9
+ context 'Given a class extends Renum' do
10
+ it 'if not passed a hash expect exception' do
11
+ class TestEnumNoHash
12
+ extend Renum
13
+
14
+ def init_enumerators
15
+ enumerator 'None'
16
+ end
17
+ end
18
+ expect { puts TestEnumNoHash.whatever }.to raise_error(Exception)
19
+ end
20
+ it 'if class has enumerator undefined expect exception' do
21
+ class TestEnumUndefined
22
+ extend Renum
23
+ enumerator 'None' => '123'
24
+ end
25
+ expect { puts TestEnumUndefined.whatever }.to raise_error(Exception)
26
+ end
27
+ it 'if class has multiple nodes defined for enumerator hash expect exception' do
28
+ expect {
29
+ class TestEnumMultiple
30
+ extend Renum
31
+ enumerator :None => '123', :Stuff => 456
32
+ end
33
+ TestEnumMultiple.None
34
+ }.to raise_error(Exception)
35
+ end
36
+ it 'if class does not have referenced enumerator defined expect exception' do
37
+ class TestEnumMissing
38
+ extend Renum
39
+ #nothing here
40
+ end
41
+ expect { puts TestEnumMissing.whatever }.to raise_error(Exception)
42
+ end
43
+ it 'if enum class enumerator keys are strings, values should evaluate correctly' do
44
+ class TestEnumStringKeys
45
+ extend Renum
46
+ enumerator 'None' => '123'
47
+ enumerator 'Some' => '456'
48
+ end
49
+ expect(TestEnumStringKeys.None).to eq('123')
50
+ end
51
+ it 'if enum class enumerator keys are symbols, values should evaluate correctly' do
52
+ class TestEnumSyms
53
+ extend Renum
54
+ enumerator :Nones => '123'
55
+ enumerator :Somes => '456'
56
+ end
57
+ expect(TestEnumSyms.Nones).to eq('123')
58
+ end
59
+ it 'if enum class has byte valued enumerators, bitwise comparisons should evaluate correctly' do
60
+ class TestEnumByte
61
+ extend Renum
62
+ enumerator :Nones => 0x0000
63
+ enumerator :Somes => 0x0001
64
+ enumerator :Anys => 0x0002
65
+ enumerator :Each => 0x0004
66
+ end
67
+
68
+ testItem = TestEnumByte.Nones
69
+ testItem = testItem | TestEnumByte.Anys
70
+ expect(((testItem & TestEnumByte.Each) == TestEnumByte.Each)).to eq(false)
71
+ expect(((testItem & TestEnumByte.Anys) == TestEnumByte.Anys)).to eq(true)
72
+ expect(((testItem & TestEnumByte.Anys) != 0)).to eq(true)
73
+ expect(((testItem & TestEnumByte.Somes) != 0)).to eq(false)
74
+ end
75
+ it 'if enum values are ints values should evaluate correctly' do
76
+ class TestEnumInts
77
+ extend Renum
78
+ enumerator :Nones => 1
79
+ enumerator :Somes => 2
80
+ enumerator :Anys => 3
81
+ enumerator :Each => 4
82
+ end
83
+
84
+ expect(TestEnumInts.Nones).to eq(1)
85
+ expect(TestEnumInts.Somes).to eq(2)
86
+ expect(TestEnumInts.Anys).to eq(3)
87
+ expect(TestEnumInts.Each).to eq(4)
88
+ end
89
+ it 'if enum values are bools values should evaluate correctly' do
90
+ class Tristate
91
+ extend Renum
92
+ enumerator :True => true
93
+ enumerator :False => false
94
+ enumerator :Undef => 'Undefined'
95
+ end
96
+ expect(Tristate.True).to eq(true)
97
+ expect(Tristate.False).to eq(false)
98
+ expect(Tristate.Undef).to eq('Undefined')
99
+ end
100
+ it 'if to_descriptor called, should evaluate correctly' do
101
+ class TestEnumDescriptor
102
+ extend Renum
103
+ enumerator :Nones => '123'
104
+ enumerator :Somes => '456'
105
+ end
106
+ expect(TestEnumDescriptor.to_descriptor('123')).to eq('Nones')
107
+ end
108
+ it 'if to_value called, should evaluate correctly' do
109
+ class TestEnumToValue
110
+ extend Renum
111
+ enumerator :Nones => '123'
112
+ enumerator :Somes => '456'
113
+ end
114
+ expect(TestEnumToValue.parse(:Nones)).to eq('123')
115
+ end
116
+ it 'if iterate over enum correct values yielded' do
117
+ class TestEnumToIterate
118
+ extend Renum
119
+ enumerator :Nones => 0x0000
120
+ enumerator :Somes => 0x0001
121
+ enumerator :Anys => 0x0002
122
+ enumerator :Each => 0x0004
123
+ end
124
+
125
+ TestEnumToIterate.each do |x|
126
+ expect((((TestEnumToIterate.Nones |
127
+ TestEnumToIterate.Somes |
128
+ TestEnumToIterate.Anys |
129
+ TestEnumToIterate.Each) & x) == x)).to eq(true)
130
+ end
131
+ end
132
+ end
133
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: enumb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Manther
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ version_requirements: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ requirement: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - "~>"
23
+ - !ruby/object:Gem::Version
24
+ version: '1.3'
25
+ prerelease: false
26
+ type: :development
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ prerelease: false
40
+ type: :development
41
+ description: Renum helps developers create an enum like object in Ruby that closely resembles commonly found enum behavior of popular statically typed languages.
42
+ email:
43
+ - jason.manther.young@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".idea/.name"
50
+ - ".idea/.rakeTasks"
51
+ - ".idea/Renum.iml"
52
+ - ".idea/dictionaries/jasony.xml"
53
+ - ".idea/encodings.xml"
54
+ - ".idea/misc.xml"
55
+ - ".idea/modules.xml"
56
+ - ".idea/scopes/scope_settings.xml"
57
+ - ".idea/vcs.xml"
58
+ - ".idea/workspace.xml"
59
+ - Gemfile
60
+ - LICENSE.txt
61
+ - README.md
62
+ - Rakefile
63
+ - Renum.gemspec
64
+ - lib/Renum.rb
65
+ - lib/Renum/version.rb
66
+ - spec/unit/Renum_spec.rb
67
+ homepage: http://jasonwyoung.blogspot.com/
68
+ licenses:
69
+ - MIT
70
+ metadata: {}
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 2.2.1
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: Enum module
91
+ test_files:
92
+ - spec/unit/Renum_spec.rb