ruby_enum 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 88208684028da60b266f75674a01986da03faff7
4
- data.tar.gz: da4214d467a22efce5178fcf838b7001209841ba
3
+ metadata.gz: 113d01a02e7c03f45bfd760a6b6c531f9a63011d
4
+ data.tar.gz: 9f2d6107d1279bbbfaa0b2fd374bca2bf764a1e4
5
5
  SHA512:
6
- metadata.gz: fcdfc3364540ee74a798f98639525b13a433386ca2d3393e37720b2857105476edcfac97eab538415a79fc2442ce23c63bee01a39a64eef076d9dd1e73e50d5d
7
- data.tar.gz: 57ef03195f5a6ccf80c3379d55d1709002e779e80ad2ade438bfea2360279c36b1c47a139205593e2e73832a7696ef5309c181a933f1658f362741d03de1109f
6
+ metadata.gz: 21714b280c186a89e71fecac56bac2527b0ff9b3c41325c19915f0f5be692cd8de2c6b2361ccfa8868ddbcacc264b0980d5b0898ffa4a9d24b858d7a84676dcb
7
+ data.tar.gz: 1cc669a45f6e49df3734dfaff82d83085e70b77d40b1b96965f2ade6a2bf90097888d1ba836e02a6628cba292a06806202839672dc26a3fbf3c6f38d0b9e3ae2
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.1
6
+ - 2.2.2
7
+ script: bundle exec rspec spec
8
+ branches:
9
+ only:
10
+ - master
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ruby_enum (0.4.0)
4
+ ruby_enum (0.4.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -30,3 +30,6 @@ DEPENDENCIES
30
30
  rake (~> 10.0)
31
31
  rspec (~> 3.3.0)
32
32
  ruby_enum!
33
+
34
+ BUNDLED WITH
35
+ 1.10.6
data/README.md CHANGED
@@ -1,3 +1,6 @@
1
+ [![Gem
2
+ Version](https://badge.fury.io/rb/ruby_enum.svg)](https://badge.fury.io/rb/ruby_enum)
3
+
1
4
  # RubyEnum
2
5
 
3
6
  A simple enumeration type for ruby.
@@ -20,55 +23,161 @@ Or install it yourself as:
20
23
 
21
24
  ## Usage
22
25
 
23
- To create an enumeration type simply include the `RubyEnum` module in your
24
- enumeration class and provide the enumeration's values as follows:
26
+ To create an enumeration type include the `RubyEnum` module in your enumeration
27
+ class and provide the enumeration's values as follows:
28
+
25
29
  ```ruby
26
- class Coordinates
30
+ class Coordinate
27
31
  include RubyEnum
28
32
 
29
33
  enum :north
30
34
  enum :south
31
35
  enum :west
32
36
  enum :east
37
+ end
33
38
  ```
39
+ Note that you can't define the same enumeration instance twice. An error will
40
+ be raised in this case, for example:
34
41
 
35
- Including `RubyEnum` in any class will make it a singleton. This means that you
36
- can't create new instances of your class using `new`, `allocate`, `clone` or
37
- `dup`. You can access an enumeration's value instances in three different ways:
38
-
39
- 1. Using a class method that matches the provided name of the enumeration
40
- value:
41
42
  ```ruby
42
- north = Coordinates.north
43
+ class InvalidEnumeration
44
+ include RubyEnum
45
+
46
+ enum :value
47
+ enum :value
48
+ end
43
49
  ```
44
50
 
45
- 2. Using a constant:
51
+ Including `RubyEnum` in any class will make it a singleton. You will not be able
52
+ to create new instances of your class using `new`, `allocate`, `clone` or
53
+ `dup`. You can access an enumeration's values in three different ways:
54
+
55
+ Using a class method that matches the enumeration instance name:
56
+
46
57
  ```ruby
47
- north = Coordinates::NORTH
58
+ north = Coordinate.north
48
59
  ```
49
60
 
50
- 3. Treating your enumeration class as a dictionary:
61
+ Using a constant that matches the enumeration instance name:
62
+
51
63
  ```ruby
52
- north = Coordinates[:north]
64
+ north = Coordinate::NORTH
53
65
  ```
54
66
 
55
- All three approaches are equivalent and subject to personal style.
67
+ Treating your enumeration class as a dictionary:
68
+
69
+ ```ruby
70
+ north = Coordinate[:north]
71
+ ```
72
+ Note that his method returns `nil` if no enumeration instance is found by the
73
+ specified name.
56
74
 
57
- To retrieve all values of an enumeration, simply invoke `all` method on your
75
+ To retrieve all enumeration instances simply use method `all` on your
58
76
  enumeration class:
77
+
59
78
  ```ruby
60
- coordinates = Coordinates.all
79
+ coordinates = Coordinate.all
61
80
  ```
62
81
 
63
- ## Development
82
+ ### Specifying associated values
64
83
 
65
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
84
+ Each enumeration instance has an implicit name and associated value attributes
85
+ accessible through the `name` and `value` methods. If no associated value is
86
+ specified for an enumeration instance in the definition of the enumeration
87
+ class, it will be implicitly set to its name:
66
88
 
67
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
89
+ ```ruby
90
+ north = Coordinate.north
91
+ north.name
92
+ # => :north
93
+ north.value
94
+ # => "north"
95
+ ```
96
+
97
+ You may provide a custom associated value to each enumeration instance following
98
+ its name in the declaration:
99
+
100
+ ```ruby
101
+ class Planet
102
+ include RubyEnum
103
+
104
+ enum :mercury, 1
105
+ enum :venus, 2
106
+ enum :earth, 3
107
+ end
108
+
109
+ mercury = Planet.mercury
110
+ mercury.name
111
+ # => :mercury
112
+ mercury.value
113
+ # => 1
114
+ ```
115
+
116
+ Associated values should be unique in the context of an enumeration class. An
117
+ error will be thrown in those cases, for example:
118
+
119
+ ```ruby
120
+ class InvalidEnumeration
121
+ include RubyEnum
122
+
123
+ enum :a, 1
124
+ enum :b, 1
125
+ end
126
+ ```
127
+
128
+ ### Finding enumerations by associated value
129
+
130
+ You can find an enumeration value by its associated value:
131
+
132
+ ```ruby
133
+ mercury = Planet.find_by_value 1
134
+ ```
135
+
136
+ If no enumeration instance is found with the specified associated value this
137
+ method returns `nil`. Using the `find_by_value!` version, an error is raised in
138
+ this case.
139
+
140
+ ### Testing enumeration classes
141
+
142
+ To test enumeration classes you can use the custom `be_an_enumeration` and
143
+ `define_enumeration_value` matchers.
144
+
145
+ ```ruby
146
+ require 'ruby_enum/rspec'
147
+
148
+ describe Coordinate do
149
+
150
+ it { should be_an_enumeration }
151
+ it { should define_enumeration_value :north }
152
+ it { should define_enumeration_value :north, 'north' }
153
+ end
154
+ ```
155
+
156
+ ### Adding custom methods
157
+
158
+ Nothing stops you from adding your own methods to an enumeration.
159
+
160
+ ```ruby
161
+ class Planet
162
+ include RubyEnum
163
+
164
+ enum :mercury, 1
165
+ enum :venus, 2
166
+ enum :earth, 3
167
+
168
+ def description
169
+ "#{name.to_s.capitalize} is the #{value.ordinalize} rock from the sun"
170
+ end
171
+ end
172
+
173
+ mercury = Planet::EARTH
174
+ mercury.description
175
+ # => "Earth is the 3rd rock from the sun"
176
+ ```
68
177
 
69
178
  ## Contributing
70
179
 
71
- 1. Fork it ( https://github.com/[my-github-username]/ruby_enum/fork )
180
+ 1. Fork it ( https://github.com/laskaridis/ruby_enum/fork )
72
181
  2. Create your feature branch (`git checkout -b my-new-feature`)
73
182
  3. Commit your changes (`git commit -am 'Add some feature'`)
74
183
  4. Push to the branch (`git push origin my-new-feature`)
@@ -1,3 +1,3 @@
1
1
  module RubyEnum
2
- VERSION = "0.4.0"
2
+ VERSION = "0.4.1"
3
3
  end
data/lib/ruby_enum.rb CHANGED
@@ -3,10 +3,11 @@ require "ruby_enum/version"
3
3
  module RubyEnum
4
4
 
5
5
  def self.included(base)
6
+ p base
6
7
  # enmeration instances are singleton values
7
8
  base.private_class_method(:new, :allocate)
8
- base.extend(ClassMethods)
9
- base.include(InstanceMethods)
9
+ base.send :extend, ClassMethods
10
+ base.send :include, InstanceMethods
10
11
  end
11
12
 
12
13
  module InstanceMethods
@@ -28,11 +29,12 @@ module RubyEnum
28
29
  end
29
30
  end
30
31
 
31
- # the value associated with the enumeration instance
32
+ # @return [String] the associated value of the enumeration instance
32
33
  def value
33
34
  @_value
34
35
  end
35
36
 
37
+ # @return [Symbol] the name of the enumeration instance
36
38
  def name
37
39
  @_name
38
40
  end
@@ -60,27 +62,36 @@ module RubyEnum
60
62
  self[name] || super
61
63
  end
62
64
 
63
- # defines enumeration values
64
65
  def enum(name, value = nil)
66
+ raise ArgumentError, 'name is required for an enumeration' if name.nil?
67
+
65
68
  normalized_name = _normalize name
66
69
  if self[normalized_name]
67
- raise ArgumentError, "An enumeration value for #{normalized_name} has " \
70
+ raise ArgumentError, "an enumeration value for #{normalized_name} has " \
68
71
  "already been defined in #{self.name}."
69
72
  else
70
73
  value = normalized_name.to_s unless value
74
+ if find_by_value value
75
+ raise ArgumentError, "duplicate associated value `#{value}` for enumeration" \
76
+ "with name `#{name}`"
77
+ end
71
78
  _define_instance_accessor_for normalized_name
72
79
  _enumeration_values[normalized_name] = new(normalized_name, value)
73
80
  end
74
81
  end
75
82
 
83
+ # @return the enumeration instance with the specified name or nil if none exists
76
84
  def [](name)
77
85
  _enumeration_values[_normalize(name)]
78
86
  end
79
87
 
88
+ # @return [Array] all enumeration instances defined in this enumeration class
80
89
  def all
81
90
  _enumeration_values.map { |_, instance| instance }
82
91
  end
83
92
 
93
+ # @return the enumeration instance with the specified associated value
94
+ # @raise [ArgumentError] when no enumeration instance with the associated value is found
84
95
  def find_by_value!(value)
85
96
  result = find_by_value(value)
86
97
  unless result
@@ -90,6 +101,7 @@ module RubyEnum
90
101
  result
91
102
  end
92
103
 
104
+ # @return the enumeration instance with the specifed associated value
93
105
  def find_by_value(value)
94
106
  all.find { |instance| instance.value == value }
95
107
  end
@@ -102,10 +114,14 @@ module RubyEnum
102
114
 
103
115
  def _define_instance_accessor_for(name)
104
116
  # check {http://ryanangilly.com/post/234897271/dynamically-adding-class-methods-in-ruby}
117
+ _metaclass.instance_eval do
118
+ define_method(name) { return self[name] }
119
+ end
120
+ end
121
+
122
+ def _metaclass
105
123
  class << self
106
124
  self
107
- end.instance_eval do
108
- define_method(name) { return self[name] }
109
125
  end
110
126
  end
111
127
 
metadata CHANGED
@@ -1,55 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lefteris Laskaridis
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-10-15 00:00:00.000000000 Z
11
+ date: 2015-10-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ~>
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.8'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ~>
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.8'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ~>
32
32
  - !ruby/object:Gem::Version
33
33
  version: '10.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ~>
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ~>
46
46
  - !ruby/object:Gem::Version
47
47
  version: 3.3.0
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ~>
53
53
  - !ruby/object:Gem::Version
54
54
  version: 3.3.0
55
55
  description: Implementation of a simple enumeration type for ruby
@@ -59,7 +59,8 @@ executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
- - ".gitignore"
62
+ - .gitignore
63
+ - .travis.yml
63
64
  - Gemfile
64
65
  - Gemfile.lock
65
66
  - LICENSE.txt
@@ -71,7 +72,6 @@ files:
71
72
  - lib/ruby_enum/rspec.rb
72
73
  - lib/ruby_enum/version.rb
73
74
  - ruby_enum.gemspec
74
- - tags
75
75
  homepage: https://github.com/laskaridis/ruby_enum
76
76
  licenses:
77
77
  - MIT
@@ -82,17 +82,17 @@ require_paths:
82
82
  - lib
83
83
  required_ruby_version: !ruby/object:Gem::Requirement
84
84
  requirements:
85
- - - ">="
85
+ - - '>='
86
86
  - !ruby/object:Gem::Version
87
87
  version: '0'
88
88
  required_rubygems_version: !ruby/object:Gem::Requirement
89
89
  requirements:
90
- - - ">="
90
+ - - '>='
91
91
  - !ruby/object:Gem::Version
92
92
  version: '0'
93
93
  requirements: []
94
94
  rubyforge_project:
95
- rubygems_version: 2.4.6
95
+ rubygems_version: 2.4.8
96
96
  signing_key:
97
97
  specification_version: 4
98
98
  summary: Simple enumeration type for ruby
data/tags DELETED
@@ -1,20 +0,0 @@
1
- !_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/
2
- !_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/
3
- !_TAG_PROGRAM_AUTHOR Darren Hiebert /dhiebert@users.sourceforge.net/
4
- !_TAG_PROGRAM_NAME Exuberant Ctags //
5
- !_TAG_PROGRAM_URL http://ctags.sourceforge.net /official site/
6
- !_TAG_PROGRAM_VERSION 5.9~svn20110310 //
7
- ClassMethods lib/ruby_enum.rb /^ module ClassMethods$/;" m class:RubyEnum
8
- Coordinates spec/ruby_enum_spec.rb /^ class Coordinates$/;" c
9
- Member lib/ruby_enum/member.rb /^ class Member$/;" c class:RubyEnum
10
- RubyEnum lib/ruby_enum.rb /^module RubyEnum$/;" m
11
- RubyEnum lib/ruby_enum/member.rb /^module RubyEnum$/;" m
12
- RubyEnum lib/ruby_enum/version.rb /^module RubyEnum$/;" m
13
- [] lib/ruby_enum.rb /^ def [](member_name)$/;" f class:RubyEnum.ClassMethods
14
- enum lib/ruby_enum.rb /^ def enum(name, value = nil)$/;" f class:RubyEnum.ClassMethods
15
- enumeration_members lib/ruby_enum.rb /^ def enumeration_members$/;" f class:RubyEnum.ClassMethods
16
- enumeration_value_for lib/ruby_enum.rb /^ def enumeration_value_for(member_name)$/;" f class:RubyEnum.ClassMethods
17
- find_enumeration_member_by lib/ruby_enum.rb /^ def find_enumeration_member_by(name)$/;" f class:RubyEnum.ClassMethods
18
- included lib/ruby_enum.rb /^ def self.included(base)$/;" F class:RubyEnum
19
- initialize lib/ruby_enum/member.rb /^ def initialize(name, value = nil)$/;" f class:RubyEnum.Member
20
- method_missing lib/ruby_enum.rb /^ def method_missing(name, *args, &blk)$/;" f class:RubyEnum