typed-enum 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,12 @@
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 do
9
+ gem "bundler", "~> 1.0.0"
10
+ gem "jeweler", "~> 1.5.2"
11
+ gem "rcov", ">= 0"
12
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Chris Tucker
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.
@@ -0,0 +1,47 @@
1
+ = typed-enum
2
+
3
+ A TypedEnum offers an enumeration facility allowing for the expression of a constrained set of (enumerated) values of a user-defined class, akin to Java's concept of enum. It supports listing the available enumerated values and serializing/deserializing instances to/from string representations. It is *not* thread safe (though it could be made so relatively easily).
4
+
5
+ == Basic Usage
6
+
7
+ A TypedEnum can be used to represent a typed, enumerated set of values. A TypedEnum offers the following features:
8
+
9
+ * Instances are restricted (in the absence of class-opening) to the set of instances defined in
10
+ the class definition
11
+ * Available values can be enumerated in a consistent order via a call to the #values class method
12
+ * Instances can be retrieved via class constants
13
+ * Instances can be retrieved by parsing a string/symbol
14
+ * Instances can be serialized to a string
15
+
16
+ Basic usage example:
17
+ class Color
18
+ @enum = lambda {[
19
+ RED = self.new(:red),
20
+ GREEN = self.new(:green),
21
+ BLUE = self.new(:blue)
22
+ ]}
23
+ include TypedEnum
24
+ end
25
+
26
+ Color::RED => #<Color:0x163d59f8 @name=:red>
27
+ Color.from_name(:red) => #<Color:0x163d59f8 @name=:red>
28
+ Color.from_name('red') => #<Color:0x163d59f8 @name=:red>
29
+ Color::RED.name => :red
30
+ Color.values => [#<Color:0x163d59f8 @name=:red>, #<Color:0x163d59a8 @name=:green>, #<Color:0x163d5980 @name=:blue>]
31
+
32
+
33
+ == Contributing to typed-enum
34
+
35
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
36
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
37
+ * Fork the project
38
+ * Start a feature/bugfix branch
39
+ * Commit and push until you are happy with your contribution
40
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
41
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
42
+
43
+ == Copyright
44
+
45
+ Copyright (c) 2011 Chris Tucker. See LICENSE.txt for
46
+ further details.
47
+
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "typed-enum"
16
+ gem.homepage = "http://github.com/ctucker/typed-enum"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{Typed, restricted enumerated set of values}
19
+ gem.description = %Q{A type-safe(ish) way to provide a constrained set of enumerated values that support basic enumeration, serialization/deserialization, and comparison}
20
+ gem.email = "ctucker@glyde.com"
21
+ gem.authors = ["Chris Tucker"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ require 'rcov/rcovtask'
37
+ Rcov::RcovTask.new do |test|
38
+ test.libs << 'test'
39
+ test.pattern = 'test/**/test_*.rb'
40
+ test.verbose = true
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "typed-enum #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,104 @@
1
+ # A TypedEnum can be used to represent a typed, enumerated set of values. A
2
+ # TypedEnum offers the following features:
3
+ #
4
+ # * Instances are restricted (in the absence of class-opening) to the set of instances defined in
5
+ # the class definition
6
+ # * Available values can be enumerated in a consistent order via a call to the
7
+ # * #values class method
8
+ # * Instances can be retrieved via class constants
9
+ # * Instances can be retrieved by parsing a string/symbol
10
+ # * Instances can be serialized to a string
11
+ #
12
+ # Basic usage example:
13
+ # class Color
14
+ # @enum = lambda {[
15
+ # RED = self.new(:red),
16
+ # GREEN = self.new(:green),
17
+ # BLUE = self.new(:blue)
18
+ # ]}
19
+ # include TypedEnum
20
+ # end
21
+ #
22
+ # Color::RED => #<Color:0x163d59f8 @name=:red>
23
+ # Color.from_name(:red) => #<Color:0x163d59f8 @name=:red>
24
+ # Color.from_name('red') => #<Color:0x163d59f8 @name=:red>
25
+ # Color::RED.name => :red
26
+ # Color.values => [#<Color:0x163d59f8 @name=:red>, #<Color:0x163d59a8 @name=:green>, #<Color:0x163d5980 @name=:blue>]
27
+ #
28
+ module TypedEnum
29
+
30
+ attr_reader :name
31
+ def initialize(name)
32
+ @name = name
33
+ end
34
+
35
+ def clone
36
+ raise TypeError, "Can't clone instance of TypedEnum #{self.class}"
37
+ end
38
+ def dup
39
+ raise TypeError, "Can't dup instance of TypedEnum #{self.class}"
40
+ end
41
+ def dump(depth=-1)
42
+ @name.to_s
43
+ end
44
+ end
45
+
46
+ class << TypedEnum
47
+ module TypedEnumClassMethods
48
+ def from_name(sym)
49
+ inverse_map[sym.to_sym] or raise NameError.new("No mapping for symbol #{sym} to vertical")
50
+ end
51
+
52
+ def parse(token)
53
+ sym = token.to_sym
54
+ from_symbol(sym)
55
+ end
56
+
57
+ def values
58
+ @@values
59
+ end
60
+
61
+ def _load(str)
62
+ parse(str.to_sym)
63
+ end
64
+
65
+ @initialized = false
66
+ def init
67
+ if !@initialized
68
+ @initialized = true
69
+ @@values = @enum.call
70
+ else
71
+ raise "Attempt to re-initialize typed enum #{self.class}"
72
+ end
73
+ end
74
+
75
+ private
76
+
77
+ def inverse_map
78
+ @@inverse_map ||= values.inject({}) { |m, v| m[v.name] = v; m }
79
+ end
80
+
81
+ end
82
+
83
+ def TypedEnum.included(klass)
84
+ super
85
+ klass.extend TypedEnumClassMethods
86
+ klass.init
87
+ klass.private_class_method :new, :allocate
88
+ end
89
+ end
90
+
91
+ if __FILE__ == $0
92
+
93
+ class Foo
94
+ @enum = lambda { [BOOKS = self.new(:books), GAMES = self.new(:games)] }
95
+ include TypedEnum
96
+ end
97
+
98
+ require 'pp'
99
+
100
+ pp (Foo.methods - Object.methods).sort
101
+
102
+ pp Foo.values
103
+
104
+ end
@@ -0,0 +1,17 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+
12
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ require 'typed_enum'
15
+
16
+ class Test::Unit::TestCase
17
+ end
@@ -0,0 +1,38 @@
1
+ require 'helper'
2
+ require 'typed_enum'
3
+
4
+ class TestTypedEnum < Test::Unit::TestCase
5
+
6
+ class Color
7
+ @enum = lambda {[
8
+ RED = self.new(:red),
9
+ GREEN = self.new(:green),
10
+ BLUE = self.new(:blue)
11
+ ]}
12
+ include TypedEnum
13
+ end
14
+
15
+
16
+ def test_get_name
17
+ assert_equal :red, Color::RED.name
18
+ end
19
+
20
+ def test_from_name_by_symbol_gets_right_instance
21
+ assert_equal Color::RED, Color.from_name(:red)
22
+ end
23
+
24
+ def test_from_name_by_string_gets_right_instance
25
+ assert_equal Color::BLUE, Color.from_name('blue')
26
+ end
27
+
28
+ def test_missing_constant_raises_error
29
+ assert_raise NameError do
30
+ Color::PURPLE
31
+ end
32
+ end
33
+
34
+ def test_get_values_returns_values_in_order
35
+ assert_equal [Color::RED, Color::GREEN, Color::BLUE], Color.values
36
+ end
37
+
38
+ end
@@ -0,0 +1,59 @@
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 = %q{typed-enum}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Chris Tucker"]
12
+ s.date = %q{2011-04-07}
13
+ s.description = %q{A type-safe(ish) way to provide a constrained set of enumerated values that support basic enumeration, serialization/deserialization, and comparison}
14
+ s.email = %q{ctucker@glyde.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "LICENSE.txt",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/typed_enum.rb",
27
+ "test/helper.rb",
28
+ "test/test_typed_enum.rb",
29
+ "typed-enum.gemspec"
30
+ ]
31
+ s.homepage = %q{http://github.com/ctucker/typed-enum}
32
+ s.licenses = ["MIT"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.7.2}
35
+ s.summary = %q{Typed, restricted enumerated set of values}
36
+ s.test_files = [
37
+ "test/helper.rb",
38
+ "test/test_typed_enum.rb"
39
+ ]
40
+
41
+ if s.respond_to? :specification_version then
42
+ s.specification_version = 3
43
+
44
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
45
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
46
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
47
+ s.add_development_dependency(%q<rcov>, [">= 0"])
48
+ else
49
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
50
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
51
+ s.add_dependency(%q<rcov>, [">= 0"])
52
+ end
53
+ else
54
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
55
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
56
+ s.add_dependency(%q<rcov>, [">= 0"])
57
+ end
58
+ end
59
+
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: typed-enum
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
+ - Chris Tucker
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-04-07 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ version_requirements: &id001 !ruby/object:Gem::Requirement
22
+ none: false
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ hash: 23
27
+ segments:
28
+ - 1
29
+ - 0
30
+ - 0
31
+ version: 1.0.0
32
+ name: bundler
33
+ prerelease: false
34
+ type: :development
35
+ requirement: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ version_requirements: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ~>
41
+ - !ruby/object:Gem::Version
42
+ hash: 7
43
+ segments:
44
+ - 1
45
+ - 5
46
+ - 2
47
+ version: 1.5.2
48
+ name: jeweler
49
+ prerelease: false
50
+ type: :development
51
+ requirement: *id002
52
+ - !ruby/object:Gem::Dependency
53
+ version_requirements: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ name: rcov
63
+ prerelease: false
64
+ type: :development
65
+ requirement: *id003
66
+ description: A type-safe(ish) way to provide a constrained set of enumerated values that support basic enumeration, serialization/deserialization, and comparison
67
+ email: ctucker@glyde.com
68
+ executables: []
69
+
70
+ extensions: []
71
+
72
+ extra_rdoc_files:
73
+ - LICENSE.txt
74
+ - README.rdoc
75
+ files:
76
+ - .document
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.rdoc
80
+ - Rakefile
81
+ - VERSION
82
+ - lib/typed_enum.rb
83
+ - test/helper.rb
84
+ - test/test_typed_enum.rb
85
+ - typed-enum.gemspec
86
+ homepage: http://github.com/ctucker/typed-enum
87
+ licenses:
88
+ - MIT
89
+ post_install_message:
90
+ rdoc_options: []
91
+
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ hash: 3
100
+ segments:
101
+ - 0
102
+ version: "0"
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ hash: 3
109
+ segments:
110
+ - 0
111
+ version: "0"
112
+ requirements: []
113
+
114
+ rubyforge_project:
115
+ rubygems_version: 1.7.2
116
+ signing_key:
117
+ specification_version: 3
118
+ summary: Typed, restricted enumerated set of values
119
+ test_files:
120
+ - test/helper.rb
121
+ - test/test_typed_enum.rb