act_as_enumerable 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.coveralls.yml +1 -0
- data/.gitignore +18 -0
- data/.travis.yml +4 -0
- data/Gemfile +11 -0
- data/Gemfile.lock +57 -0
- data/LICENSE.md +7 -0
- data/README.md +80 -0
- data/Rakefile +17 -0
- data/act_as_enumerable.gemspec +20 -0
- data/init.rb +1 -0
- data/lib/act_as_enumerable/version.rb +3 -0
- data/lib/act_as_enumerable.rb +54 -0
- data/test/act_as_enumerable_test.rb +79 -0
- metadata +122 -0
data/.coveralls.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
repo_token: ZzfF046txNMxLkyoa2fpjrANjsILZNGuJ
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
act_as_enumerable (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
activesupport (3.2.11)
|
10
|
+
i18n (~> 0.6)
|
11
|
+
multi_json (~> 1.0)
|
12
|
+
ansi (1.4.3)
|
13
|
+
bourne (1.1.2)
|
14
|
+
mocha (= 0.10.5)
|
15
|
+
colorize (0.5.8)
|
16
|
+
coveralls (0.5.5)
|
17
|
+
colorize
|
18
|
+
json
|
19
|
+
rest-client
|
20
|
+
simplecov (>= 0.7)
|
21
|
+
thor
|
22
|
+
i18n (0.6.1)
|
23
|
+
json (1.7.6)
|
24
|
+
metaclass (0.0.1)
|
25
|
+
mime-types (1.19)
|
26
|
+
minitest (4.4.0)
|
27
|
+
mocha (0.10.5)
|
28
|
+
metaclass (~> 0.0.1)
|
29
|
+
multi_json (1.5.0)
|
30
|
+
rake (10.0.3)
|
31
|
+
rest-client (1.6.7)
|
32
|
+
mime-types (>= 1.16)
|
33
|
+
shoulda (3.3.2)
|
34
|
+
shoulda-context (~> 1.0.1)
|
35
|
+
shoulda-matchers (~> 1.4.1)
|
36
|
+
shoulda-context (1.0.2)
|
37
|
+
shoulda-matchers (1.4.2)
|
38
|
+
activesupport (>= 3.0.0)
|
39
|
+
bourne (~> 1.1.2)
|
40
|
+
simplecov (0.7.1)
|
41
|
+
multi_json (~> 1.0)
|
42
|
+
simplecov-html (~> 0.7.1)
|
43
|
+
simplecov-html (0.7.1)
|
44
|
+
thor (0.16.0)
|
45
|
+
turn (0.9.6)
|
46
|
+
ansi
|
47
|
+
|
48
|
+
PLATFORMS
|
49
|
+
ruby
|
50
|
+
|
51
|
+
DEPENDENCIES
|
52
|
+
act_as_enumerable!
|
53
|
+
coveralls
|
54
|
+
minitest (~> 4.4.0)
|
55
|
+
rake (~> 10.0.3)
|
56
|
+
shoulda (~> 3.3.2)
|
57
|
+
turn (~> 0.9.3)
|
data/LICENSE.md
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright 2012 caedes
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
# ActAsEnumerable
|
2
|
+
|
3
|
+
[![Build Status][travis-img]][travis-url] [![Coveralls][coveralls-img]][coveralls-url] [![Dependency Status][gemnasium-img]][gemnasium-url] [![Code Climate][codeclimate-img]][codeclimate-url] [![Gem Version][gemversion-img]][gemversion-url]
|
4
|
+
|
5
|
+
_Add enumeration to your ruby classes_
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
```shell
|
10
|
+
gem install act_as_enumerable
|
11
|
+
```
|
12
|
+
|
13
|
+
Or in your Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'act_as_enumerable'
|
17
|
+
```
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
class Person
|
23
|
+
attr_accessor :gender
|
24
|
+
|
25
|
+
def initialize opts
|
26
|
+
@gender = opts[:gender]
|
27
|
+
end
|
28
|
+
|
29
|
+
enum :gender, %w(male female)
|
30
|
+
end
|
31
|
+
|
32
|
+
cameron = Person.new gender: :male
|
33
|
+
|
34
|
+
p Person.gender_values
|
35
|
+
#=> ['male', 'female']
|
36
|
+
|
37
|
+
p Person.gender_captions
|
38
|
+
#=> ['Male', 'Female']
|
39
|
+
|
40
|
+
p cameron.gender
|
41
|
+
#=> 'male'
|
42
|
+
|
43
|
+
p cameron.male?
|
44
|
+
#=> true
|
45
|
+
p cameron.female?
|
46
|
+
#=> false
|
47
|
+
|
48
|
+
p cameron.female!
|
49
|
+
#=> 'female'
|
50
|
+
p cameron.male?
|
51
|
+
#=> false
|
52
|
+
p cameron.female?
|
53
|
+
#=> true
|
54
|
+
|
55
|
+
p cameron.gender_caption
|
56
|
+
#=> 'Female'
|
57
|
+
```
|
58
|
+
|
59
|
+
## Contributing
|
60
|
+
|
61
|
+
1. Fork repository
|
62
|
+
2. Create a branch following a [successfull branching model](http://nvie.com/posts/a-successful-git-branching-model/)
|
63
|
+
3. Write your feature/fix
|
64
|
+
4. Write tests
|
65
|
+
5. Pull request
|
66
|
+
|
67
|
+
## Licence
|
68
|
+
|
69
|
+
Released under the MIT License. See the [LICENSE](https://github.com/caedes/act_as_enumerable/blob/master/LICENSE.md) file for further details.
|
70
|
+
|
71
|
+
[travis-img]: https://secure.travis-ci.org/caedes/act_as_enumerable.png?branch=master
|
72
|
+
[travis-url]: http://travis-ci.org/caedes/act_as_enumerable
|
73
|
+
[coveralls-img]: https://coveralls.io/repos/caedes/act_as_enumerable/badge.png?branch=master
|
74
|
+
[coveralls-url]: https://coveralls.io/r/caedes/act_as_enumerable
|
75
|
+
[gemnasium-img]: https://gemnasium.com/caedes/act_as_enumerable.png
|
76
|
+
[gemnasium-url]: https://gemnasium.com/caedes/act_as_enumerable
|
77
|
+
[codeclimate-img]: https://codeclimate.com/badge.png
|
78
|
+
[codeclimate-url]: https://codeclimate.com/github/caedes/act_as_enumerable
|
79
|
+
[gemversion-img]: https://badge.fury.io/rb/act_as_enumerable.png
|
80
|
+
[gemversion-url]: http://badge.fury.io/rb/act_as_enumerable
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
|
8
|
+
Bundler::GemHelper.install_tasks
|
9
|
+
|
10
|
+
require 'rake/testtask'
|
11
|
+
|
12
|
+
Rake::TestTask.new do |t|
|
13
|
+
t.libs << 'test'
|
14
|
+
end
|
15
|
+
|
16
|
+
desc 'Run tests'
|
17
|
+
task default: :test
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require File.expand_path('../lib/act_as_enumerable/version', __FILE__)
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = 'act_as_enumerable'
|
5
|
+
s.version = ActAsEnumerable::VERSION
|
6
|
+
s.authors = ['caedes']
|
7
|
+
s.email = ['laurentromain@gmail.com']
|
8
|
+
s.homepage = 'https://github.com/caedes/act_as_enumerable'
|
9
|
+
s.summary = 'Add enumeration to your ruby classes'
|
10
|
+
s.description = s.summary
|
11
|
+
|
12
|
+
s.files = `git ls-files`.split("\n")
|
13
|
+
|
14
|
+
s.require_path = 'lib'
|
15
|
+
|
16
|
+
s.add_development_dependency 'rake'
|
17
|
+
s.add_development_dependency 'minitest'
|
18
|
+
s.add_development_dependency 'turn', '~> 0.9.3'
|
19
|
+
s.add_development_dependency 'shoulda', '~> 3.1.1'
|
20
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'act_as_enumerable'
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'act_as_enumerable/version'
|
2
|
+
|
3
|
+
unless String.methods.include? 'underscore'
|
4
|
+
class String
|
5
|
+
def underscore
|
6
|
+
self.gsub(/::/, '/').
|
7
|
+
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
8
|
+
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
9
|
+
tr("-", "_").
|
10
|
+
downcase
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class Object
|
16
|
+
def self.enum name, enumeration
|
17
|
+
name = name.to_s
|
18
|
+
class_eval "
|
19
|
+
attr_accessor :#{name}
|
20
|
+
def self.#{name}_values
|
21
|
+
#{enumeration}
|
22
|
+
end
|
23
|
+
def self.#{name}_captions
|
24
|
+
#{enumeration}.map do |value|
|
25
|
+
humanize_#{name} value
|
26
|
+
end
|
27
|
+
end"
|
28
|
+
enumeration.each do |value|
|
29
|
+
value = value.to_s
|
30
|
+
eval "
|
31
|
+
def #{value}?
|
32
|
+
self.#{name} == '#{value}'
|
33
|
+
end
|
34
|
+
def #{value}!
|
35
|
+
self.#{name} = '#{value}'
|
36
|
+
end"
|
37
|
+
end
|
38
|
+
eval "
|
39
|
+
def humanize_#{name} value
|
40
|
+
value = value.to_s
|
41
|
+
begin
|
42
|
+
Required::Module::const_get 'I18n'
|
43
|
+
::I18n.t \"enum.#{self.to_s.underscore}.#{name}.#{'#{value}'}\"
|
44
|
+
rescue NameError
|
45
|
+
value
|
46
|
+
end
|
47
|
+
end
|
48
|
+
def #{name}_caption
|
49
|
+
self.#{name}.nil? ? self.#{name} : humanize_#{name}(self.#{name})
|
50
|
+
end"
|
51
|
+
|
52
|
+
# validates_inclusion_of name.to_sym, { allow_nil: true, in: enumeration } if self.ancestors.include? 'ActiveRecord::Base'
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
begin; require 'turn/autorun'; rescue LoadError; end
|
3
|
+
require 'act_as_enumerable'
|
4
|
+
require 'shoulda'
|
5
|
+
|
6
|
+
require 'coveralls'
|
7
|
+
Coveralls.wear!
|
8
|
+
|
9
|
+
class EnumPlusTest < Test::Unit::TestCase
|
10
|
+
|
11
|
+
context 'when defining a new object' do
|
12
|
+
setup do
|
13
|
+
class EnumPlusTest
|
14
|
+
end
|
15
|
+
|
16
|
+
@enum_plus_test = EnumPlusTest.new
|
17
|
+
end
|
18
|
+
|
19
|
+
should 'define :enum' do
|
20
|
+
assert EnumPlusTest.singleton_methods.include? :enum
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'with an object using :enum method (e.g. :gender)' do
|
25
|
+
setup do
|
26
|
+
class EnumPlusTest
|
27
|
+
enum :gender, %w(male female)
|
28
|
+
end
|
29
|
+
|
30
|
+
@enum_plus_test = EnumPlusTest.new
|
31
|
+
end
|
32
|
+
|
33
|
+
should 'define `gender_values` method' do
|
34
|
+
assert EnumPlusTest.singleton_methods.include? :gender_values
|
35
|
+
assert_equal %w(male female), EnumPlusTest.gender_values
|
36
|
+
end
|
37
|
+
|
38
|
+
context 'without I18n' do
|
39
|
+
should 'define `gender_caption` method' do
|
40
|
+
assert EnumPlusTest.singleton_methods.include? :gender_captions
|
41
|
+
assert_equal %w(male female), EnumPlusTest.gender_captions
|
42
|
+
end
|
43
|
+
|
44
|
+
should 'define `.humanize_gender` method' do
|
45
|
+
assert @enum_plus_test.methods.include? :humanize_gender
|
46
|
+
|
47
|
+
%w(male female).each do |value|
|
48
|
+
assert_equal value, @enum_plus_test.humanize_gender(value.to_sym)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'without setting :gender' do
|
53
|
+
should 'define `.gender_caption` method' do
|
54
|
+
assert @enum_plus_test.methods.include? :gender_caption
|
55
|
+
assert_equal nil, @enum_plus_test.gender_caption
|
56
|
+
end
|
57
|
+
%w(male female).each do |value|
|
58
|
+
should "define `.#{value}?` method" do
|
59
|
+
assert @enum_plus_test.methods.include? "#{value}?".to_sym
|
60
|
+
%w(male female).each do |value2|
|
61
|
+
assert_equal false, @enum_plus_test.send("#{value}?".to_sym)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
context 'with setting :gender' do
|
68
|
+
%w(male female).each do |value|
|
69
|
+
setup { @enum_plus_test.gender = value }
|
70
|
+
|
71
|
+
should "define `.#{value}?` method" do
|
72
|
+
assert @enum_plus_test.methods.include? "#{value}?".to_sym
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: act_as_enumerable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- caedes
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-05-18 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: minitest
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: turn
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 0.9.3
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.9.3
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: shoulda
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 3.1.1
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 3.1.1
|
78
|
+
description: Add enumeration to your ruby classes
|
79
|
+
email:
|
80
|
+
- laurentromain@gmail.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .coveralls.yml
|
86
|
+
- .gitignore
|
87
|
+
- .travis.yml
|
88
|
+
- Gemfile
|
89
|
+
- Gemfile.lock
|
90
|
+
- LICENSE.md
|
91
|
+
- README.md
|
92
|
+
- Rakefile
|
93
|
+
- act_as_enumerable.gemspec
|
94
|
+
- init.rb
|
95
|
+
- lib/act_as_enumerable.rb
|
96
|
+
- lib/act_as_enumerable/version.rb
|
97
|
+
- test/act_as_enumerable_test.rb
|
98
|
+
homepage: https://github.com/caedes/act_as_enumerable
|
99
|
+
licenses: []
|
100
|
+
post_install_message:
|
101
|
+
rdoc_options: []
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
none: false
|
112
|
+
requirements:
|
113
|
+
- - ! '>='
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
requirements: []
|
117
|
+
rubyforge_project:
|
118
|
+
rubygems_version: 1.8.23
|
119
|
+
signing_key:
|
120
|
+
specification_version: 3
|
121
|
+
summary: Add enumeration to your ruby classes
|
122
|
+
test_files: []
|