ez_enum 0.0.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 +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +80 -0
- data/Rakefile +21 -0
- data/ez_enum.gemspec +27 -0
- data/lib/ez_enum.rb +50 -0
- data/lib/ez_enum/version.rb +3 -0
- data/spec/examples/status_enum.rb +12 -0
- data/spec/ez_enum/ez_enum_spec.rb +41 -0
- data/spec/locale/af.yml +10 -0
- data/spec/locale/en.yml +10 -0
- data/spec/spec_helper.rb +24 -0
- metadata +135 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 591248acf08c3df9097d00e274ace4789dce0bd5
|
4
|
+
data.tar.gz: 20003050e42cb1ec4d760dd39da1beb033d7ee3a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2d1e40be0d043bedf7a63855d8cd1b5a7a306bd3c3e8ca2d8e59e50d339867ec17acf211274dc94dd5c32972711122a9de353217b2cff2fd58ceee44e3e9d383
|
7
|
+
data.tar.gz: 74984966eb4edf650606041bf604e5bd2e6790f7a17e0df9e14e84728ad3eb4ccb2d102df4662608ab17f74d27e476b0990f11495e622c725b50093900fb9747
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.0.0-p0
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Chris Stefano
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
# EZEnum
|
2
|
+
|
3
|
+
Provides a simple abstraction for defining enumerations in a Ruby module. Supports listing members and localization of constant names.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'ez_enum'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install ez_enum
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Define a Ruby module, giving it the name of the desired enumeration. Add constants to the module as needed.
|
22
|
+
Include `EZEnum` into your module _after_ all constant declarations. E.g.
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
module Status
|
26
|
+
|
27
|
+
New = 1
|
28
|
+
Processing = 2
|
29
|
+
Failed = 3
|
30
|
+
Complete = 4
|
31
|
+
|
32
|
+
# included here, so that constants are used
|
33
|
+
include EZEnum
|
34
|
+
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
Your module will now include an `All` constant, which contains an array of the constants defined in the module. E.g.
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
Status::All # => [New, Processing, Failed, Complete]
|
42
|
+
```
|
43
|
+
|
44
|
+
Your will also have the following helper methods add to it:
|
45
|
+
|
46
|
+
* `choices_for_select` - returns an array of the localized names and values for each constant. E.g.
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
Status.choices_for_select # => [['New', 1], ['Processing', 2], ...]
|
50
|
+
```
|
51
|
+
|
52
|
+
* `display_for(value)` - returns the localized name for the given constant value. E.g.
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
Status.display_for(Status::Failed) # => 'Epic Fail'
|
56
|
+
```
|
57
|
+
|
58
|
+
To add support for localization, add sections under the respective language code in your locales file, and using the
|
59
|
+
downcased name of the module and the downcased name of the constant, provide the corresponding translation. E.g.
|
60
|
+
|
61
|
+
```yaml
|
62
|
+
en:
|
63
|
+
enums:
|
64
|
+
status:
|
65
|
+
new: New
|
66
|
+
processing: Processing
|
67
|
+
failed: Epic Fail
|
68
|
+
completed: Completed
|
69
|
+
```
|
70
|
+
|
71
|
+
## Contributing
|
72
|
+
|
73
|
+
1. Fork it
|
74
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
75
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
76
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
77
|
+
5. Create new Pull Request
|
78
|
+
|
79
|
+
## Copyright
|
80
|
+
MIT License - Copyright (c) 2013 Chris Stefano
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'bundler' rescue 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
2
|
+
require "bundler/gem_tasks"
|
3
|
+
|
4
|
+
require "rspec"
|
5
|
+
require 'rspec/core/rake_task'
|
6
|
+
|
7
|
+
RSpec::Core::RakeTask.new('spec') do |spec|
|
8
|
+
spec.pattern = "spec/**/*_spec.rb"
|
9
|
+
|
10
|
+
# Run specs in random order to surface order dependencies. If you find an
|
11
|
+
# order dependency and want to debug it, you can fix the order by providing
|
12
|
+
# the seed, which is printed after each run.
|
13
|
+
# --seed 1234
|
14
|
+
# spec.rspec_opts = '--order rand:16996'
|
15
|
+
end
|
16
|
+
|
17
|
+
task :default => :spec
|
18
|
+
|
19
|
+
namespace :ez_enum do
|
20
|
+
# TODO
|
21
|
+
end
|
data/ez_enum.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ez_enum/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ez_enum"
|
8
|
+
spec.version = EZEnum::VERSION
|
9
|
+
spec.authors = ["Chris Stefano"]
|
10
|
+
spec.email = ["virtualstaticvoid@gmail.com"]
|
11
|
+
spec.description = %q{Provides a simple abstraction for defining enumerations in a Ruby module. Supports listing members and localization of constant names.}
|
12
|
+
spec.summary = %q{Elegant Enumerations for Ruby and Rails}
|
13
|
+
spec.homepage = "https://github.com/virtualstaticvoid/ez_enum"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "activesupport"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "rspec"
|
26
|
+
spec.add_development_dependency "pry"
|
27
|
+
end
|
data/lib/ez_enum.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'i18n'
|
2
|
+
require 'active_support/inflector'
|
3
|
+
|
4
|
+
module EZEnum
|
5
|
+
|
6
|
+
class << self
|
7
|
+
|
8
|
+
def included(base)
|
9
|
+
|
10
|
+
const_names = base.constants(false)
|
11
|
+
|
12
|
+
const_names_by_value = const_names.inject({}) do |list, name|
|
13
|
+
list[base.const_get(name)] = name
|
14
|
+
list
|
15
|
+
end
|
16
|
+
|
17
|
+
# add an "All" constant
|
18
|
+
base.const_set :All, const_names_by_value.keys
|
19
|
+
|
20
|
+
# add helper methods to module
|
21
|
+
base.class_eval do
|
22
|
+
|
23
|
+
# provides an array of display and values pairs
|
24
|
+
# typcially for use in HTML select controls
|
25
|
+
define_singleton_method :choices_for_select do
|
26
|
+
const_names_by_value.collect do |value, name|
|
27
|
+
[translate_display_for(name), value]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# resolves for the display name for the given value
|
32
|
+
define_singleton_method :display_for do |value|
|
33
|
+
translate_display_for(const_names_by_value[value])
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
# lookup i18n translation for given name
|
39
|
+
define_singleton_method :translate_display_for do |const_name|
|
40
|
+
I18n.translate(
|
41
|
+
const_name.to_s.underscore.to_sym,
|
42
|
+
:scope => [:enums, base.name.underscore],
|
43
|
+
:default => const_name.to_s
|
44
|
+
)
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require './spec/spec_helper'
|
2
|
+
|
3
|
+
describe "ez_enum" do
|
4
|
+
|
5
|
+
before do
|
6
|
+
I18n.locale = :en
|
7
|
+
end
|
8
|
+
|
9
|
+
AllStatuses = [Status::New, Status::Processing, Status::Failed, Status::Complete]
|
10
|
+
|
11
|
+
describe "::All" do
|
12
|
+
it { defined?(Status::All).should_not be nil }
|
13
|
+
it { Status::All.should eq(AllStatuses) }
|
14
|
+
end
|
15
|
+
|
16
|
+
describe ".choices_for_select" do
|
17
|
+
it { Status.respond_to?(:choices_for_select).should be true }
|
18
|
+
it { Status.choices_for_select.should_not be nil }
|
19
|
+
it { Status.choices_for_select.length.should eq(AllStatuses.length) }
|
20
|
+
it { Status.choices_for_select.first.should eq(['New', 1])}
|
21
|
+
it "has correct display for switch in locale" do
|
22
|
+
I18n.locale = :af
|
23
|
+
Status.choices_for_select.first.should eq(['Nuwe', 1])
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe ".display_for" do
|
28
|
+
it { Status.respond_to?(:display_for).should be true }
|
29
|
+
it "provides display_for each member" do
|
30
|
+
Status::All.each do |status|
|
31
|
+
Status.display_for(status).should_not be nil
|
32
|
+
end
|
33
|
+
end
|
34
|
+
it { Status.display_for(Status::Failed).should eq('Epic Fail') }
|
35
|
+
it "has correct display for switch in locale" do
|
36
|
+
I18n.locale = :af
|
37
|
+
Status.display_for(Status::Failed).should eq('Gebreek')
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
data/spec/locale/af.yml
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
# Sample localization file for English. Add more files in this directory for other locales.
|
2
|
+
# See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
|
3
|
+
|
4
|
+
af:
|
5
|
+
enums:
|
6
|
+
status:
|
7
|
+
new: Nuwe
|
8
|
+
processing: Verwerking
|
9
|
+
failed: Gebreek
|
10
|
+
completed: Voltooi
|
data/spec/locale/en.yml
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
# Sample localization file for English. Add more files in this directory for other locales.
|
2
|
+
# See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
|
3
|
+
|
4
|
+
en:
|
5
|
+
enums:
|
6
|
+
status:
|
7
|
+
new: New
|
8
|
+
processing: Processing
|
9
|
+
failed: Epic Fail
|
10
|
+
completed: Completed
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
|
8
|
+
RSpec.configure do |config|
|
9
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
10
|
+
config.run_all_when_everything_filtered = true
|
11
|
+
config.filter_run :focus
|
12
|
+
|
13
|
+
# Run specs in random order to surface order dependencies. If you find an
|
14
|
+
# order dependency and want to debug it, you can fix the order by providing
|
15
|
+
# the seed, which is printed after each run.
|
16
|
+
# --seed 1234
|
17
|
+
config.order = 'random'
|
18
|
+
end
|
19
|
+
|
20
|
+
# Load examples
|
21
|
+
Dir["./spec/examples/**/*.rb"].sort.each {|f| require f }
|
22
|
+
|
23
|
+
# Load translations
|
24
|
+
I18n.load_path = ['./spec/locale/en.yml', './spec/locale/af.yml']
|
metadata
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ez_enum
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Chris Stefano
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-04-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Provides a simple abstraction for defining enumerations in a Ruby module.
|
84
|
+
Supports listing members and localization of constant names.
|
85
|
+
email:
|
86
|
+
- virtualstaticvoid@gmail.com
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- .gitignore
|
92
|
+
- .rspec
|
93
|
+
- .ruby-version
|
94
|
+
- Gemfile
|
95
|
+
- LICENSE.txt
|
96
|
+
- README.md
|
97
|
+
- Rakefile
|
98
|
+
- ez_enum.gemspec
|
99
|
+
- lib/ez_enum.rb
|
100
|
+
- lib/ez_enum/version.rb
|
101
|
+
- spec/examples/status_enum.rb
|
102
|
+
- spec/ez_enum/ez_enum_spec.rb
|
103
|
+
- spec/locale/af.yml
|
104
|
+
- spec/locale/en.yml
|
105
|
+
- spec/spec_helper.rb
|
106
|
+
homepage: https://github.com/virtualstaticvoid/ez_enum
|
107
|
+
licenses:
|
108
|
+
- MIT
|
109
|
+
metadata: {}
|
110
|
+
post_install_message:
|
111
|
+
rdoc_options: []
|
112
|
+
require_paths:
|
113
|
+
- lib
|
114
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - '>='
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - '>='
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
requirements: []
|
125
|
+
rubyforge_project:
|
126
|
+
rubygems_version: 2.0.0
|
127
|
+
signing_key:
|
128
|
+
specification_version: 4
|
129
|
+
summary: Elegant Enumerations for Ruby and Rails
|
130
|
+
test_files:
|
131
|
+
- spec/examples/status_enum.rb
|
132
|
+
- spec/ez_enum/ez_enum_spec.rb
|
133
|
+
- spec/locale/af.yml
|
134
|
+
- spec/locale/en.yml
|
135
|
+
- spec/spec_helper.rb
|