enumerations 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/Rakefile +12 -0
- data/Readme.md +73 -0
- data/enumerations.gemspec +16 -0
- data/lib/enumerations.rb +27 -28
- data/test/enumerations_test.rb +61 -0
- data/test/test_helper.rb +24 -0
- metadata +21 -7
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
.DS_Store
|
data/Rakefile
ADDED
data/Readme.md
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
Enumerations
|
2
|
+
==========
|
3
|
+
|
4
|
+
Rails plugin for enumerations in ActiveRecord models.
|
5
|
+
|
6
|
+
Install
|
7
|
+
=======
|
8
|
+
|
9
|
+
If you are using Bundler (and you should be), just add it as a gem.
|
10
|
+
|
11
|
+
gem 'enumerations'
|
12
|
+
|
13
|
+
If not, then install it as a plugin.
|
14
|
+
|
15
|
+
rails plugin install git://github.com/infinum/enumerations.git
|
16
|
+
|
17
|
+
Usage
|
18
|
+
=====
|
19
|
+
|
20
|
+
Create a model for your enumerations
|
21
|
+
|
22
|
+
class Status < Enumeration::Base
|
23
|
+
values :draft => {:id => 1, :name => 'Draft'},
|
24
|
+
:review_pending => {:id => 2, :name => 'Review pending'},
|
25
|
+
:published => {:id => 3, :name => 'Published'}
|
26
|
+
end
|
27
|
+
|
28
|
+
Include enumerations for integer fields in other models
|
29
|
+
|
30
|
+
class Post < ActiveRecord::Base
|
31
|
+
enumeration :status
|
32
|
+
validates_presence_of :body, :title, :status_id
|
33
|
+
end
|
34
|
+
|
35
|
+
Set enumerations, find enumerations by symbol
|
36
|
+
|
37
|
+
@post = Post.first
|
38
|
+
@post.status = Status.find(:draft)
|
39
|
+
@post.save
|
40
|
+
|
41
|
+
Find enumerations by id
|
42
|
+
|
43
|
+
@post.status = Status.find(2) # => Review pending
|
44
|
+
@post.save
|
45
|
+
|
46
|
+
Compare enumerations
|
47
|
+
|
48
|
+
@post.status == :published # => true
|
49
|
+
@post.status == 3 # => true
|
50
|
+
@post.status == Status.find(:published) # => true
|
51
|
+
@post.status.published? # => true
|
52
|
+
|
53
|
+
Get all enumerations
|
54
|
+
|
55
|
+
Status.all
|
56
|
+
|
57
|
+
Use in forms
|
58
|
+
|
59
|
+
%p
|
60
|
+
= f.label :status_id
|
61
|
+
%br
|
62
|
+
= f.collection_select :status_id, Status.all, :id, :name
|
63
|
+
|
64
|
+
TODO
|
65
|
+
====
|
66
|
+
|
67
|
+
* support for object values (not just symbols and strings)
|
68
|
+
|
69
|
+
Author
|
70
|
+
======
|
71
|
+
|
72
|
+
Copyright (c) 2010 Tomislav Car, Infinum
|
73
|
+
|
@@ -0,0 +1,16 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'enumerations'
|
3
|
+
s.version = '1.1.0'
|
4
|
+
s.date = '2010-08-20'
|
5
|
+
s.summary = "Enumerations for ActiveRecord!"
|
6
|
+
s.description = "Extends ActiveRecord with enumeration capabilites."
|
7
|
+
s.authors = ["Tomislav Car", "Nikica Jokic", "Nikola Santic"]
|
8
|
+
s.email = ["tomislav@infinum.hr", "nikica.jokic@infinum.hr", "nikola.santic@infinum.hr"]
|
9
|
+
s.homepage = 'https://github.com/infinum/enumerations'
|
10
|
+
|
11
|
+
s.add_dependency 'activerecord'
|
12
|
+
s.add_dependency 'activesupport'
|
13
|
+
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
+
end
|
data/lib/enumerations.rb
CHANGED
@@ -1,30 +1,9 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
#
|
5
|
-
# enumerate_values :pekinezer => 'Jako lijepi pekinezer'
|
6
|
-
# end
|
7
|
-
#
|
8
|
-
# DogType.all => [DogType, DogType, DogType]
|
9
|
-
# DogType.find(id) => DogType
|
10
|
-
#
|
11
|
-
# if something == DogType.pekinezer # => <#DogType>
|
12
|
-
# if something == DogType::Pekinezer # => <#DogType> # => TODO
|
13
|
-
# if something == DogType[:pekinezer] # => <#DogType> # => TODO
|
14
|
-
#
|
15
|
-
# So we have
|
16
|
-
# * id (numeric). used in database
|
17
|
-
# * lookup method (for comparison), symbol basically, used in source code
|
18
|
-
# * name. used in user interface
|
19
|
-
#
|
20
|
-
# Status.draft?
|
21
|
-
# Status.review_pending?
|
22
|
-
#
|
23
|
-
# TODO
|
24
|
-
# - http://github.com/binarylogic/enumlogic/blob/master/lib/enumlogic.rb
|
25
|
-
# - http://github.com/eeng/ar-enums
|
26
|
-
# - think about storing strings, not integers
|
1
|
+
require 'active_support/core_ext/class/attribute.rb'
|
2
|
+
require 'active_support/core_ext/string/inflections.rb'
|
3
|
+
|
27
4
|
module Enumeration
|
5
|
+
VERSION = "1.1"
|
6
|
+
|
28
7
|
def self.included(receiver)
|
29
8
|
receiver.extend ClassMethods
|
30
9
|
end
|
@@ -44,9 +23,29 @@ module Enumeration
|
|
44
23
|
define_method "#{name}=" do |other|
|
45
24
|
send("#{options[:foreign_key]}=", other.id)
|
46
25
|
end
|
26
|
+
|
27
|
+
# store a list of used enumerations
|
28
|
+
@_all_enumerations ||= []
|
29
|
+
@_all_enumerations << EnumerationReflection.new(name, options)
|
30
|
+
end
|
31
|
+
|
32
|
+
# output all the enumerations that this model has defined
|
33
|
+
def reflect_on_all_enumerations
|
34
|
+
@_all_enumerations
|
47
35
|
end
|
48
36
|
end
|
49
37
|
|
38
|
+
class EnumerationReflection < Struct.new(:name, :options)
|
39
|
+
def class_name
|
40
|
+
options[:class_name]
|
41
|
+
end
|
42
|
+
|
43
|
+
def foreign_key
|
44
|
+
options[:foreign_key]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# used as a base class for enumeration classes
|
50
49
|
class Base < Struct.new(:id, :name, :symbol)
|
51
50
|
class_attribute :all, :id_index, :symbol_index
|
52
51
|
|
@@ -120,7 +119,7 @@ module Enumeration
|
|
120
119
|
|
121
120
|
def method_missing(method_name, *args)
|
122
121
|
symbol = method_name.to_s.gsub(/[?]$/, '')
|
123
|
-
if self.class.find(symbol) && method_name
|
122
|
+
if self.class.find(symbol) && method_name =~ /[?]$/
|
124
123
|
self.symbol == symbol.to_sym
|
125
124
|
else
|
126
125
|
super(method_name, args)
|
@@ -130,4 +129,4 @@ module Enumeration
|
|
130
129
|
end
|
131
130
|
|
132
131
|
# Extend ActiveRecord with Enumeration capabilites
|
133
|
-
ActiveRecord::Base.send(:include, Enumeration)
|
132
|
+
ActiveRecord::Base.send(:include, Enumeration) if defined? ActiveRecord
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/test_helper')
|
2
|
+
|
3
|
+
class EnumerationsTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_lookup_by_symbol
|
6
|
+
status = Status.find(:draft)
|
7
|
+
|
8
|
+
assert_equal :draft, status.symbol
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_lookup_fail_by_symbol
|
12
|
+
status = Status.find(:draft)
|
13
|
+
|
14
|
+
assert_not_equal :published, status.symbol
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_all
|
18
|
+
statuses = Status.all
|
19
|
+
|
20
|
+
assert_equal 3, statuses.size
|
21
|
+
assert_equal statuses.first, Status.draft
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_reflections
|
25
|
+
enumerations = Post.reflect_on_all_enumerations
|
26
|
+
|
27
|
+
assert_equal 2, enumerations.size
|
28
|
+
assert_equal :status, enumerations.first.name
|
29
|
+
assert_equal 'Status', enumerations.first.class_name
|
30
|
+
|
31
|
+
assert_equal :some_other_status_id, enumerations[1].foreign_key
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_model_enumeration_assignment
|
35
|
+
p = Post.new
|
36
|
+
p.status = Status.draft
|
37
|
+
|
38
|
+
assert_equal "Draft", p.status.to_s
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_model_via_id_assignment
|
42
|
+
p = Post.new
|
43
|
+
p.some_other_status_id = Status.published.id
|
44
|
+
|
45
|
+
assert_equal "Published", p.different_status.to_s
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_boolean_lookup
|
49
|
+
p = Post.new
|
50
|
+
p.status = Status.draft
|
51
|
+
|
52
|
+
assert_equal true, p.status.draft?
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_false_boolean_lookup
|
56
|
+
p = Post.new
|
57
|
+
p.status = Status.draft
|
58
|
+
|
59
|
+
assert_equal false, p.status.published?
|
60
|
+
end
|
61
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
2
|
+
|
3
|
+
#require 'rubygems' # not sure if i need this
|
4
|
+
|
5
|
+
require 'test/unit'
|
6
|
+
require 'enumerations'
|
7
|
+
|
8
|
+
# faking activerecord
|
9
|
+
class MockActiveRecordBase
|
10
|
+
include Enumeration
|
11
|
+
end
|
12
|
+
|
13
|
+
class Status < Enumeration::Base
|
14
|
+
values :draft => {:id => 1, :name => 'Draft'},
|
15
|
+
:review_pending => {:id => 2, :name => 'Review pending'},
|
16
|
+
:published => {:id => 3, :name => 'Published'}
|
17
|
+
end
|
18
|
+
|
19
|
+
class Post < MockActiveRecordBase
|
20
|
+
attr_accessor :status_id, :some_other_status_id
|
21
|
+
|
22
|
+
enumeration :status
|
23
|
+
enumeration :different_status, :foreign_key => :some_other_status_id, :class_name => 'Status'
|
24
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enumerations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -11,11 +11,11 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2010-
|
14
|
+
date: 2010-08-20 00:00:00.000000000Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: activerecord
|
18
|
-
requirement: !ruby/object:Gem::Requirement
|
18
|
+
requirement: &70144752765260 !ruby/object:Gem::Requirement
|
19
19
|
none: false
|
20
20
|
requirements:
|
21
21
|
- - ! '>='
|
@@ -23,12 +23,18 @@ dependencies:
|
|
23
23
|
version: '0'
|
24
24
|
type: :runtime
|
25
25
|
prerelease: false
|
26
|
-
version_requirements:
|
26
|
+
version_requirements: *70144752765260
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: &70144752764520 !ruby/object:Gem::Requirement
|
27
30
|
none: false
|
28
31
|
requirements:
|
29
32
|
- - ! '>='
|
30
33
|
- !ruby/object:Gem::Version
|
31
34
|
version: '0'
|
35
|
+
type: :runtime
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: *70144752764520
|
32
38
|
description: Extends ActiveRecord with enumeration capabilites.
|
33
39
|
email:
|
34
40
|
- tomislav@infinum.hr
|
@@ -38,7 +44,13 @@ executables: []
|
|
38
44
|
extensions: []
|
39
45
|
extra_rdoc_files: []
|
40
46
|
files:
|
47
|
+
- .gitignore
|
48
|
+
- Rakefile
|
49
|
+
- Readme.md
|
50
|
+
- enumerations.gemspec
|
41
51
|
- lib/enumerations.rb
|
52
|
+
- test/enumerations_test.rb
|
53
|
+
- test/test_helper.rb
|
42
54
|
homepage: https://github.com/infinum/enumerations
|
43
55
|
licenses: []
|
44
56
|
post_install_message:
|
@@ -59,8 +71,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
59
71
|
version: '0'
|
60
72
|
requirements: []
|
61
73
|
rubyforge_project:
|
62
|
-
rubygems_version: 1.8.
|
74
|
+
rubygems_version: 1.8.10
|
63
75
|
signing_key:
|
64
76
|
specification_version: 3
|
65
|
-
summary: Enumerations for
|
66
|
-
test_files:
|
77
|
+
summary: Enumerations for ActiveRecord!
|
78
|
+
test_files:
|
79
|
+
- test/enumerations_test.rb
|
80
|
+
- test/test_helper.rb
|