active_enum 0.7.1 → 0.8.0
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.
- data/CHANGELOG +5 -0
- data/README.rdoc +4 -18
- data/Rakefile +1 -1
- data/active_enum.gemspec +30 -0
- data/lib/active_enum.rb +4 -0
- data/lib/active_enum/base.rb +29 -39
- data/lib/active_enum/storage/abstract_store.rb +29 -0
- data/lib/active_enum/storage/memory_store.rb +39 -0
- data/lib/active_enum/version.rb +1 -1
- data/spec/active_enum/base_spec.rb +109 -67
- data/spec/active_enum/storage/memory_store_spec.rb +113 -0
- data/spec/active_enum_spec.rb +5 -1
- data/spec/spec_helper.rb +7 -1
- metadata +10 -5
data/CHANGELOG
ADDED
data/README.rdoc
CHANGED
@@ -167,31 +167,17 @@ Giving you the familiar enum methods
|
|
167
167
|
|
168
168
|
There is support for Formtastic[http://github.com/justinfrench/formtastic] and SimpleForm[http://github.com/plataformatec/simple_form]
|
169
169
|
to make it easier to use the enum values as select options. The plugins have an almost identical API which is handy.
|
170
|
-
|
170
|
+
You just need to require the extension for the respective plugin in an initialiser:
|
171
171
|
|
172
|
-
|
173
|
-
|
174
|
-
In an initialiser:
|
172
|
+
Formtastic:
|
175
173
|
|
176
174
|
require 'active_enum/form_helpers/formtastic'
|
177
175
|
|
178
|
-
|
179
|
-
|
180
|
-
<% semantic_form_for(@person) do |f| %>
|
181
|
-
<%= f.input :sex, :as => :enum %>
|
182
|
-
<% end %>
|
183
|
-
|
184
|
-
==== SimpleForm
|
185
|
-
|
186
|
-
In an initialiser:
|
176
|
+
Or, SimpleForm:
|
187
177
|
|
188
178
|
require 'active_enum/form_helpers/simple_form'
|
189
179
|
|
190
|
-
|
191
|
-
|
192
|
-
<% simple_form_for(@person) do |f| %>
|
193
|
-
<%= f.input :sex, :as => :enum %>
|
194
|
-
<% end %>
|
180
|
+
The input type will be automatically detected for enumerated attributes. You can override with the :as option as normal.
|
195
181
|
|
196
182
|
|
197
183
|
== TODO
|
data/Rakefile
CHANGED
@@ -23,7 +23,7 @@ spec = Gem::Specification.new do |s|
|
|
23
23
|
|
24
24
|
s.require_path = 'lib'
|
25
25
|
s.autorequire = GEM_NAME
|
26
|
-
s.files = %w(MIT-LICENSE README.rdoc Rakefile) + Dir.glob("{lib,spec}/**/*")
|
26
|
+
s.files = %w(active_enum.gemspec MIT-LICENSE CHANGELOG README.rdoc Rakefile) + Dir.glob("{lib,spec}/**/*")
|
27
27
|
end
|
28
28
|
|
29
29
|
desc 'Default: run specs.'
|
data/active_enum.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{active_enum}
|
5
|
+
s.version = "0.8.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Adam Meehan"]
|
9
|
+
s.autorequire = %q{active_enum}
|
10
|
+
s.date = %q{2010-07-19}
|
11
|
+
s.description = %q{Define enum classes in Rails and use them to enumerate ActiveRecord attributes}
|
12
|
+
s.email = %q{adam.meehan@gmail.com}
|
13
|
+
s.extra_rdoc_files = ["README.rdoc"]
|
14
|
+
s.files = ["active_enum.gemspec", "MIT-LICENSE", "CHANGELOG", "README.rdoc", "Rakefile", "lib/active_enum", "lib/active_enum/acts_as_enum.rb", "lib/active_enum/base.rb", "lib/active_enum/extensions.rb", "lib/active_enum/form_helpers", "lib/active_enum/form_helpers/formtastic.rb", "lib/active_enum/form_helpers/simple_form.rb", "lib/active_enum/storage", "lib/active_enum/storage/abstract_store.rb", "lib/active_enum/storage/memory_store.rb", "lib/active_enum/version.rb", "lib/active_enum.rb", "lib/generators", "spec/active_enum", "spec/active_enum/acts_as_enum_spec.rb", "spec/active_enum/base_spec.rb", "spec/active_enum/extensions_spec.rb", "spec/active_enum/form_helpers", "spec/active_enum/form_helpers/formtastic_spec.rb", "spec/active_enum/form_helpers/simple_form_spec.rb", "spec/active_enum/storage", "spec/active_enum/storage/memory_store_spec.rb", "spec/active_enum_spec.rb", "spec/schema.rb", "spec/spec_helper.rb"]
|
15
|
+
s.homepage = %q{http://github.com/adzap/active_enum}
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.rubyforge_project = %q{active_enum}
|
18
|
+
s.rubygems_version = %q{1.3.7}
|
19
|
+
s.summary = %q{Define enum classes in Rails and use them to enumerate ActiveRecord attributes}
|
20
|
+
|
21
|
+
if s.respond_to? :specification_version then
|
22
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
23
|
+
s.specification_version = 3
|
24
|
+
|
25
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
26
|
+
else
|
27
|
+
end
|
28
|
+
else
|
29
|
+
end
|
30
|
+
end
|
data/lib/active_enum.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'active_enum/base'
|
2
2
|
require 'active_enum/extensions'
|
3
3
|
require 'active_enum/acts_as_enum'
|
4
|
+
require 'active_enum/storage/abstract_store'
|
4
5
|
require 'active_enum/version'
|
5
6
|
|
6
7
|
module ActiveEnum
|
@@ -10,6 +11,9 @@ module ActiveEnum
|
|
10
11
|
mattr_accessor :use_name_as_value
|
11
12
|
self.use_name_as_value = false
|
12
13
|
|
14
|
+
mattr_accessor :storage
|
15
|
+
self.storage = :memory
|
16
|
+
|
13
17
|
class Configuration
|
14
18
|
def enum(name, &block)
|
15
19
|
class_name = name.to_s.camelize
|
data/lib/active_enum/base.rb
CHANGED
@@ -5,70 +5,69 @@ module ActiveEnum
|
|
5
5
|
class Base
|
6
6
|
|
7
7
|
class << self
|
8
|
+
attr_accessor :store
|
8
9
|
|
9
10
|
def inherited(subclass)
|
10
11
|
ActiveEnum.enum_classes << subclass
|
11
12
|
end
|
12
13
|
|
13
|
-
# :name => 'Foo', implicit ID
|
14
14
|
# :id => 1, :name => 'Foo'
|
15
|
+
# :name => 'Foo'
|
15
16
|
# 1 => 'Foo'
|
16
17
|
#
|
17
18
|
def value(enum_value)
|
18
|
-
|
19
|
-
|
20
|
-
id, name = id_and_name(enum_value)
|
21
|
-
check_duplicate(id, name)
|
22
|
-
|
23
|
-
@values << [id, name]
|
24
|
-
sort_values! unless @order == :as_defined
|
19
|
+
store.set *id_and_name_and_meta(enum_value)
|
25
20
|
end
|
26
21
|
|
27
|
-
#
|
22
|
+
# Order enum values. Allowed values are :asc, :desc or :as_defined
|
28
23
|
#
|
29
24
|
def order(order)
|
30
25
|
@order = order
|
31
26
|
end
|
32
27
|
|
33
28
|
def all
|
34
|
-
|
29
|
+
store.values
|
35
30
|
end
|
36
31
|
|
37
32
|
def ids
|
38
|
-
|
33
|
+
store.values.map {|v| v[0] }
|
39
34
|
end
|
40
35
|
|
41
36
|
def names
|
42
|
-
|
37
|
+
store.values.map {|v| v[1] }
|
43
38
|
end
|
44
39
|
|
45
40
|
def to_select
|
46
|
-
|
41
|
+
store.values.map {|v| [v[1], v[0]] }
|
47
42
|
end
|
48
43
|
|
49
44
|
def [](index)
|
50
45
|
if index.is_a?(Fixnum)
|
51
|
-
row =
|
46
|
+
row = store.get_by_id(index)
|
52
47
|
row[1] if row
|
53
48
|
else
|
54
|
-
row =
|
49
|
+
row = store.get_by_name(index)
|
55
50
|
row[0] if row
|
56
51
|
end
|
57
52
|
end
|
58
53
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
54
|
+
def meta(index)
|
55
|
+
row = if index.is_a?(Fixnum)
|
56
|
+
store.get_by_id(index)
|
57
|
+
else
|
58
|
+
store.get_by_name(index)
|
59
|
+
end
|
60
|
+
row[2] || {} if row
|
63
61
|
end
|
64
62
|
|
65
|
-
|
66
|
-
@values.rassoc(index.to_s) || @values.rassoc(index.to_s.titleize)
|
67
|
-
end
|
63
|
+
private
|
68
64
|
|
69
|
-
def
|
65
|
+
def id_and_name_and_meta(hash)
|
70
66
|
if hash.has_key?(:id) || hash.has_key?(:name)
|
71
|
-
|
67
|
+
id = hash.delete(:id) || next_id
|
68
|
+
name = hash.delete(:name)
|
69
|
+
meta = hash
|
70
|
+
return id, name, (meta.blank? ? nil : meta)
|
72
71
|
elsif hash.keys.first.is_a?(Fixnum)
|
73
72
|
return *Array(hash).first
|
74
73
|
else
|
@@ -77,25 +76,16 @@ module ActiveEnum
|
|
77
76
|
end
|
78
77
|
|
79
78
|
def next_id
|
80
|
-
|
79
|
+
ids.max.to_i + 1
|
81
80
|
end
|
82
81
|
|
83
|
-
def
|
84
|
-
|
85
|
-
raise ActiveEnum::DuplicateValue, "The id #{id} is already defined for #{self} enum."
|
86
|
-
elsif lookup_by_name(name)
|
87
|
-
raise ActiveEnum::DuplicateValue, "The name #{name} is already defined for #{self} enum."
|
88
|
-
end
|
82
|
+
def store
|
83
|
+
@store ||= storage_class.new(self, @order || :asc)
|
89
84
|
end
|
90
85
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
@values.sort! {|a,b| a[0] <=> b[0] }
|
95
|
-
when :desc
|
96
|
-
@values.sort! {|a,b| b[0] <=> a[0] }
|
97
|
-
end
|
98
|
-
end
|
86
|
+
def storage_class
|
87
|
+
"ActiveEnum::Storage::#{ActiveEnum.storage.to_s.classify}Store".constantize
|
88
|
+
end
|
99
89
|
|
100
90
|
end
|
101
91
|
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module ActiveEnum
|
2
|
+
module Storage
|
3
|
+
autoload :MemoryStore, "active_enum/storage/memory_store"
|
4
|
+
|
5
|
+
class NotImplemented < StandardError; end
|
6
|
+
|
7
|
+
class AbstractStore
|
8
|
+
def initialize(enum_class, order)
|
9
|
+
@enum, @order = enum_class, order
|
10
|
+
end
|
11
|
+
|
12
|
+
def set(id, name, meta=nil)
|
13
|
+
raise NotImplemented
|
14
|
+
end
|
15
|
+
|
16
|
+
def get_by_id(id)
|
17
|
+
raise NotImplemented
|
18
|
+
end
|
19
|
+
|
20
|
+
def get_by_name(name)
|
21
|
+
raise NotImplemented
|
22
|
+
end
|
23
|
+
|
24
|
+
def values
|
25
|
+
raise NotImplemented
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module ActiveEnum
|
2
|
+
module Storage
|
3
|
+
class MemoryStore < AbstractStore
|
4
|
+
def set(id, name, meta=nil)
|
5
|
+
check_duplicate id, name
|
6
|
+
values << [id, name, meta].compact
|
7
|
+
sort!
|
8
|
+
end
|
9
|
+
|
10
|
+
def get_by_id(id)
|
11
|
+
values.assoc(id)
|
12
|
+
end
|
13
|
+
|
14
|
+
def get_by_name(name)
|
15
|
+
values.rassoc(name.to_s) || values.rassoc(name.to_s.titleize)
|
16
|
+
end
|
17
|
+
|
18
|
+
def values
|
19
|
+
@values ||= []
|
20
|
+
end
|
21
|
+
|
22
|
+
def check_duplicate(id, name)
|
23
|
+
if get_by_id(id) || get_by_name(name)
|
24
|
+
raise ActiveEnum::DuplicateValue
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def sort!
|
29
|
+
return if @order == :as_defined
|
30
|
+
case @order
|
31
|
+
when :asc
|
32
|
+
values.sort! {|a,b| a[0] <=> b[0] }
|
33
|
+
when :desc
|
34
|
+
values.sort! {|a,b| b[0] <=> a[0] }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/active_enum/version.rb
CHANGED
@@ -1,70 +1,106 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
2
|
|
3
3
|
describe ActiveEnum::Base do
|
4
|
-
|
5
|
-
|
6
|
-
class
|
4
|
+
|
5
|
+
context ".store" do
|
6
|
+
it 'should load the storage class instance using the storage setting' do
|
7
|
+
ActiveEnum::Base.send(:store).should be_instance_of(ActiveEnum::Storage::MemoryStore)
|
7
8
|
end
|
8
|
-
ActiveEnum.enum_classes.should == [NewEnum]
|
9
9
|
end
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
context ".enum_classes" do
|
12
|
+
it 'should return all enum classes defined use class definition' do
|
13
|
+
ActiveEnum.enum_classes = []
|
14
|
+
class NewEnum < ActiveEnum::Base; end
|
15
|
+
ActiveEnum.enum_classes.should == [NewEnum]
|
16
|
+
end
|
14
17
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
+
it 'should return all enum classes defined using define block' do
|
19
|
+
ActiveEnum.enum_classes = []
|
20
|
+
ActiveEnum.define do
|
21
|
+
enum(:bulk_new_enum) { }
|
22
|
+
end
|
23
|
+
ActiveEnum.enum_classes.should == [BulkNewEnum]
|
18
24
|
end
|
19
|
-
enum.all.should == [[1,'Name']]
|
20
25
|
end
|
21
26
|
|
22
|
-
|
23
|
-
|
24
|
-
|
27
|
+
context ".all" do
|
28
|
+
it 'should return an empty array when no values defined' do
|
29
|
+
define_enum.all.should == []
|
25
30
|
end
|
26
|
-
enum.all.should == [[1,'Name']]
|
27
31
|
end
|
28
32
|
|
29
|
-
|
30
|
-
|
31
|
-
|
33
|
+
context ".value" do
|
34
|
+
it 'should allow me to define a value with an id and name' do
|
35
|
+
enum = define_enum do
|
36
|
+
value :id => 1, :name => 'Name'
|
37
|
+
end
|
38
|
+
enum.all.should == [[1,'Name']]
|
32
39
|
end
|
33
|
-
enum.all.should == [[1,'Name']]
|
34
|
-
end
|
35
40
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
41
|
+
it 'should allow me to define a value with a name only' do
|
42
|
+
enum = define_enum do
|
43
|
+
value :name => 'Name'
|
44
|
+
end
|
45
|
+
enum.all.should == [[1,'Name']]
|
40
46
|
end
|
41
|
-
enum.all.should == [[1,'Name 1'], [2, 'Name 2']]
|
42
|
-
end
|
43
47
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
value :id => 1, :name => 'Name 1'
|
48
|
-
value :id => 1, :name => 'Name 2'
|
48
|
+
it 'should allow me to define a value as hash with id as key and name as value' do
|
49
|
+
enum = define_enum do
|
50
|
+
value 1 => 'Name'
|
49
51
|
end
|
50
|
-
|
51
|
-
|
52
|
+
enum.all.should == [[1,'Name']]
|
53
|
+
end
|
52
54
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
value :id => 1, :name => 'Name'
|
57
|
-
value :id => 2, :name => 'Name'
|
55
|
+
it 'should allow to define meta data value with extra key value pairs' do
|
56
|
+
enum = define_enum do
|
57
|
+
value :id => 1, :name => 'Name', :description => 'extra'
|
58
58
|
end
|
59
|
-
|
59
|
+
enum.all.should == [[1,'Name',{:description => 'extra'}]]
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'should increment value ids when defined without ids' do
|
63
|
+
enum = define_enum do
|
64
|
+
value :name => 'Name 1'
|
65
|
+
value :name => 'Name 2'
|
66
|
+
end
|
67
|
+
enum.all.should == [[1,'Name 1'], [2, 'Name 2']]
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'should raise error if the id is a duplicate' do
|
71
|
+
lambda do
|
72
|
+
define_enum do
|
73
|
+
value :id => 1, :name => 'Name 1'
|
74
|
+
value :id => 1, :name => 'Name 2'
|
75
|
+
end
|
76
|
+
end.should raise_error(ActiveEnum::DuplicateValue)
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'should raise error if the name is a duplicate' do
|
80
|
+
lambda do
|
81
|
+
define_enum do
|
82
|
+
value :id => 1, :name => 'Name'
|
83
|
+
value :id => 2, :name => 'Name'
|
84
|
+
end
|
85
|
+
end.should raise_error(ActiveEnum::DuplicateValue)
|
86
|
+
end
|
60
87
|
end
|
61
88
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
89
|
+
context ".meta" do
|
90
|
+
it 'should return meta values hash for a given index value' do
|
91
|
+
enum = define_enum do
|
92
|
+
value :id => 1, :name => 'Name', :description => 'extra'
|
93
|
+
end
|
94
|
+
enum.meta(1).should == {:description => 'extra'}
|
66
95
|
end
|
67
|
-
|
96
|
+
|
97
|
+
it 'should return empty hash for index with no meta defined' do
|
98
|
+
enum = define_enum do
|
99
|
+
value :id => 1, :name => 'Name'
|
100
|
+
end
|
101
|
+
enum.meta(1).should == {}
|
102
|
+
end
|
103
|
+
|
68
104
|
end
|
69
105
|
|
70
106
|
context "sorting" do
|
@@ -96,23 +132,27 @@ describe ActiveEnum::Base do
|
|
96
132
|
end
|
97
133
|
end
|
98
134
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
135
|
+
context ".ids" do
|
136
|
+
it 'should return array of ids' do
|
137
|
+
enum = define_enum do
|
138
|
+
value :id => 1, :name => 'Name 1'
|
139
|
+
value :id => 2, :name => 'Name 2'
|
140
|
+
end
|
141
|
+
enum.ids.should == [1,2]
|
103
142
|
end
|
104
|
-
enum.ids.should == [1,2]
|
105
143
|
end
|
106
144
|
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
145
|
+
context ".names" do
|
146
|
+
it 'should return array of names' do
|
147
|
+
enum = define_enum do
|
148
|
+
value :id => 1, :name => 'Name 1'
|
149
|
+
value :id => 2, :name => 'Name 2'
|
150
|
+
end
|
151
|
+
enum.names.should == ['Name 1', 'Name 2']
|
111
152
|
end
|
112
|
-
enum.names.should == ['Name 1', 'Name 2']
|
113
153
|
end
|
114
154
|
|
115
|
-
|
155
|
+
context "element reference method" do
|
116
156
|
|
117
157
|
it 'should return name when given an id' do
|
118
158
|
enum = define_enum do
|
@@ -141,21 +181,23 @@ describe ActiveEnum::Base do
|
|
141
181
|
|
142
182
|
end
|
143
183
|
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
184
|
+
context ".to_select" do
|
185
|
+
it 'should return array for select helpers' do
|
186
|
+
enum = define_enum do
|
187
|
+
value :id => 1, :name => 'Name 1'
|
188
|
+
value :id => 2, :name => 'Name 2'
|
189
|
+
end
|
190
|
+
enum.to_select.should == [['Name 1',1], ['Name 2',2]]
|
148
191
|
end
|
149
|
-
enum.to_select.should == [['Name 1',1], ['Name 2',2]]
|
150
|
-
end
|
151
192
|
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
193
|
+
it 'should return array sorted using order setting' do
|
194
|
+
enum = define_enum() do
|
195
|
+
order :desc
|
196
|
+
value :id => 1, :name => 'Name 1'
|
197
|
+
value :id => 2, :name => 'Name 2'
|
198
|
+
end
|
199
|
+
enum.to_select.should == [['Name 2',2], ['Name 1',1]]
|
157
200
|
end
|
158
|
-
enum.to_select.should == [['Name 2',2], ['Name 1',1]]
|
159
201
|
end
|
160
202
|
|
161
203
|
def define_enum(&block)
|
@@ -0,0 +1,113 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
class TestMemoryStoreEnum < ActiveEnum::Base; end
|
4
|
+
class TestOtherAREnum < ActiveEnum::Base; end
|
5
|
+
|
6
|
+
describe ActiveEnum::Storage::MemoryStore do
|
7
|
+
attr_accessor :store
|
8
|
+
|
9
|
+
context '#set' do
|
10
|
+
it 'should store value of id and name' do
|
11
|
+
store.set 1, 'test name'
|
12
|
+
store.values.should == [[1, 'test name']]
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should store value of id, name and meta hash' do
|
16
|
+
store.set 1, 'test name', :description => 'meta'
|
17
|
+
store.values.should == [[1, 'test name', {:description => 'meta'}]]
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should raise error if duplicate id' do
|
21
|
+
lambda {
|
22
|
+
store.set 1, 'Name 1'
|
23
|
+
store.set 1, 'Other Name'
|
24
|
+
}.should raise_error(ActiveEnum::DuplicateValue)
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should raise error if duplicate name' do
|
28
|
+
lambda {
|
29
|
+
store.set 1, 'Name 1'
|
30
|
+
store.set 2, 'Name 1'
|
31
|
+
}.should raise_error(ActiveEnum::DuplicateValue)
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'should raise error if duplicate name matches title-case name' do
|
35
|
+
lambda {
|
36
|
+
store.set 1, 'Name 1'
|
37
|
+
store.set 2, 'name 1'
|
38
|
+
}.should raise_error(ActiveEnum::DuplicateValue)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context "#values" do
|
43
|
+
it 'should return array of stored values' do
|
44
|
+
store.set 1, 'Name 1'
|
45
|
+
store.values.should == [[1, 'Name 1']]
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'should return values for set enum only' do
|
49
|
+
alt_store.set 1, 'Other Name 1'
|
50
|
+
store.set 1, 'Name 1'
|
51
|
+
store.values.should == [[1, 'Name 1']]
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context "#get_by_id" do
|
56
|
+
it 'should return the value for a given id' do
|
57
|
+
store.set 1, 'test name'
|
58
|
+
store.get_by_id(1).should == [1, 'test name']
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'should return nil when id not found' do
|
62
|
+
store.get_by_id(1).should be_nil
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
context "#get_by_name" do
|
67
|
+
it 'should return the value for a given name' do
|
68
|
+
store.set 1, 'test name'
|
69
|
+
store.get_by_name('test name').should == [1, 'test name']
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'should return the value with title-cased name for a given lowercase name' do
|
73
|
+
store.set 1, 'Test Name'
|
74
|
+
store.get_by_name('test name').should == [1, 'Test Name']
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'should return nil when name not found' do
|
78
|
+
store.get_by_name('test name').should be_nil
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
context "sort!" do
|
83
|
+
it 'should sort values ascending when passed :asc' do
|
84
|
+
@order = :asc
|
85
|
+
store.set 2, 'Name 2'
|
86
|
+
store.set 1, 'Name 1'
|
87
|
+
store.values.should == [[1,'Name 1'], [2, 'Name 2']]
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'should sort values descending when passed :desc' do
|
91
|
+
@order = :desc
|
92
|
+
store.set 1, 'Name 1'
|
93
|
+
store.set 2, 'Name 2'
|
94
|
+
store.values.should == [[2, 'Name 2'], [1,'Name 1']]
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'should not sort values when passed :as_defined' do
|
98
|
+
@order = :as_defined
|
99
|
+
store.set 1, 'Name 1'
|
100
|
+
store.set 3, 'Name 3'
|
101
|
+
store.set 2, 'Name 2'
|
102
|
+
store.values.should == [[1,'Name 1'], [3,'Name 3'], [2, 'Name 2']]
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def store
|
107
|
+
@store ||= ActiveEnum::Storage::MemoryStore.new(TestMemoryStoreEnum, @order)
|
108
|
+
end
|
109
|
+
|
110
|
+
def alt_store
|
111
|
+
@alt_store ||= ActiveEnum::Storage::MemoryStore.new(TestOtherAREnum, :asc)
|
112
|
+
end
|
113
|
+
end
|
data/spec/active_enum_spec.rb
CHANGED
@@ -2,7 +2,7 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
2
2
|
|
3
3
|
describe "Bulk enum definitions" do
|
4
4
|
|
5
|
-
it 'define enum constants using block' do
|
5
|
+
it 'should define enum constants using block' do
|
6
6
|
ActiveEnum.define do
|
7
7
|
enum(:foo) do
|
8
8
|
value :id => 1, :name => 'Foo 1'
|
@@ -17,4 +17,8 @@ describe "Bulk enum definitions" do
|
|
17
17
|
Bar.all.should == [[1,'Bar 1']]
|
18
18
|
end
|
19
19
|
|
20
|
+
it 'should use the memory store by default' do
|
21
|
+
ActiveEnum.storage.should == :memory
|
22
|
+
end
|
23
|
+
|
20
24
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -3,15 +3,21 @@ $:.unshift(File.join(File.dirname(__FILE__), '..', 'spec'))
|
|
3
3
|
|
4
4
|
require 'rubygems'
|
5
5
|
require 'rspec/autorun'
|
6
|
-
require 'rspec/rails'
|
7
6
|
|
8
7
|
require 'active_record'
|
8
|
+
require 'action_controller'
|
9
|
+
require 'action_view'
|
10
|
+
require 'action_mailer'
|
11
|
+
|
9
12
|
require 'active_enum'
|
10
13
|
|
11
14
|
RAILS_ROOT = File.dirname(__FILE__)
|
12
15
|
|
16
|
+
require 'rspec/rails'
|
17
|
+
|
13
18
|
ActiveRecord::Migration.verbose = false
|
14
19
|
ActiveRecord::Base.establish_connection({:adapter => 'sqlite3', :database => ':memory:'})
|
20
|
+
ActiveRecord::Base.logger = Logger.new('/dev/null')
|
15
21
|
|
16
22
|
require 'schema'
|
17
23
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 63
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 8
|
9
|
+
- 0
|
10
|
+
version: 0.8.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Adam Meehan
|
@@ -15,7 +15,7 @@ autorequire: active_enum
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-07-
|
18
|
+
date: 2010-07-19 00:00:00 +10:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -28,7 +28,9 @@ extensions: []
|
|
28
28
|
extra_rdoc_files:
|
29
29
|
- README.rdoc
|
30
30
|
files:
|
31
|
+
- active_enum.gemspec
|
31
32
|
- MIT-LICENSE
|
33
|
+
- CHANGELOG
|
32
34
|
- README.rdoc
|
33
35
|
- Rakefile
|
34
36
|
- lib/active_enum/acts_as_enum.rb
|
@@ -36,6 +38,8 @@ files:
|
|
36
38
|
- lib/active_enum/extensions.rb
|
37
39
|
- lib/active_enum/form_helpers/formtastic.rb
|
38
40
|
- lib/active_enum/form_helpers/simple_form.rb
|
41
|
+
- lib/active_enum/storage/abstract_store.rb
|
42
|
+
- lib/active_enum/storage/memory_store.rb
|
39
43
|
- lib/active_enum/version.rb
|
40
44
|
- lib/active_enum.rb
|
41
45
|
- spec/active_enum/acts_as_enum_spec.rb
|
@@ -43,6 +47,7 @@ files:
|
|
43
47
|
- spec/active_enum/extensions_spec.rb
|
44
48
|
- spec/active_enum/form_helpers/formtastic_spec.rb
|
45
49
|
- spec/active_enum/form_helpers/simple_form_spec.rb
|
50
|
+
- spec/active_enum/storage/memory_store_spec.rb
|
46
51
|
- spec/active_enum_spec.rb
|
47
52
|
- spec/schema.rb
|
48
53
|
- spec/spec_helper.rb
|