ruby-enum 0.1.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,12 @@
1
+ ### 0.3.0 (Next Release)
2
+
3
+ * Your contribution here.
4
+
5
+ ### 0.2.1 (5/15/2013)
6
+
7
+ * Added `Ruby::Enum#values`, `Ruby::Enum#keys` and `Ruby::Enum#to_h` - [@dblock](https://github.com/dblock).
8
+ * A `Ruby::Enum::Errors::UninitializedConstantError` will now be raised when referencing an undefined enum - [@dblock](https://github.com/dblock).
9
+
1
10
  ### 0.1.0 (5/14/2013)
2
11
 
3
- * Initial public release - [@dblock](https://github.com/dblock).
12
+ * Initial public release, live-coded during [May 2013 NYC.rb](http://code.dblock.org/your-first-ruby-gem) - [@dblock](https://github.com/dblock).
data/Gemfile CHANGED
@@ -3,4 +3,4 @@ source "http://rubygems.org"
3
3
  gemspec
4
4
 
5
5
  gem "rspec"
6
- gem "rake"
6
+ gem "rake"
@@ -0,0 +1,28 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ruby-enum (0.2.1)
5
+ i18n
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.2.4)
11
+ i18n (0.6.4)
12
+ rake (10.0.4)
13
+ rspec (2.13.0)
14
+ rspec-core (~> 2.13.0)
15
+ rspec-expectations (~> 2.13.0)
16
+ rspec-mocks (~> 2.13.0)
17
+ rspec-core (2.13.1)
18
+ rspec-expectations (2.13.0)
19
+ diff-lcs (>= 1.1.3, < 2.0)
20
+ rspec-mocks (2.13.1)
21
+
22
+ PLATFORMS
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ rake
27
+ rspec
28
+ ruby-enum!
data/README.md CHANGED
@@ -3,9 +3,9 @@ Ruby::Enum
3
3
 
4
4
  [![Build Status](https://travis-ci.org/dblock/ruby-enum.png)](https://travis-ci.org/dblock/ruby-enum)
5
5
 
6
- A handy library for defining enums in Ruby.
6
+ Enum-like behavior for Ruby, heavily inspired by [this](http://www.rubyfleebie.com/enumerations-and-ruby) and improved upon [another blog post](http://code.dblock.org/how-to-define-enums-in-ruby).
7
7
 
8
- ### Usage
8
+ ## Usage
9
9
 
10
10
  ``` ruby
11
11
  class Colors
@@ -16,18 +16,37 @@ class Colors
16
16
  end
17
17
  ```
18
18
 
19
- ### Contributing
19
+ ### Referencing
20
+
21
+ ``` ruby
22
+ Colors::RED # "red"
23
+ Colors::GREEN # "green"
24
+ Colors::UNDEFINED # raises Ruby::Enum::Errors::UninitializedConstantError
25
+ Colors.keys # [ :RED, :GREEN ]
26
+ Colors.values # [ "red", "green" ]
27
+ Colors.to_h # { :RED => "red", :GREEN => "green" }
28
+ ```
29
+
30
+ ### Iterating
31
+
32
+ ``` ruby
33
+ Colors.each do |key, enum|
34
+ # key and enum.key is :RED, :GREEN
35
+ # enum.value is "red", "green"
36
+ end
37
+ ```
38
+
39
+ ## Contributing
20
40
 
21
41
  You're encouraged to contribute to this gem.
22
42
 
23
43
  * Fork this project.
24
44
  * Make changes, write tests.
25
- * Updated CHANGELOG.
45
+ * Updated [CHANGELOG](CHANGELOG.md).
26
46
  * Make a pull request, bonus points for topic branches.
27
47
 
28
- ### Copyright and License
29
-
30
- Copyright Daniel Doubrovkine and Contributors, 2013
48
+ ## Copyright and License
31
49
 
32
- [MIT License](LICENSE.md)
50
+ Copyright (c) 2013, Daniel Doubrovkine and [Contributors](CHANGELOG.md).
33
51
 
52
+ This project is licensed under the [MIT License](LICENSE.md).
@@ -0,0 +1,14 @@
1
+ en:
2
+ ruby:
3
+ enum:
4
+ errors:
5
+ messages:
6
+ uninitialized_constant:
7
+ message: "Uninitialized constant."
8
+ summary: "The constant %{name}::%{key} has not been defined."
9
+ resolution: "The enumerated value could not be found in class %{name}. Use 'define' to declare it.\n
10
+ \_Example:\n
11
+ \_\_module %{name}\n
12
+ \_\_\_include Ruby::Enum\n
13
+ \_\_\_define %{key}, 'value'\n
14
+ \_\_end"
@@ -1,3 +1,10 @@
1
+ require 'i18n'
2
+
1
3
  require 'ruby-enum/version'
2
4
  require 'ruby-enum/enum'
3
5
 
6
+ I18n.load_path << File.join(File.dirname(__FILE__), 'config', 'locales', 'en.yml')
7
+
8
+ require 'ruby-enum/errors/base'
9
+ require 'ruby-enum/errors/uninitialized_constant_error'
10
+
@@ -2,7 +2,7 @@ module Ruby
2
2
  module Enum
3
3
 
4
4
  attr_reader :key, :value
5
-
5
+
6
6
  def initialize(key, value)
7
7
  @key = key
8
8
  @value = value
@@ -14,36 +14,65 @@ module Ruby
14
14
 
15
15
  module ClassMethods
16
16
 
17
+ # Define an enumerated value.
18
+ #
19
+ # === Parameters
20
+ # [key] Enumerator key.
21
+ # [value] Enumerator value.
17
22
  def define(key, value)
18
- @hash ||= {}
19
- @hash[key] = self.new(key, value)
23
+ @_enum_hash ||= {}
24
+ @_enum_hash[key] = self.new(key, value)
20
25
  end
21
26
 
22
27
  def const_missing(key)
23
- if @hash[key]
24
- @hash[key].value
28
+ if @_enum_hash[key]
29
+ @_enum_hash[key].value
25
30
  else
26
31
  raise Ruby::Enum::Errors::UninitializedConstantError.new({ :name => name, :key => key })
27
32
  end
28
33
  end
29
34
 
35
+ # Iterate over all enumerated values.
36
+ # Yields a key and an enumerated instance.
30
37
  def each(&block)
31
- @hash.each do |key, value|
38
+ @_enum_hash.each do |key, value|
32
39
  yield key, value
33
40
  end
34
41
  end
35
42
 
43
+ # Attempt to parse an enumerated value.
44
+ #
45
+ # === Parameters
46
+ # [s] The string to parse.
47
+ #
48
+ # Returns an enumerated value or nil.
36
49
  def parse(s)
37
50
  s = s.to_s.upcase
38
- each do |key, value|
51
+ each do |key, enum|
39
52
  if key.to_s.upcase == s
40
- return value.value
53
+ return enum.value
41
54
  end
42
55
  end
43
56
  nil
44
- end
57
+ end
58
+
59
+ # Returns all enum keys.
60
+ def keys
61
+ @_enum_hash.values.map(&:key)
62
+ end
63
+
64
+ # Returns all enum values.
65
+ def values
66
+ @_enum_hash.values.map(&:value)
67
+ end
68
+
69
+ def to_h
70
+ Hash[@_enum_hash.map do |key, enum|
71
+ [ key, enum.value ]
72
+ end]
73
+ end
45
74
 
46
75
  end
47
76
 
48
77
  end
49
- end
78
+ end
@@ -0,0 +1,81 @@
1
+ module Ruby
2
+ module Enum
3
+ module Errors
4
+ class Base < StandardError
5
+
6
+ # Problem occurred.
7
+ attr_reader :problem
8
+
9
+ # Summary of the problem.
10
+ attr_reader :summary
11
+
12
+ # Suggested problem resolution.
13
+ attr_reader :resolution
14
+
15
+ # Compose the message.
16
+ # === Parameters
17
+ # [key] Lookup key in the translation table.
18
+ # [attributes] The objects to pass to create the message.
19
+ def compose_message(key, attributes = {})
20
+ @problem = create_problem(key, attributes)
21
+ @summary = create_summary(key, attributes)
22
+ @resolution = create_resolution(key, attributes)
23
+
24
+ "\nProblem:\n #{@problem}"+
25
+ "\nSummary:\n #{@summary}"+
26
+ "\nResolution:\n #{@resolution}"
27
+ end
28
+
29
+ private
30
+
31
+ BASE_KEY = "ruby.enum.errors.messages" #:nodoc:
32
+
33
+ # Given the key of the specific error and the options hash, translate the
34
+ # message.
35
+ #
36
+ # === Parameters
37
+ # [key] The key of the error in the locales.
38
+ # [options] The objects to pass to create the message.
39
+ #
40
+ # Returns a localized error message string.
41
+ def translate(key, options)
42
+ ::I18n.translate("#{BASE_KEY}.#{key}", { :locale => :en }.merge(options)).strip
43
+ end
44
+
45
+ # Create the problem.
46
+ #
47
+ # === Parameters
48
+ # [key] The error key.
49
+ # [attributes] The attributes to interpolate.
50
+ #
51
+ # Returns the problem.
52
+ def create_problem(key, attributes)
53
+ translate("#{key}.message", attributes)
54
+ end
55
+
56
+ # Create the summary.
57
+ #
58
+ # === Parameters
59
+ # [key] The error key.
60
+ # [attributes] The attributes to interpolate.
61
+ #
62
+ # Returns the summary.
63
+ def create_summary(key, attributes)
64
+ translate("#{key}.summary", attributes)
65
+ end
66
+
67
+ # Create the resolution.
68
+ #
69
+ # === Parameters
70
+ # [key] The error key.
71
+ # [attributes] The attributes to interpolate.
72
+ #
73
+ # Returns the resolution.
74
+ def create_resolution(key, attributes)
75
+ translate("#{key}.resolution", attributes)
76
+ end
77
+
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,11 @@
1
+ module Ruby
2
+ module Enum
3
+ module Errors
4
+ class UninitializedConstantError < Base
5
+ def initialize(attrs)
6
+ super(compose_message("uninitialized_constant", attrs))
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -1,5 +1,5 @@
1
1
  module Ruby
2
2
  module Enum
3
- VERSION = '0.1.0'
3
+ VERSION = '0.2.1'
4
4
  end
5
5
  end
Binary file
Binary file
@@ -8,9 +8,10 @@ Gem::Specification.new do |s|
8
8
  s.email = "dblock@dblock.org"
9
9
  s.platform = Gem::Platform::RUBY
10
10
  s.required_rubygems_version = '>= 1.3.6'
11
- s.files = `git ls-files`.split("\n")
11
+ s.files = Dir['**/*']
12
12
  s.require_paths = [ "lib" ]
13
13
  s.homepage = "http://github.com/dblock/ruby-enum"
14
14
  s.licenses = [ "MIT" ]
15
15
  s.summary = "Enum-like behavior for Ruby."
16
+ s.add_dependency "i18n"
16
17
  end
@@ -12,7 +12,25 @@ describe Ruby::Enum do
12
12
  Colors::RED.should eq "red"
13
13
  Colors::GREEN.should eq "green"
14
14
  end
15
- context "parse" do
15
+ it "raises UninitializedConstantError on an invalid constant" do
16
+ expect { Colors::ANYTHING }.to raise_error Ruby::Enum::Errors::UninitializedConstantError
17
+ end
18
+ context "#each" do
19
+ it "iterates over constants" do
20
+ keys = []
21
+ enum_keys = []
22
+ enum_values = []
23
+ Colors.each do |key, enum|
24
+ keys << key
25
+ enum_keys << enum.key
26
+ enum_values << enum.value
27
+ end
28
+ keys.should == [ :RED, :GREEN ]
29
+ enum_keys.should == [ :RED, :GREEN ]
30
+ enum_values.should == [ "red", "green" ]
31
+ end
32
+ end
33
+ context "#parse" do
16
34
  it "parses exact value" do
17
35
  Colors.parse("red").should == Colors::RED
18
36
  end
@@ -26,4 +44,19 @@ describe Ruby::Enum do
26
44
  Colors.parse("invalid").should be_nil
27
45
  end
28
46
  end
47
+ context "#keys" do
48
+ it "returns keys" do
49
+ Colors.keys.should == [ :RED, :GREEN ]
50
+ end
51
+ end
52
+ context "#values" do
53
+ it "returns values" do
54
+ Colors.values.should == [ "red", "green" ]
55
+ end
56
+ end
57
+ context "#to_h" do
58
+ it "returns a hash of key:values" do
59
+ Colors.to_h.should == { :RED => "red", :GREEN => "green" }
60
+ end
61
+ end
29
62
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,26 +9,45 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-14 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2013-05-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: i18n
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
14
30
  description:
15
31
  email: dblock@dblock.org
16
32
  executables: []
17
33
  extensions: []
18
34
  extra_rdoc_files: []
19
35
  files:
20
- - .gitignore
21
- - .rspec
22
- - .travis.yml
23
36
  - CHANGELOG.md
24
37
  - Gemfile
25
- - LICENSE.md
26
- - README.md
27
- - Rakefile
28
- - lib/ruby-enum.rb
38
+ - Gemfile.lock
39
+ - lib/config/locales/en.yml
29
40
  - lib/ruby-enum/enum.rb
41
+ - lib/ruby-enum/errors/base.rb
42
+ - lib/ruby-enum/errors/uninitialized_constant_error.rb
30
43
  - lib/ruby-enum/version.rb
44
+ - lib/ruby-enum.rb
31
45
  - lib/ruby_enum.rb
46
+ - LICENSE.md
47
+ - pkg/ruby-enum-0.2.0.gem
48
+ - pkg/ruby-enum-0.3.0.gem
49
+ - Rakefile
50
+ - README.md
32
51
  - ruby-enum.gemspec
33
52
  - spec/ruby-enum/enum_spec.rb
34
53
  - spec/ruby-enum/version_spec.rb
@@ -48,7 +67,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
48
67
  version: '0'
49
68
  segments:
50
69
  - 0
51
- hash: 388465617
70
+ hash: 4593317306755494398
52
71
  required_rubygems_version: !ruby/object:Gem::Requirement
53
72
  none: false
54
73
  requirements:
@@ -57,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
57
76
  version: 1.3.6
58
77
  requirements: []
59
78
  rubyforge_project:
60
- rubygems_version: 1.8.24
79
+ rubygems_version: 1.8.25
61
80
  signing_key:
62
81
  specification_version: 3
63
82
  summary: Enum-like behavior for Ruby.
data/.gitignore DELETED
@@ -1,2 +0,0 @@
1
- Gemfile.lock
2
- pkg
data/.rspec DELETED
@@ -1,2 +0,0 @@
1
- --format documentation
2
- --color
@@ -1,6 +0,0 @@
1
- rvm:
2
- - 1.8.7
3
- - 1.9.3
4
- - 2.0.0
5
- - jruby-19mode
6
- - rbx-19mode