mschuerig-easy_enums 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +4 -0
- data/Manifest.txt +8 -0
- data/README.rdoc +64 -0
- data/Rakefile +23 -0
- data/lib/easy_enums.rb +96 -0
- data/test/test_easy_enums.rb +89 -0
- data/test/test_helper.rb +3 -0
- metadata +84 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
= easy_enums
|
2
|
+
|
3
|
+
* http://github.com/mschuerig/easy_enums
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
Simplistic enumerations for ActiveRecord.
|
8
|
+
|
9
|
+
== FEATURES/PROBLEMS:
|
10
|
+
|
11
|
+
* Easy access to cached enumerations.
|
12
|
+
* Simple/Simplistic.
|
13
|
+
* Reloading does not work across server processes.
|
14
|
+
Therefore, only use it for data that is static.
|
15
|
+
|
16
|
+
== SYNOPSIS:
|
17
|
+
|
18
|
+
class Color < ActiveRecord::Base
|
19
|
+
enumerates do |e|
|
20
|
+
e.create :name => 'red'
|
21
|
+
e.create :name => 'green'
|
22
|
+
e.create :name => 'blue'
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
* Color[:green]
|
27
|
+
* Color.find_by_name('red')
|
28
|
+
* Color.find_by_name!(:red)
|
29
|
+
* Color.all
|
30
|
+
* Color.count
|
31
|
+
* Color.reload
|
32
|
+
|
33
|
+
== REQUIREMENTS:
|
34
|
+
|
35
|
+
* ActiveRecord
|
36
|
+
|
37
|
+
== INSTALL:
|
38
|
+
|
39
|
+
* sudo gem install mschuerig-easy_enums
|
40
|
+
|
41
|
+
== LICENSE:
|
42
|
+
|
43
|
+
(The MIT License)
|
44
|
+
|
45
|
+
Copyright (c) 2009 Michael Schuerig
|
46
|
+
|
47
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
48
|
+
a copy of this software and associated documentation files (the
|
49
|
+
'Software'), to deal in the Software without restriction, including
|
50
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
51
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
52
|
+
permit persons to whom the Software is furnished to do so, subject to
|
53
|
+
the following conditions:
|
54
|
+
|
55
|
+
The above copyright notice and this permission notice shall be
|
56
|
+
included in all copies or substantial portions of the Software.
|
57
|
+
|
58
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
59
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
60
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
61
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
62
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
63
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
64
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
%w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
|
2
|
+
require File.dirname(__FILE__) + '/lib/easy_enums'
|
3
|
+
|
4
|
+
# Generate all the Rake tasks
|
5
|
+
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
6
|
+
$hoe = Hoe.new('easy_enums', EasyEnumerations::VERSION) do |p|
|
7
|
+
p.developer('Michael Schuerig', 'michael@schuerig.de')
|
8
|
+
p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
|
9
|
+
# p.extra_deps = [
|
10
|
+
# ['activesupport','>= 2.0.2'],
|
11
|
+
# ]
|
12
|
+
p.extra_dev_deps = [
|
13
|
+
['newgem', ">= #{::Newgem::VERSION}"]
|
14
|
+
]
|
15
|
+
|
16
|
+
p.clean_globs |= %w[**/.DS_Store tmp *.log]
|
17
|
+
path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
|
18
|
+
p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
|
19
|
+
p.rsync_args = '-av --delete --ignore-errors'
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'newgem/tasks' # load /tasks/*.rake
|
23
|
+
Dir['tasks/**/*.rake'].each { |t| load t }
|
data/lib/easy_enums.rb
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
|
2
|
+
module EasyEnumerations
|
3
|
+
VERSION = '0.0.1'
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
base.extend(ClassMethods)
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def enumerates
|
11
|
+
extend SingletonMethods
|
12
|
+
validates_presence_of :name
|
13
|
+
creator = EnumerationCreator.new(self)
|
14
|
+
yield creator
|
15
|
+
@enum_cache = creator.cache
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module SingletonMethods
|
20
|
+
def all
|
21
|
+
@enum_cache.values
|
22
|
+
end
|
23
|
+
|
24
|
+
def count(*args)
|
25
|
+
if args.empty?
|
26
|
+
@enum_cache.size
|
27
|
+
else
|
28
|
+
super
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def reload
|
33
|
+
@enum_cache.reload
|
34
|
+
end
|
35
|
+
|
36
|
+
def find_by_name(name)
|
37
|
+
@enum_cache[name]
|
38
|
+
end
|
39
|
+
alias :[] :find_by_name
|
40
|
+
|
41
|
+
def find_by_name!(name)
|
42
|
+
find_by_name(name) or
|
43
|
+
raise ::ActiveRecord::RecordNotFound, "Couldn't find #{self.name} with name = #{name}"
|
44
|
+
end
|
45
|
+
|
46
|
+
def each_name
|
47
|
+
all.each do |role_type|
|
48
|
+
yield role_type.name
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
class EnumerationCreator
|
54
|
+
def initialize(model)
|
55
|
+
@model = model
|
56
|
+
end
|
57
|
+
|
58
|
+
def create(options)
|
59
|
+
unless @model.exists?(:name => options[:name])
|
60
|
+
@model.create!(options)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def cache
|
65
|
+
Cache.new(@model)
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
class Cache
|
71
|
+
def initialize(model)
|
72
|
+
@model = model
|
73
|
+
reload
|
74
|
+
end
|
75
|
+
|
76
|
+
def reload
|
77
|
+
@cache = @model.find(:all).inject({}) do |c, e|
|
78
|
+
c[e.name] = e
|
79
|
+
c
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def values
|
84
|
+
@values ||= @cache.values
|
85
|
+
end
|
86
|
+
|
87
|
+
def [](name)
|
88
|
+
@cache[name.to_s]
|
89
|
+
end
|
90
|
+
|
91
|
+
def size
|
92
|
+
values.size
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
class RecordNotFound < StandardError; end
|
5
|
+
end
|
6
|
+
|
7
|
+
class TestEasyEnums < Test::Unit::TestCase
|
8
|
+
|
9
|
+
module ActiveRecordMock
|
10
|
+
def self.included(base)
|
11
|
+
base.extend(ClassMethods)
|
12
|
+
end
|
13
|
+
|
14
|
+
module ClassMethods
|
15
|
+
def validates_presence_of(*args); end
|
16
|
+
def exists?(args); false; end
|
17
|
+
def find(*args); @values; end
|
18
|
+
|
19
|
+
def create!(options)
|
20
|
+
@values ||= []
|
21
|
+
@values << new(options[:name])
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
attr_reader :name
|
26
|
+
def initialize(name)
|
27
|
+
@name = name
|
28
|
+
end
|
29
|
+
|
30
|
+
def ==(other)
|
31
|
+
name == other.name
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class Enumbed
|
36
|
+
include ActiveRecordMock
|
37
|
+
include EasyEnumerations
|
38
|
+
|
39
|
+
enumerates do |e|
|
40
|
+
e.create :name => 'red'
|
41
|
+
e.create :name => 'green'
|
42
|
+
e.create :name => 'blue'
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
class Season
|
47
|
+
include ActiveRecordMock
|
48
|
+
include EasyEnumerations
|
49
|
+
|
50
|
+
enumerates do |e|
|
51
|
+
e.create :name => 'Spring'
|
52
|
+
e.create :name => 'Summer'
|
53
|
+
e.create :name => 'Fall'
|
54
|
+
e.create :name => 'Winter'
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_array_access
|
59
|
+
assert_equal Enumbed.new('red'), Enumbed[:red]
|
60
|
+
assert_equal Enumbed.new('red'), Enumbed['red']
|
61
|
+
assert_nil Enumbed[:magenta]
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_find_by_name
|
65
|
+
assert_equal Enumbed.new('red'), Enumbed.find_by_name(:red)
|
66
|
+
assert_equal Enumbed.new('red'), Enumbed.find_by_name('red')
|
67
|
+
assert_nil Enumbed.find_by_name(:magenta)
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_find_by_name!
|
71
|
+
assert_raise(ActiveRecord::RecordNotFound) do
|
72
|
+
Enumbed.find_by_name!(:magenta)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_count
|
77
|
+
assert_equal 3, Enumbed.count
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_all
|
81
|
+
assert_equal ['blue', 'green', 'red'],
|
82
|
+
Enumbed.all.map { |e| e.name }.sort
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_two_enums_do_not_interfere
|
86
|
+
assert_equal ["Fall", "Spring", "Summer", "Winter"],
|
87
|
+
Season.all.map { |e| e.name }.sort
|
88
|
+
end
|
89
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mschuerig-easy_enums
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michael Schuerig
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-04-02 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: newgem
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.3.0
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: hoe
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.8.0
|
34
|
+
version:
|
35
|
+
description: Simplistic enumerations for ActiveRecord.
|
36
|
+
email:
|
37
|
+
- michael@schuerig.de
|
38
|
+
executables: []
|
39
|
+
|
40
|
+
extensions: []
|
41
|
+
|
42
|
+
extra_rdoc_files:
|
43
|
+
- History.txt
|
44
|
+
- Manifest.txt
|
45
|
+
- README.rdoc
|
46
|
+
files:
|
47
|
+
- History.txt
|
48
|
+
- Manifest.txt
|
49
|
+
- README.rdoc
|
50
|
+
- Rakefile
|
51
|
+
- init.rb
|
52
|
+
- lib/easy_enums.rb
|
53
|
+
- test/test_easy_enums.rb
|
54
|
+
- test/test_helper.rb
|
55
|
+
has_rdoc: true
|
56
|
+
homepage: http://github.com/mschuerig/easy_enums
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options:
|
59
|
+
- --main
|
60
|
+
- README.rdoc
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: "0"
|
68
|
+
version:
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: "0"
|
74
|
+
version:
|
75
|
+
requirements: []
|
76
|
+
|
77
|
+
rubyforge_project: easy_enums
|
78
|
+
rubygems_version: 1.2.0
|
79
|
+
signing_key:
|
80
|
+
specification_version: 2
|
81
|
+
summary: Simplistic enumerations for ActiveRecord.
|
82
|
+
test_files:
|
83
|
+
- test/test_helper.rb
|
84
|
+
- test/test_easy_enums.rb
|