enumz 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --color
2
+ --format documentation
3
+ --backtrace
4
+ --default_path spec
data/.simplecov ADDED
@@ -0,0 +1,5 @@
1
+ # configure code coverage
2
+ SimpleCov.start do
3
+ # exclude directories and files
4
+ add_filter "/spec/"
5
+ end
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development, :test do
9
+ gem "rdoc", ">= 3.12"
10
+ gem "bundler", ">= 1.0.0"
11
+ gem "jeweler", ">= 1.8.4"
12
+ gem "rspec", ">= 0"
13
+ gem "simplecov"
14
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,37 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.3)
5
+ git (1.2.5)
6
+ jeweler (1.8.4)
7
+ bundler (~> 1.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rdoc
11
+ json (1.7.5)
12
+ multi_json (1.3.6)
13
+ rake (0.9.2.2)
14
+ rdoc (3.12)
15
+ json (~> 1.4)
16
+ rspec (2.11.0)
17
+ rspec-core (~> 2.11.0)
18
+ rspec-expectations (~> 2.11.0)
19
+ rspec-mocks (~> 2.11.0)
20
+ rspec-core (2.11.1)
21
+ rspec-expectations (2.11.2)
22
+ diff-lcs (~> 1.1.3)
23
+ rspec-mocks (2.11.2)
24
+ simplecov (0.6.4)
25
+ multi_json (~> 1.0)
26
+ simplecov-html (~> 0.5.3)
27
+ simplecov-html (0.5.3)
28
+
29
+ PLATFORMS
30
+ ruby
31
+
32
+ DEPENDENCIES
33
+ bundler (>= 1.0.0)
34
+ jeweler (>= 1.8.4)
35
+ rdoc (>= 3.12)
36
+ rspec
37
+ simplecov
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 obie quelland
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ enumz
2
+ =====
3
+
4
+ TODO: update content
data/Rakefile ADDED
@@ -0,0 +1,47 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "enumz"
18
+ gem.homepage = "http://github.com/obieq/enumz"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Ruby Enumeration}
21
+ gem.description = %Q{ Provides enumeration functionality akin to that of C#'s }
22
+ gem.email = "quelland@gmail.com"
23
+ gem.authors = ["obie quelland"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rdoc/task'
29
+ Rake::RDocTask.new do |rdoc|
30
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
31
+
32
+ rdoc.rdoc_dir = 'rdoc'
33
+ rdoc.title = "enumz #{version}"
34
+ rdoc.rdoc_files.include('README*')
35
+ rdoc.rdoc_files.include('lib/**/*.rb')
36
+ end
37
+
38
+ # Get spec rake tasks working in RSpec 2.0
39
+ require 'rspec/core/rake_task'
40
+
41
+ desc 'Default: run specs.'
42
+ task :default => :spec
43
+
44
+ desc "Run specs"
45
+ RSpec::Core::RakeTask.new do |t|
46
+
47
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
data/enumz.gemspec ADDED
@@ -0,0 +1,67 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "enumz"
8
+ s.version = "1.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["obie quelland"]
12
+ s.date = "2012-09-15"
13
+ s.description = " Provides enumeration functionality akin to that of C#'s "
14
+ s.email = "quelland@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".rspec",
21
+ ".simplecov",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.md",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "enumz.gemspec",
29
+ "lib/enumz.rb",
30
+ "lib/enumz/base_enumz.rb",
31
+ "lib/enumz/enumz_factory.rb",
32
+ "spec/enum_spec.rb",
33
+ "spec/spec.opts",
34
+ "spec/spec_helper.rb",
35
+ "spec/support/enumz_creation_shared_examples.rb"
36
+ ]
37
+ s.homepage = "http://github.com/obieq/enumz"
38
+ s.licenses = ["MIT"]
39
+ s.require_paths = ["lib"]
40
+ s.rubygems_version = "1.8.19"
41
+ s.summary = "Ruby Enumeration"
42
+
43
+ if s.respond_to? :specification_version then
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
+ s.add_development_dependency(%q<rdoc>, [">= 3.12"])
48
+ s.add_development_dependency(%q<bundler>, [">= 1.0.0"])
49
+ s.add_development_dependency(%q<jeweler>, [">= 1.8.4"])
50
+ s.add_development_dependency(%q<rspec>, [">= 0"])
51
+ s.add_development_dependency(%q<simplecov>, [">= 0"])
52
+ else
53
+ s.add_dependency(%q<rdoc>, [">= 3.12"])
54
+ s.add_dependency(%q<bundler>, [">= 1.0.0"])
55
+ s.add_dependency(%q<jeweler>, [">= 1.8.4"])
56
+ s.add_dependency(%q<rspec>, [">= 0"])
57
+ s.add_dependency(%q<simplecov>, [">= 0"])
58
+ end
59
+ else
60
+ s.add_dependency(%q<rdoc>, [">= 3.12"])
61
+ s.add_dependency(%q<bundler>, [">= 1.0.0"])
62
+ s.add_dependency(%q<jeweler>, [">= 1.8.4"])
63
+ s.add_dependency(%q<rspec>, [">= 0"])
64
+ s.add_dependency(%q<simplecov>, [">= 0"])
65
+ end
66
+ end
67
+
data/lib/enumz.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'enumz/enumz_factory'
2
+
3
+ module Enumz
4
+
5
+ def enum(name, data_elements, options={})
6
+ EnumzFactory.create(name, data_elements, options)
7
+ end # enum
8
+
9
+ end # module Enumz
10
+
11
+ extend Enumz
12
+
13
+ Module.module_eval do
14
+ extend Enumz
15
+ end
@@ -0,0 +1,42 @@
1
+ module BaseEnumz
2
+ attr_reader :data_elements, :options
3
+
4
+ # list of ruby module method names that
5
+ # cannot be overridden
6
+ # i.e. overridding enum.class is too destructive
7
+ RESERVED_METHOD_NAMES = [:class, :object_id, :method_missing,
8
+ :instance_eval, :send, :should_not, :nil?,
9
+ :const_get, :const_missing, :inspect, :to_s]
10
+
11
+ # return list of enum names
12
+ def names
13
+ @names ||= data_elements.keys
14
+ end
15
+
16
+ # return list of enum values
17
+ def values
18
+ @values ||= data_elements.values
19
+ end
20
+
21
+ # return the corresponding enum name for a given value
22
+ def [](value)
23
+ @inverted_data_elements ||= data_elements.invert
24
+ @inverted_data_elements[value]
25
+ end
26
+
27
+ # create a method for each enum name
28
+ def method_missing(meth, *args, &block)
29
+ if data_elements[meth.to_s.to_sym]
30
+ self.instance_eval <<-EVAL
31
+ def #{meth}
32
+ data_elements[:#{meth}]
33
+ end
34
+ EVAL
35
+
36
+ __send__(meth, *args, &block)
37
+ else
38
+ super
39
+ end
40
+ end
41
+
42
+ end # module BaseEnumz
@@ -0,0 +1,75 @@
1
+ require 'enumz/base_enumz'
2
+
3
+ module EnumzFactory
4
+
5
+ class << self
6
+
7
+ def create(name, data_elements, options)
8
+ name = if name.is_a? Symbol
9
+ name.to_s.split('_').map(&:capitalize).join
10
+ else
11
+ raise ArgumentError, 'name must be of type symbol. e.g., "enum :my_name [:red, "yellow", ..]"'
12
+ end
13
+
14
+ create_module(name, process_data_elements(data_elements), options)
15
+ end # create
16
+
17
+ def process_data_elements(data_elements)
18
+ data_elements = if data_elements.is_a? Array
19
+ # if an array, then convert to a hash
20
+ # 1) the hash keys = array data_elements
21
+ # 2) the hash data_elements correspond to the array value's index position
22
+ # 3) the hash keys are symbols
23
+ Hash[*data_elements.reduce([]) {|result, a| result << a.to_s.to_sym; result << (result.count / 2) + 1 }.flatten]
24
+ elsif data_elements.is_a? Hash
25
+ # ensure the values are unique
26
+ raise ArgumentError, 'values must be unique' unless data_elements.values.count == data_elements.values.uniq.count
27
+
28
+ # ensure that the keys are symbols
29
+ Hash[ data_elements.map { |k, v| [ k.to_s.to_sym, v ] }]
30
+ else
31
+ raise ArgumentError, 'data_elements must be either an array or a hash, e.g., "enum :my_name {:red => 1, "yellow" => 2, ..}"'
32
+ end
33
+
34
+ # raise an exception if an enum name is a reserved method name
35
+ intersection = (data_elements.keys & BaseEnumz::RESERVED_METHOD_NAMES)
36
+ raise ArgumentError, "enum name #{intersection[0]} is a reserved ruby method name" unless intersection.empty?
37
+
38
+ # freeze the hash so no modifications can be made at a later time
39
+ data_elements.freeze
40
+ end # process_data_elements
41
+
42
+ def create_module(name, data_elements, options)
43
+ # create a new module
44
+ mod = Object.const_set(name.to_s, Module.new)
45
+
46
+ # grab base methods from the new module,
47
+ # which we'll delete at the end
48
+ base_methods = mod.methods.clone.sort
49
+
50
+ # the new module extends the BaseEnum module
51
+ mod.extend BaseEnumz
52
+
53
+ # set the data_elements property
54
+ mod.instance_variable_set('@data_elements', data_elements)
55
+
56
+ # set the options property
57
+ mod.instance_variable_set('@options', options)
58
+
59
+ # add a constant for each enum name/value
60
+ data_elements.each { |k,v| mod.const_set(k.to_s.capitalize, v) }
61
+
62
+ # remove all unnecessary defacto ruby module methods
63
+ # in order to avoid potential conflicts with enum names
64
+ # EX: module.name vs enum.name
65
+ # module.clone vs enum.clone
66
+ base_methods.each do |m|
67
+ mod.instance_eval "undef :#{m}" unless (m.match(/^__/)) or BaseEnumz::RESERVED_METHOD_NAMES.include?(m)
68
+ end
69
+
70
+ mod
71
+ end # create_module
72
+
73
+ end # class << self
74
+
75
+ end # module EnumzFactory
data/spec/enum_spec.rb ADDED
@@ -0,0 +1,80 @@
1
+ require 'spec_helper'
2
+
3
+ enum :array_colors, [:yellow, "green", 'blue']
4
+ enum :hash_colors, {:yellow => 2, "green" => 5, 'blue' => 8, :mauve => 20}
5
+ enum :module_method_names, [:name, "display", 'clone']
6
+
7
+ describe Enumz do
8
+
9
+ describe 'creation' do
10
+
11
+ describe 'via array' do
12
+ names = [:yellow, "green", 'blue']
13
+ values = [1,2,3]
14
+
15
+ it_should_behave_like 'a properly created enum', ArrayColors, 3, names, values
16
+
17
+ it 'should generate values' do
18
+ ArrayColors.values.should_not be_nil
19
+ ArrayColors.values.count.should eq(3)
20
+ ArrayColors.values.reduce(1) do |counter, value|
21
+ counter.should eq(value)
22
+ counter += 1
23
+ end
24
+ end
25
+
26
+ end # creation
27
+
28
+ describe 'via hash' do
29
+ names = [:yellow, "green", 'blue', :mauve]
30
+ values = [2,5,8,20]
31
+
32
+ it_should_behave_like 'a properly created enum', HashColors, 4, names, values
33
+ end
34
+
35
+ end # via hash
36
+
37
+ describe "features" do
38
+
39
+ it 'should remove module methods to avoid potential conflicts when creating methods for enum names' do
40
+ ModuleMethodNames.name.should eq(1)
41
+ ModuleMethodNames.display.should eq(2)
42
+ ModuleMethodNames.clone.should eq(3)
43
+ end
44
+
45
+ end
46
+
47
+ describe 'exceptions' do
48
+ module ExceptionsEnum
49
+ extend Enumz
50
+ end
51
+
52
+ it 'should raise an exception if enum name is not a symbol' do
53
+ expect { ExceptionsEnum.enum "hi", [] }.to raise_error ArgumentError
54
+ end
55
+
56
+ it 'should raise an exception if enum values is neither a hash nor an array' do
57
+ expect { ExceptionsEnum.enum :test_enum, :invalid_value }.to raise_error ArgumentError
58
+ end
59
+
60
+ it 'should raise an exception if attempting to access a non-existent enum name' do
61
+ expect { ArrayColors.invisible }.to raise_error NoMethodError
62
+ end
63
+
64
+ it 'should raise an exception if a duplicate enum value is supplied' do
65
+ expect { ExceptionsEnum.enum :test_enum, {:yellow => 1, :green => 2, :blue => 1}}.to raise_error ArgumentError
66
+ end
67
+
68
+ it 'should raise an exception when creating an enum with a name that is a ruby reserved method name' do
69
+ BaseEnumz::RESERVED_METHOD_NAMES.each do |name|
70
+ expect { ExceptionsEnum.enum :test_enum, [name] }.to raise_error ArgumentError
71
+ end
72
+ end
73
+
74
+ it 'should raise a read-only exception when trying to update' do
75
+ expect { ArrayColors.data_elements[:alyssum] = 100 }.to raise_error RuntimeError
76
+ end
77
+
78
+ end # validations
79
+
80
+ end # Enumz
data/spec/spec.opts ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format
@@ -0,0 +1,8 @@
1
+ require 'simplecov'
2
+ require 'rspec'
3
+
4
+ # require the library files
5
+ Dir["./lib/**/*.rb"].each {|f| require f}
6
+
7
+ # require the shared example files
8
+ Dir["./spec/support/**/*.rb"].each {|f| require f}
@@ -0,0 +1,55 @@
1
+ shared_examples "a properly created enum" do |enum, num_data_elements, names, values|
2
+
3
+ it 'should create a module whose name matches that of the enum' do
4
+ enum.should_not be_nil
5
+ enum.class.should eq(Module)
6
+ end
7
+
8
+ it 'should be valid' do
9
+ enum.data_elements.class.should eq(Hash)
10
+ enum.data_elements.count.should eq(num_data_elements)
11
+
12
+ enum.names.count.should eq(num_data_elements)
13
+ enum.values.count.should eq(num_data_elements)
14
+
15
+ # compare actual names w/ expected names
16
+ enum.names.reduce(0) do |counter, key|
17
+ names[counter].to_sym.should eq(key)
18
+ counter += 1
19
+ end
20
+
21
+ # compare actual values w/ expected values
22
+ enum.values.reduce(0) do |counter, value|
23
+ values[counter].should eq(value)
24
+ counter += 1
25
+ end
26
+ end
27
+
28
+ it 'should symbolize all names' do
29
+ enum.names.each do |key|
30
+ key.class.should eq(Symbol)
31
+ end
32
+ end
33
+
34
+ it 'should access values via methods whose names match those of the enum names' do
35
+ enum.names.reduce(0) do |counter, key|
36
+ enum.send("#{key}").should eq(values[counter])
37
+ counter += 1
38
+ end
39
+ end
40
+
41
+ it 'should access values via constants whose names match those of the enum names' do
42
+ enum.names.reduce(0) do |counter, key|
43
+ enum.const_get(key.to_s.capitalize).should eq(values[counter])
44
+ counter += 1
45
+ end
46
+ end
47
+
48
+ it 'should get enum name by value' do
49
+ values.reduce(0) do |counter, value|
50
+ enum[value].should eq(names[counter].to_sym)
51
+ counter += 1
52
+ end
53
+ end
54
+
55
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: enumz
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - obie quelland
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rdoc
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '3.12'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '3.12'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 1.0.0
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 1.0.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: jeweler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: 1.8.4
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.8.4
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: simplecov
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: ! ' Provides enumeration functionality akin to that of C#''s '
95
+ email: quelland@gmail.com
96
+ executables: []
97
+ extensions: []
98
+ extra_rdoc_files:
99
+ - LICENSE.txt
100
+ - README.md
101
+ files:
102
+ - .rspec
103
+ - .simplecov
104
+ - Gemfile
105
+ - Gemfile.lock
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - VERSION
110
+ - enumz.gemspec
111
+ - lib/enumz.rb
112
+ - lib/enumz/base_enumz.rb
113
+ - lib/enumz/enumz_factory.rb
114
+ - spec/enum_spec.rb
115
+ - spec/spec.opts
116
+ - spec/spec_helper.rb
117
+ - spec/support/enumz_creation_shared_examples.rb
118
+ homepage: http://github.com/obieq/enumz
119
+ licenses:
120
+ - MIT
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ none: false
127
+ requirements:
128
+ - - ! '>='
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ segments:
132
+ - 0
133
+ hash: 3518699016868134358
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ none: false
136
+ requirements:
137
+ - - ! '>='
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 1.8.19
143
+ signing_key:
144
+ specification_version: 3
145
+ summary: Ruby Enumeration
146
+ test_files: []