enumerative 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.rvmrc +4 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +66 -0
- data/Rakefile +2 -0
- data/enumerative.gemspec +17 -0
- data/lib/enumerative.rb +8 -0
- data/lib/enumerative/enumeration.rb +66 -0
- data/lib/enumerative/enumeration_sharedspec.rb +57 -0
- data/lib/enumerative/has_enumeration.rb +37 -0
- data/lib/enumerative/version.rb +5 -0
- data/spec/support/enumeration_sharedspec.rb +57 -0
- metadata +60 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Ninja Loss
|
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,66 @@
|
|
1
|
+
# Enumerative
|
2
|
+
|
3
|
+
Provides enumeration functionality.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'enumerative'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install enumerative
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Create an enumeration class and include the modiule.
|
22
|
+
|
23
|
+
class Color
|
24
|
+
|
25
|
+
def self.valid_keys
|
26
|
+
%w(
|
27
|
+
black
|
28
|
+
blue
|
29
|
+
green
|
30
|
+
)
|
31
|
+
end
|
32
|
+
|
33
|
+
include Enumerative::Enumeration # this must come after ::valid_keys
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
Declare the enumeration in an ActiveModel.
|
38
|
+
|
39
|
+
class Vehicle
|
40
|
+
|
41
|
+
include Enumerative::HasEnumeration
|
42
|
+
|
43
|
+
# add_column :vehicles, :color, :string, :limit => 35
|
44
|
+
|
45
|
+
has_enumeration :color, :from => Color
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
Translate the enumeration using localization:
|
50
|
+
|
51
|
+
en:
|
52
|
+
enumerations:
|
53
|
+
color:
|
54
|
+
black: Black
|
55
|
+
blue: Blue
|
56
|
+
green: Green
|
57
|
+
|
58
|
+
Use the enumeration:
|
59
|
+
|
60
|
+
vehicle = Vehicle.create( :color => 'black' )
|
61
|
+
|
62
|
+
vehicle.attributes['color'] # => black
|
63
|
+
vehicle.color # => #<Color:0x10702d5e8 @key="black">
|
64
|
+
vehicle.color.key # => black
|
65
|
+
vehicle.color.value # => Black
|
66
|
+
vehicle.color.to_s # => black
|
data/Rakefile
ADDED
data/enumerative.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/enumerative/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Nils Jonsson", "C. Jason Harrelson"]
|
6
|
+
gem.email = ["ninja.loss@gmail.com"]
|
7
|
+
gem.description = %q{Tools for defining and using enumerations.}
|
8
|
+
gem.summary = %q{Tools for enumerations.}
|
9
|
+
gem.homepage = "https://github.com/ninja-loss/enumerative"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "enumerative"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Enumerative::VERSION
|
17
|
+
end
|
data/lib/enumerative.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
module Enumerative
|
2
|
+
|
3
|
+
module Enumeration
|
4
|
+
|
5
|
+
def self.included( other_module )
|
6
|
+
other_module.module_eval do
|
7
|
+
valid_keys.each do |k|
|
8
|
+
const_set k.gsub( /\s/, '_' ).upcase, other_module.new( k )
|
9
|
+
end
|
10
|
+
|
11
|
+
extend ClassMethods
|
12
|
+
|
13
|
+
attr_reader :key
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
module ClassMethods
|
18
|
+
|
19
|
+
def to_select
|
20
|
+
valid_keys.sort.collect do |k|
|
21
|
+
[translate( k ), k]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def translate( key )
|
26
|
+
I18n.translate "enumerations.#{name.underscore}.#{key}", :raise => true
|
27
|
+
rescue I18n::MissingTranslationData
|
28
|
+
nil
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
def initialize( key )
|
34
|
+
@key = key.to_s.try( :dup ).try( :freeze )
|
35
|
+
end
|
36
|
+
|
37
|
+
def ==( other )
|
38
|
+
other.is_a?( self.class ) && (other.key == key)
|
39
|
+
end
|
40
|
+
|
41
|
+
def to_s
|
42
|
+
key
|
43
|
+
end
|
44
|
+
|
45
|
+
def valid?
|
46
|
+
key.blank? || self.class.valid_keys.include?( key )
|
47
|
+
end
|
48
|
+
|
49
|
+
def value
|
50
|
+
key.blank? ? nil : self.class.translate( key )
|
51
|
+
end
|
52
|
+
|
53
|
+
def blank?
|
54
|
+
key.blank?
|
55
|
+
end
|
56
|
+
|
57
|
+
# Method is required for Rails > 3.2.x due to ActiveRecord's autosave
|
58
|
+
# functionality.
|
59
|
+
#
|
60
|
+
def marked_for_destruction?
|
61
|
+
false
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
shared_examples_for 'an Enumeration' do
|
4
|
+
|
5
|
+
describe 'an instance' do
|
6
|
+
|
7
|
+
let :instance do
|
8
|
+
described_class.new 'foo'
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should have the expected #key' do
|
12
|
+
instance.key.should == 'foo'
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should have an immutable #key' do
|
16
|
+
instance.should_not respond_to( :key= )
|
17
|
+
|
18
|
+
expect {
|
19
|
+
instance.key.gsub!( /./, 'x' )
|
20
|
+
}.to raise_error( RuntimeError, "can't modify frozen string")
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should equal an equivalent instance' do
|
24
|
+
instance.should == described_class.new( 'foo' )
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should have the expected string representation' do
|
28
|
+
instance.to_s.should == 'foo'
|
29
|
+
end
|
30
|
+
|
31
|
+
describe 'that is valid' do
|
32
|
+
|
33
|
+
subject { described_class.new described_class.valid_keys.first }
|
34
|
+
|
35
|
+
it { should be_valid }
|
36
|
+
|
37
|
+
it 'should have a #value' do
|
38
|
+
subject.value.should_not == nil
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
describe 'that is invalid' do
|
44
|
+
|
45
|
+
subject { described_class.new "not a valid #{described_class.name} key" }
|
46
|
+
|
47
|
+
it { should_not be_valid }
|
48
|
+
|
49
|
+
it 'should not have a #value' do
|
50
|
+
subject.value.should == nil
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Enumerative
|
2
|
+
|
3
|
+
module HasEnumeration
|
4
|
+
|
5
|
+
def self.included( other_module )
|
6
|
+
other_module.extend ClassMethods
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
|
11
|
+
def has_enumeration( *attributes )
|
12
|
+
options = attributes.extract_options!
|
13
|
+
class_name = if options[:from].try( :is_a?, Class )
|
14
|
+
options[:from].name
|
15
|
+
else
|
16
|
+
options[:from]
|
17
|
+
end
|
18
|
+
if class_name && !class_name.constantize.include?( Enumeration )
|
19
|
+
raise ArgumentError,
|
20
|
+
':from option should refer to a class that mixes in Enumeration'
|
21
|
+
end
|
22
|
+
|
23
|
+
attributes.each do |a|
|
24
|
+
composed_of a, :allow_nil => true,
|
25
|
+
:class_name => class_name || a.to_s.classify,
|
26
|
+
:mapping => [a, 'key'],
|
27
|
+
:converter => :new
|
28
|
+
|
29
|
+
validates_associated a, :allow_blank => true
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
shared_examples_for 'an Enumeration' do
|
4
|
+
|
5
|
+
describe 'an instance' do
|
6
|
+
|
7
|
+
let :instance do
|
8
|
+
described_class.new 'foo'
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should have the expected #key' do
|
12
|
+
instance.key.should == 'foo'
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should have an immutable #key' do
|
16
|
+
instance.should_not respond_to( :key= )
|
17
|
+
|
18
|
+
expect {
|
19
|
+
instance.key.gsub!( /./, 'x' )
|
20
|
+
}.to raise_error( RuntimeError, "can't modify frozen string")
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should equal an equivalent instance' do
|
24
|
+
instance.should == described_class.new( 'foo' )
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should have the expected string representation' do
|
28
|
+
instance.to_s.should == 'foo'
|
29
|
+
end
|
30
|
+
|
31
|
+
describe 'that is valid' do
|
32
|
+
|
33
|
+
subject { described_class.new described_class.valid_keys.first }
|
34
|
+
|
35
|
+
it { should be_valid }
|
36
|
+
|
37
|
+
it 'should have a #value' do
|
38
|
+
subject.value.should_not == nil
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
describe 'that is invalid' do
|
44
|
+
|
45
|
+
subject { described_class.new "not a valid #{described_class.name} key" }
|
46
|
+
|
47
|
+
it { should_not be_valid }
|
48
|
+
|
49
|
+
it 'should not have a #value' do
|
50
|
+
subject.value.should == nil
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: enumerative
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Nils Jonsson
|
9
|
+
- C. Jason Harrelson
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2012-09-14 00:00:00.000000000 Z
|
14
|
+
dependencies: []
|
15
|
+
description: Tools for defining and using enumerations.
|
16
|
+
email:
|
17
|
+
- ninja.loss@gmail.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- .rvmrc
|
24
|
+
- Gemfile
|
25
|
+
- LICENSE
|
26
|
+
- README.md
|
27
|
+
- Rakefile
|
28
|
+
- enumerative.gemspec
|
29
|
+
- lib/enumerative.rb
|
30
|
+
- lib/enumerative/enumeration.rb
|
31
|
+
- lib/enumerative/enumeration_sharedspec.rb
|
32
|
+
- lib/enumerative/has_enumeration.rb
|
33
|
+
- lib/enumerative/version.rb
|
34
|
+
- spec/support/enumeration_sharedspec.rb
|
35
|
+
homepage: https://github.com/ninja-loss/enumerative
|
36
|
+
licenses: []
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ! '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
requirements: []
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 1.8.24
|
56
|
+
signing_key:
|
57
|
+
specification_version: 3
|
58
|
+
summary: Tools for enumerations.
|
59
|
+
test_files:
|
60
|
+
- spec/support/enumeration_sharedspec.rb
|