another_enum 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NGY2Y2NlMzMyM2Q0YjdjYTA1MzNjMDU3MDM2MGFhMWNjMmMxNTI5MA==
5
+ data.tar.gz: !binary |-
6
+ MGE4Yzg3OWViY2I1ODJiNGQ4NTFmMmU3NzgyNjEyMDZhMWFmY2M2Zg==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ ZWQ4OWE2OTVkNjA0NDdiYWVmZTJlYjM1Yjk1YWExOWFkMzg5YjRkMmEwMjUw
10
+ MjNlZmZhZWE4NGQ1MDIwZDgzZTk3ZTM5Nzc0MzM2NjI4YmE0NDU3NjZmYWYx
11
+ MTU0Njg2OGQwOGRjY2FkYzhiMTg3MTZlZTNiNWYyZjMwNGFkYmY=
12
+ data.tar.gz: !binary |-
13
+ YTA2M2NlM2ExOWZmMTVlZmM3NjllOTkwYzMyYmZlNTZhZGY4NmU5ZDdjNzg0
14
+ ZmU5MjIxNjJmMmYyZmEyNmRmMTkwZTM1YjM2ZmJhNjRmYzUyNWUwOGFmMTc4
15
+ ODU4YjRhMmUwY2NmYzYyNTFkMTE3YjZmNzQwYjYxNGQ5NmQ4MjA=
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in enum_type.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Mike Williams <mdub@dogbiscuit.org>
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,76 @@
1
+ # AnotherEnum
2
+
3
+ `AnotherEnum` provides support for defining enumerated types in Ruby. An enumerated type (or "enum") is a class with a finite (and usually small) set of predefined, named values.
4
+
5
+ ## Usage
6
+
7
+ Define a enumerated type by subclassing AnotherEnum. Use `.define` to define allowed values:
8
+
9
+ require 'another_enum'
10
+
11
+ class Colour < AnotherEnum
12
+ define :red
13
+ define :green
14
+ define :blue
15
+ end
16
+
17
+ which become globally available:
18
+
19
+ Colour.red
20
+ Colour.green
21
+ Colour.blue
22
+
23
+ Each value gets a code:
24
+
25
+ Colour.red.code #=> "red"
26
+
27
+ which can be used later to lookup the value:
28
+
29
+ Colour["red"] #=> Colour.red
30
+
31
+ It's easy to get all the defined values, or all the codes:
32
+
33
+ Colour.all #=> [Colour.red, Colour.green, Colour.blue]
34
+ Colour.codes #=> ["red", "green", "blue"]
35
+
36
+ `AnotherEnum.define` takes a block, which can be used to define methods on the singleton values, e.g.
37
+
38
+ class CreditCardType < AnotherEnum
39
+
40
+ define :visa do
41
+
42
+ def surcharge
43
+ 0.08
44
+ end
45
+
46
+ end
47
+
48
+ define :amex do
49
+
50
+ def surcharge
51
+ 0.16
52
+ end
53
+
54
+ end
55
+
56
+ end
57
+
58
+ There's even a shortcut for defining methods that return predefined values:
59
+
60
+ class CreditCardType < AnotherEnum
61
+
62
+ define :visa do
63
+ hardcode name: "Visa"
64
+ hardcode surcharge: 0.08
65
+ end
66
+
67
+ define :amex do
68
+ hardcode name: "American Express"
69
+ hardcode surcharge: 0.16
70
+ end
71
+
72
+ end
73
+
74
+ ## Contributing
75
+
76
+ It's on GitHub. You know what to do.
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ task :default => :spec
4
+
5
+ require "rspec/core/rake_task"
6
+
7
+ RSpec::Core::RakeTask.new do |t|
8
+ t.pattern = 'spec/**/*_spec.rb'
9
+ end
@@ -0,0 +1,20 @@
1
+ # coding: utf-8
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "another_enum"
5
+ spec.version = "1.0.0"
6
+ spec.authors = ["Mike Williams"]
7
+ spec.email = ["mdub@dogbiscuit.org"]
8
+ spec.summary = %q{Support for defining enumerated types}
9
+ spec.homepage = "https://github.com/mdub/another_enum"
10
+ spec.license = "MIT"
11
+
12
+ spec.files = `git ls-files`.split($/)
13
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
14
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
15
+ spec.require_paths = ["lib"]
16
+
17
+ spec.add_development_dependency "rake", "~> 10.1"
18
+ spec.add_development_dependency "rspec", "~> 2.14"
19
+
20
+ end
@@ -0,0 +1,125 @@
1
+ # Provide support for defining enumerated types.
2
+ #
3
+ # Example
4
+ #
5
+ # class Colour < AnotherEnum
6
+ #
7
+ # define :red do
8
+ # # ...
9
+ # end
10
+ #
11
+ # define :green do
12
+ # # ...
13
+ # end
14
+ #
15
+ # define :blue do
16
+ # # ...
17
+ # end
18
+ #
19
+ # end
20
+ #
21
+ class AnotherEnum
22
+
23
+ class << self
24
+
25
+ protected :new # prevent arbitrary new instances
26
+
27
+ # Public: Declare a enumerated value.
28
+ #
29
+ # code - a unique (String) code that identifies this value
30
+ # block - optional block defines behaviour specific to this value
31
+ #
32
+ # Example
33
+ #
34
+ # class Colour < AnotherEnum; end
35
+ #
36
+ # Colour.define :red do
37
+ #
38
+ # def rgb
39
+ # "#ff0000"
40
+ # end
41
+ #
42
+ # end
43
+ #
44
+ def define(code, &block)
45
+ code = code.to_sym.to_s
46
+ value = generate_value(code, &block)
47
+ register_value(code, value)
48
+ end
49
+
50
+ # Public: Succintly declare methods to return hardcoded values.
51
+ #
52
+ # attributes - a Hash; a method will be generated for each key, returning
53
+ # the corresponding value
54
+ #
55
+ # Example
56
+ #
57
+ # Colour.define :red do
58
+ #
59
+ # hardcode :rgb => "#ff0000"
60
+ #
61
+ # end
62
+ #
63
+ def hardcode(attributes)
64
+ attributes.each do |name, value|
65
+ define_method(name) { value }
66
+ end
67
+ end
68
+
69
+ def get(arg)
70
+ return arg if self === arg
71
+ @all_by_code[arg.to_s]
72
+ end
73
+
74
+ alias [] get
75
+
76
+ def fetch(code)
77
+ get(code) || raise(ArgumentError, "no such #{self}: #{code.inspect}")
78
+ end
79
+
80
+ def all
81
+ @all_by_code.values
82
+ end
83
+
84
+ def codes
85
+ @all_by_code.keys
86
+ end
87
+
88
+ private
89
+
90
+ def generate_value(code, &block)
91
+ new.tap do |value|
92
+ value.singleton_class.class_eval do
93
+ define_method(:code) { code }
94
+ define_method("#{code}?") { true }
95
+ class_eval(&block) if block
96
+ end
97
+ end
98
+ end
99
+
100
+ def register_value(code, value)
101
+ singleton_class.send(:define_method, code) { value } # e.g. Brand.hooroo
102
+ class_eval "def #{code}?; false; end" # e.g. other_brand.hooroo?
103
+ @all_by_code ||= {}
104
+ @all_by_code[code] = value
105
+ end
106
+
107
+ end
108
+
109
+ def to_s
110
+ code
111
+ end
112
+
113
+ def inspect
114
+ "#{self.class}.#{code}"
115
+ end
116
+
117
+ end
118
+
119
+ module Kernel
120
+ def singleton_class
121
+ class << self
122
+ self
123
+ end
124
+ end unless respond_to?(:singleton_class)
125
+ end
@@ -0,0 +1,71 @@
1
+ require 'another_enum'
2
+
3
+ describe AnotherEnum, :fast do
4
+
5
+ let(:direction_class) do
6
+ Class.new(AnotherEnum) do
7
+ define :north
8
+ define :south
9
+ define :east
10
+ define :west
11
+ end
12
+ end
13
+
14
+ describe ".define" do
15
+
16
+ it "defines a new value" do
17
+ direction_class.north.should be_kind_of(direction_class)
18
+ end
19
+
20
+ context "with a block" do
21
+
22
+ before do
23
+ direction_class.define :up do
24
+ def saying
25
+ "Up up and away!"
26
+ end
27
+ end
28
+ end
29
+
30
+ it "extends the behaviour of the specified enum value" do
31
+ direction_class.up.saying.should eq("Up up and away!")
32
+ end
33
+
34
+ it "does not affect other classes" do
35
+ direction_class.north.should_not respond_to(:saying)
36
+ end
37
+
38
+ end
39
+
40
+ end
41
+
42
+ describe ".all" do
43
+
44
+ it "returns all the defined values" do
45
+ direction_class.all.should eq([
46
+ direction_class.north,
47
+ direction_class.south,
48
+ direction_class.east,
49
+ direction_class.west
50
+ ])
51
+ end
52
+
53
+ end
54
+
55
+ describe ".codes" do
56
+
57
+ it "returns all the codes" do
58
+ direction_class.codes.should eq(%w(north south east west))
59
+ end
60
+
61
+ end
62
+
63
+ describe "#code" do
64
+
65
+ it "returns the value's unique code" do
66
+ direction_class.north.code.should eq('north')
67
+ end
68
+
69
+ end
70
+
71
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: another_enum
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Mike Williams
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '10.1'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '10.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '2.14'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '2.14'
41
+ description:
42
+ email:
43
+ - mdub@dogbiscuit.org
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - .rspec
50
+ - Gemfile
51
+ - LICENSE
52
+ - README.md
53
+ - Rakefile
54
+ - another_enum.gemspec
55
+ - lib/another_enum.rb
56
+ - spec/another_enum_spec.rb
57
+ homepage: https://github.com/mdub/another_enum
58
+ licenses:
59
+ - MIT
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 2.0.7
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: Support for defining enumerated types
81
+ test_files:
82
+ - spec/another_enum_spec.rb