jeffp-enumerated_attribute 0.1.7 → 0.2.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +8 -1
- data/README.rdoc +146 -32
- data/Rakefile +29 -14
- data/lib/enumerated_attribute.rb +0 -1
- data/lib/enumerated_attribute/attribute.rb +28 -26
- data/lib/enumerated_attribute/attribute/attribute_descriptor.rb +50 -0
- data/lib/enumerated_attribute/integrations/active_record.rb +6 -3
- data/lib/enumerated_attribute/integrations/object.rb +2 -1
- data/lib/enumerated_attribute/method_definition_dsl.rb +21 -11
- data/lib/enumerated_attribute/rails_helpers.rb +73 -0
- data/spec/active_record/active_record_spec.rb +49 -2
- data/spec/active_record/association_test_classes.rb +40 -0
- data/spec/active_record/associations_spec.rb +130 -0
- data/spec/active_record/test_in_memory.rb +40 -0
- data/spec/poro_spec.rb +32 -4
- data/spec/rails/README +243 -0
- data/spec/rails/Rakefile +10 -0
- data/spec/rails/app/controllers/application_controller.rb +10 -0
- data/spec/rails/app/controllers/form_test_controller.rb +38 -0
- data/spec/rails/app/helpers/application_helper.rb +3 -0
- data/spec/rails/app/helpers/form_test_helper.rb +2 -0
- data/spec/rails/app/models/user.rb +9 -0
- data/spec/rails/app/views/form_test/form.html.erb +1 -0
- data/spec/rails/app/views/form_test/form_for.html.erb +10 -0
- data/spec/rails/app/views/form_test/form_tag.html.erb +9 -0
- data/spec/rails/app/views/form_test/index.html.erb +6 -0
- data/spec/rails/app/views/layouts/application.html.erb +11 -0
- data/spec/rails/config/boot.rb +110 -0
- data/spec/rails/config/database.yml +22 -0
- data/spec/rails/config/environment.rb +45 -0
- data/spec/rails/config/environments/development.rb +17 -0
- data/spec/rails/config/environments/production.rb +28 -0
- data/spec/rails/config/environments/test.rb +28 -0
- data/spec/rails/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/rails/config/initializers/inflections.rb +10 -0
- data/spec/rails/config/initializers/mime_types.rb +5 -0
- data/spec/rails/config/initializers/new_rails_defaults.rb +19 -0
- data/spec/rails/config/initializers/session_store.rb +15 -0
- data/spec/rails/config/locales/en.yml +5 -0
- data/spec/rails/config/routes.rb +43 -0
- data/spec/rails/db/development.sqlite3 +0 -0
- data/spec/rails/db/migrate/20090804230221_create_sessions.rb +16 -0
- data/spec/rails/db/migrate/20090804230546_create_users.rb +21 -0
- data/spec/rails/db/schema.rb +35 -0
- data/spec/rails/db/test.sqlite3 +0 -0
- data/spec/rails/public/404.html +30 -0
- data/spec/rails/public/422.html +30 -0
- data/spec/rails/public/500.html +30 -0
- data/spec/rails/public/favicon.ico +0 -0
- data/spec/rails/public/images/rails.png +0 -0
- data/spec/rails/public/index.html +275 -0
- data/spec/rails/public/javascripts/application.js +2 -0
- data/spec/rails/public/javascripts/controls.js +963 -0
- data/spec/rails/public/javascripts/dragdrop.js +973 -0
- data/spec/rails/public/javascripts/effects.js +1128 -0
- data/spec/rails/public/javascripts/prototype.js +4320 -0
- data/spec/rails/public/robots.txt +5 -0
- data/spec/rails/public/stylesheets/scaffold.css +54 -0
- data/spec/rails/script/about +4 -0
- data/spec/rails/script/autospec +6 -0
- data/spec/rails/script/console +3 -0
- data/spec/rails/script/dbconsole +3 -0
- data/spec/rails/script/destroy +3 -0
- data/spec/rails/script/generate +3 -0
- data/spec/rails/script/performance/benchmarker +3 -0
- data/spec/rails/script/performance/profiler +3 -0
- data/spec/rails/script/plugin +3 -0
- data/spec/rails/script/runner +3 -0
- data/spec/rails/script/server +3 -0
- data/spec/rails/script/spec +10 -0
- data/spec/rails/script/spec_server +9 -0
- data/spec/rails/spec/controllers/form_test_controller_spec.rb +41 -0
- data/spec/rails/spec/integrations/enum_select_spec.rb +73 -0
- data/spec/rails/spec/matchers.rb +12 -0
- data/spec/rails/spec/rcov.opts +2 -0
- data/spec/rails/spec/spec.opts +4 -0
- data/spec/rails/spec/spec_helper.rb +38 -0
- data/spec/rails/spec/views/form_test/form.html.erb_spec.rb +12 -0
- data/spec/rails/spec/views/form_test/form_for.html.erb_spec.rb +12 -0
- data/spec/rails/spec/views/form_test/form_tag.html.erb_spec.rb +12 -0
- data/spec/rcov.opts +2 -0
- data/spec/spec.opts +4 -0
- data/spec/tractor.rb +4 -1
- metadata +103 -5
data/.gitignore
CHANGED
data/README.rdoc
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
= enumerated_attribute
|
2
2
|
|
3
|
-
|
3
|
+
Easily code enumerations for your models and expose them as
|
4
|
+
drop-down lists with the +enum_select+ helper, or use any of +enumerated_attribute+
|
5
|
+
features to simplify coding enumerations in any Ruby object.
|
4
6
|
|
5
7
|
== Resources
|
6
8
|
|
@@ -12,13 +14,16 @@ Source
|
|
12
14
|
|
13
15
|
* git://github.com/jeffp/enumerated_attribute.git
|
14
16
|
|
17
|
+
Install
|
18
|
+
|
19
|
+
* sudo gem install jeffp-enumerated_attribute --source=http://gems.github.com
|
20
|
+
|
15
21
|
== Description
|
16
22
|
|
17
23
|
Enumerations are a common and useful pattern in programming. Typically, in Ruby,
|
18
24
|
enumerated attributes are implemented with strings, symbols or constants. Often the
|
19
25
|
developer is burdened with repeatedly defining common methods in support of each
|
20
|
-
attribute. Such repetition coding unnecessarily increases
|
21
|
-
costs and wastes time.
|
26
|
+
attribute. Such repetition coding unnecessarily increases costs and wastes time.
|
22
27
|
|
23
28
|
+enumerated_attribute+ simplifies the definition of enumerated attributes by emphasizing
|
24
29
|
convention and DRYing the implementation. Repetitive code such as initializers, accessors,
|
@@ -26,32 +31,78 @@ predicate and enumeration methods are automatically generated, resulting in bett
|
|
26
31
|
encapsulation, quicker implementation and cleaner code.
|
27
32
|
|
28
33
|
Features include:
|
29
|
-
*
|
34
|
+
* ActiveRecord integration
|
35
|
+
* ActionView form helpers
|
36
|
+
* Configurable enumeration labels
|
30
37
|
* Auto-defined attribute methods
|
31
38
|
* Dynamically-generated predicate methods
|
32
|
-
*
|
33
|
-
*
|
34
|
-
|
39
|
+
* Automatic initialization
|
40
|
+
* Advanced configuration DSL
|
41
|
+
|
42
|
+
== Setup
|
43
|
+
|
44
|
+
For a Ruby application, install the gem and require it
|
45
|
+
|
46
|
+
require 'enumerated_attribute'
|
47
|
+
|
48
|
+
or for a rails application configure the gem in the config block of the
|
49
|
+
config/environment.rb file
|
50
|
+
|
51
|
+
config.gem "jeffp-enumerated_attribute", :lib=>"enumerated_attribute"
|
52
|
+
|
53
|
+
and run the gem install rake task
|
54
|
+
|
55
|
+
rake gems:install
|
56
|
+
|
35
57
|
|
36
|
-
== Example
|
58
|
+
== Rails Example
|
37
59
|
|
38
|
-
Here's
|
60
|
+
Here's an example of +enumerated_attribute+ features in a Rails application:
|
61
|
+
|
62
|
+
In the migration, declare your enumeration attributes with +enum+
|
63
|
+
|
64
|
+
create_table :users, :force=>true do |t|
|
65
|
+
t.string :first_name
|
66
|
+
t.enum :gender
|
67
|
+
t.enum :degree
|
68
|
+
...
|
69
|
+
end
|
70
|
+
|
71
|
+
Define the enumerations in your models with +enum_attr+
|
72
|
+
|
73
|
+
class User < ActiveRecord::Base
|
74
|
+
enum_attr :gender, %w(male female)
|
75
|
+
enum_attr :degree, %w(^none high_school college graduate)
|
76
|
+
end
|
77
|
+
|
78
|
+
Expose the enumeration in your forms with +enum_select+
|
79
|
+
|
80
|
+
<% form_for :user do |f| %>
|
81
|
+
<%= f.label :user %> <%= f.text_field :first_name %><br/>
|
82
|
+
<%= f.label :gender %> <%= f.enum_select :gender %><br/>
|
83
|
+
<%= f.label :degree %> <%= f.enum_select :degree %><br/>
|
84
|
+
<%= submit_tag 'save' %>
|
85
|
+
<% end %>
|
86
|
+
|
87
|
+
The select options text can be customized. See 'Customizing Labels' in the Integration section.
|
88
|
+
|
89
|
+
== Ruby Example
|
90
|
+
|
91
|
+
Here's an example of +enumerated_attribute+ features in a Ruby application:
|
39
92
|
|
40
93
|
require 'enumerated_attribute'
|
41
94
|
|
42
95
|
class Tractor
|
43
|
-
enum_attr :gear, %w(reverse ^neutral first second over_drive)
|
44
|
-
|
45
|
-
upshift { self.gear_is_not_in_over_drive? ? self.gear_next : self.gear }
|
46
|
-
end
|
96
|
+
enum_attr :gear, %w(reverse ^neutral first second over_drive)
|
97
|
+
|
47
98
|
end
|
48
99
|
|
49
100
|
t = Tractor.new
|
50
101
|
t.gear # => :neutral
|
51
|
-
t.
|
52
|
-
t.
|
53
|
-
t.
|
54
|
-
t.
|
102
|
+
t.neutral? # => true
|
103
|
+
t.gear_next # => :first
|
104
|
+
t.not_neutral? # => true
|
105
|
+
t.gear_previous # => :neutral
|
55
106
|
t.gear = :second # => :second
|
56
107
|
t.gear_is_not_in_first? # => true
|
57
108
|
|
@@ -203,22 +254,25 @@ Alternatively, the :init option can be used to indicate the initial value of the
|
|
203
254
|
|
204
255
|
==== Setting Attributes to nil
|
205
256
|
|
206
|
-
By default, the attribute setter
|
207
|
-
|
257
|
+
By default, the attribute setter allows nils unless the :nil option is set to false.
|
258
|
+
When :nil is set to false, the attribute may initialize to nil, but may not be set
|
259
|
+
to nil thereafter.
|
208
260
|
|
209
261
|
class Tractor
|
210
|
-
enum_attr :plow %w(
|
262
|
+
enum_attr :plow, %w(up down), :nil=>false
|
211
263
|
end
|
212
264
|
|
213
265
|
t = Tractor.new
|
214
|
-
t.plow # =>
|
215
|
-
t.plow_nil? # =>
|
216
|
-
t.plow =
|
217
|
-
t.plow_is_nil? # =>
|
218
|
-
t.plow_is_not_nil? # =>
|
266
|
+
t.plow # => nil
|
267
|
+
t.plow_nil? # => true
|
268
|
+
t.plow = :up # => :up
|
269
|
+
t.plow_is_nil? # => false
|
270
|
+
t.plow_is_not_nil? # => true
|
271
|
+
t.plow = nil # => raises error
|
219
272
|
|
220
273
|
Regardless of the :nil option setting, the plugin can dynamically recognize and define
|
221
|
-
predicate methods for testing 'nil' values.
|
274
|
+
predicate methods for testing 'nil' values. The setter methods also treat empty
|
275
|
+
strings (or '') as nil values.
|
222
276
|
|
223
277
|
|
224
278
|
==== Changing Method Names
|
@@ -357,6 +411,62 @@ enumeration functionality is consistent across integrations.
|
|
357
411
|
o.invoice # => "43556334-W84"
|
358
412
|
|
359
413
|
|
414
|
+
==== Labels
|
415
|
+
|
416
|
+
Each enumeration value has a corresponding text label. The defaults are made
|
417
|
+
up from the enumeration symbols. For the Tractor class example:
|
418
|
+
|
419
|
+
t=Tractor.new
|
420
|
+
t.enums(:gear) # => [:reverse, :neutral, :first, :second, :over_drive]
|
421
|
+
t.enums(:gear).labels # => ['Reverse', 'Neutral', 'First', 'Second', 'Over drive']
|
422
|
+
|
423
|
+
The +enums(:attribute)+ method provides information about the attribute's enumerations.
|
424
|
+
It is the same as the plural form of the attribute name. There are several kinds
|
425
|
+
of information available from the +enums+ method.
|
426
|
+
|
427
|
+
t=Tractor.new
|
428
|
+
e = t.enums(:plow) # => [:up, :down]
|
429
|
+
e.labels # => ['Up', 'Down']
|
430
|
+
e.hash # => {:up=>'Up', :down=>'Down'}
|
431
|
+
e.select_options # => [['Up', 'up'], ['Down', 'down']]
|
432
|
+
e.label(:up) # => 'Up'
|
433
|
+
|
434
|
+
==== Customizing Labels
|
435
|
+
|
436
|
+
Labels can be customized as shown here:
|
437
|
+
|
438
|
+
class User < ActiveRecord::Base
|
439
|
+
enum_attr :contact_options, %w(none phone email mail) do
|
440
|
+
label :none=>'Please do not contact me'
|
441
|
+
label :phone=>'I would like a representative to call me'
|
442
|
+
label :email=>'I would like information via email'
|
443
|
+
label :mail=>'I would like information mailed to me'
|
444
|
+
end
|
445
|
+
end
|
446
|
+
|
447
|
+
Likewise, the labels can be provided on the same line
|
448
|
+
|
449
|
+
class Tractor
|
450
|
+
enum_attr :gear, %w(reverse ^neutral first second over_drive) do
|
451
|
+
labels :first=>'1st Gear', :second=>'2nd Gear', :over_drive=>'Over Drive'
|
452
|
+
end
|
453
|
+
end
|
454
|
+
|
455
|
+
==== View Helpers
|
456
|
+
|
457
|
+
There are two +enum_select+ helpers, one for use with +form_for+ and one for use without
|
458
|
+
it. An example for form_for was given in the examples at the beginning. Here's an
|
459
|
+
example with the +form_tag+ and a @user object.
|
460
|
+
|
461
|
+
<% form_tag :action=>:register do %>
|
462
|
+
<%= label_tag 'First name' %>: <%= text_field :user, :first_name %><br/>
|
463
|
+
<%= label_tag 'Gender' %>: <%= enum_select :user, :gender %><br/>
|
464
|
+
<%= label_tag 'Degree' %>: <%= enum_select :user, :degree %><br/>
|
465
|
+
...
|
466
|
+
<%= submit_tag 'Register' %>
|
467
|
+
<% end %>
|
468
|
+
|
469
|
+
|
360
470
|
=== Implementation Notes
|
361
471
|
|
362
472
|
==== New and Method_missing methods
|
@@ -366,7 +476,7 @@ implementations in the same class declaring an enumerated_attribute should come
|
|
366
476
|
declaration; otherwise, the 'new' and 'method_missing' implementations must chain in order to avoid
|
367
477
|
overwriting the plugin's methods. The best approach is shown here:
|
368
478
|
|
369
|
-
class
|
479
|
+
class Soup
|
370
480
|
def self.new(*args)
|
371
481
|
...
|
372
482
|
end
|
@@ -376,24 +486,28 @@ overwriting the plugin's methods. The best approach is shown here:
|
|
376
486
|
...
|
377
487
|
end
|
378
488
|
|
379
|
-
enum_attr
|
489
|
+
enum_attr temp:, %w(cold warm hot boiling)
|
380
490
|
end
|
381
491
|
|
382
492
|
|
383
493
|
== Testing
|
384
494
|
|
385
|
-
The plugin uses RSpec for testing. Make sure you have the RSpec gem installed:
|
495
|
+
The plugin uses RSpec and Webrat for testing. Make sure you have the RSpec gem installed:
|
386
496
|
|
387
|
-
gem install rspec
|
497
|
+
gem install rspec webrat
|
388
498
|
|
389
499
|
To test the plugin for regular ruby objects, run:
|
390
500
|
|
391
|
-
rake spec
|
501
|
+
rake spec:object
|
392
502
|
|
393
503
|
Testing ActiveRecord integration requires the install of Sqlite3 and the
|
394
504
|
sqlite3-ruby gem. To test ActiveRecord, run:
|
395
505
|
|
396
506
|
rake spec:active_record
|
507
|
+
|
508
|
+
And for testing +enum_select+ in form views:
|
509
|
+
|
510
|
+
rake spec:forms
|
397
511
|
|
398
512
|
To test all specs:
|
399
513
|
|
@@ -402,5 +516,5 @@ To test all specs:
|
|
402
516
|
|
403
517
|
== Dependencies
|
404
518
|
|
405
|
-
* ActiveRecord
|
519
|
+
* ActiveRecord (but not required)
|
406
520
|
* Sqlite3 and sqlite3-ruby gem (for testing)
|
data/Rakefile
CHANGED
@@ -5,12 +5,14 @@ require 'rake/contrib/sshpublisher'
|
|
5
5
|
|
6
6
|
spec = Gem::Specification.new do |s|
|
7
7
|
s.name = 'enumerated_attribute'
|
8
|
-
s.version = '0.
|
8
|
+
s.version = '0.2.0.2'
|
9
9
|
s.platform = Gem::Platform::RUBY
|
10
|
-
s.description = '
|
11
|
-
s.summary = 'Add enumerated attributes
|
10
|
+
s.description = 'Enumerated model attributes and view helpers'
|
11
|
+
s.summary = 'Add enumerated attributes to your models and expose them in drop-down lists in your views'
|
12
12
|
|
13
|
-
|
13
|
+
exclude_folders = 'spec/rails/{doc,lib,log,nbproject,tmp,vendor,test}'
|
14
|
+
exclude_files = FileList['**/*.log'] + FileList[exclude_folders+'/**/*'] + FileList[exclude_folders]
|
15
|
+
s.files = FileList['{examples,lib,tasks,spec}/**/*'] + %w(CHANGELOG.rdoc init.rb LICENSE Rakefile README.rdoc .gitignore) - exclude_files
|
14
16
|
s.require_path = 'lib'
|
15
17
|
s.has_rdoc = true
|
16
18
|
s.test_files = Dir['spec/*_spec.rb']
|
@@ -24,24 +26,37 @@ require 'spec/version'
|
|
24
26
|
require 'spec/rake/spectask'
|
25
27
|
|
26
28
|
desc "Run specs"
|
27
|
-
Spec::Rake::SpecTask.new(:spec) do |t|
|
28
|
-
t.spec_files = FileList['spec/*_spec.rb']
|
29
|
-
t.libs << 'lib' << 'spec'
|
30
|
-
t.rcov = false
|
31
|
-
#t.spec_opts = ['--options', 'spec/spec.opts']
|
32
|
-
#t.rcov_dir = 'coverage'
|
33
|
-
#t.rcov_opts = ['--exclude', "kernel,load-diff-lcs\.rb,instance_exec\.rb,lib/spec.rb,lib/spec/runner.rb,^spec/*,bin/spec,examples,/gems,/Library/Ruby,\.autotest,#{ENV['GEM_HOME']}"]
|
34
|
-
end
|
35
29
|
|
36
30
|
namespace :spec do
|
31
|
+
task :default=>:object
|
32
|
+
Spec::Rake::SpecTask.new(:object) do |t|
|
33
|
+
t.spec_files = FileList['spec/*_spec.rb']
|
34
|
+
t.libs << 'lib' << 'spec'
|
35
|
+
t.rcov = false
|
36
|
+
t.spec_opts = ['--options', 'spec/spec.opts']
|
37
|
+
#t.rcov_dir = 'coverage'
|
38
|
+
#t.rcov_opts = ['--exclude', "kernel,load-diff-lcs\.rb,instance_exec\.rb,lib/spec.rb,lib/spec/runner.rb,^spec/*,bin/spec,examples,/gems,/Library/Ruby,\.autotest,#{ENV['GEM_HOME']}"]
|
39
|
+
end
|
37
40
|
desc "Run ActiveRecord integration specs"
|
38
41
|
Spec::Rake::SpecTask.new(:active_record) do |t|
|
39
42
|
t.spec_files = FileList['spec/active_record/*_spec.rb']
|
40
43
|
t.libs << 'lib' << 'spec/active_record'
|
44
|
+
t.spec_opts = ['--options', 'spec/spec.opts']
|
45
|
+
t.rcov = false
|
46
|
+
end
|
47
|
+
Spec::Rake::SpecTask.new(:forms) do |t|
|
48
|
+
t.spec_files = FileList['spec/rails/spec/integrations/*_spec.rb']
|
49
|
+
t.libs << 'lib' << 'spec/rails/spec'
|
50
|
+
t.spec_opts = ['--options', 'spec/spec.opts']
|
41
51
|
t.rcov = false
|
42
52
|
end
|
53
|
+
# Spec::Rake::SpecTask.new(:associations) do |t|
|
54
|
+
# t.spec_files = FileList['spec/active_record/associations_spec.rb']
|
55
|
+
# t.libs << 'lib' << 'spec/active_record'
|
56
|
+
# t.rcov = false
|
57
|
+
# end
|
43
58
|
desc "Run all specs"
|
44
|
-
task :all=>[:
|
59
|
+
task :all=>[:object, :active_record, :forms]
|
45
60
|
end
|
46
61
|
|
47
62
|
|
@@ -63,7 +78,7 @@ end
|
|
63
78
|
|
64
79
|
Rake::GemPackageTask.new(spec) do |p|
|
65
80
|
p.gem_spec = spec
|
66
|
-
p.need_tar = true
|
81
|
+
p.need_tar = RUBY_PLATFORM =~ /mswin/ ? false : true
|
67
82
|
p.need_zip = true
|
68
83
|
end
|
69
84
|
|
data/lib/enumerated_attribute.rb
CHANGED
@@ -1,5 +1,8 @@
|
|
1
|
+
require 'enumerated_attribute/attribute/attribute_descriptor'
|
1
2
|
require 'enumerated_attribute/method_definition_dsl'
|
2
3
|
require 'enumerated_attribute/integrations'
|
4
|
+
require 'enumerated_attribute/rails_helpers'
|
5
|
+
|
3
6
|
|
4
7
|
module EnumeratedAttribute
|
5
8
|
|
@@ -24,7 +27,7 @@ module EnumeratedAttribute
|
|
24
27
|
|
25
28
|
initial_value = nil
|
26
29
|
plural_name = opts[:plural] || opts[:enums_accessor] || opts[:enums] || begin
|
27
|
-
|
30
|
+
case
|
28
31
|
when attr_name =~ /[aeiou]y$/
|
29
32
|
"#{attr_name}s"
|
30
33
|
when attr_name =~ /y$/
|
@@ -38,15 +41,13 @@ module EnumeratedAttribute
|
|
38
41
|
incrementor = opts[:incrementor] || opts[:inc] || "#{attr_name}_next"
|
39
42
|
decrementor = opts[:decrementor] || opts[:dec] || "#{attr_name}_previous"
|
40
43
|
|
41
|
-
enums = enums.map{|v| (v =~ /^\^/ ? (initial_value
|
42
|
-
|
44
|
+
enums = enums.map{|v| (v =~ /^\^/ ? (initial_value ||= v[1, v.length-1].to_sym) : v.to_sym )}
|
45
|
+
|
43
46
|
class_eval <<-ATTRIB
|
44
|
-
@@
|
45
|
-
@@
|
46
|
-
@@
|
47
|
-
@@
|
48
|
-
@@enumerated_attribute_values ||= {}
|
49
|
-
@@enumerated_attribute_values[:#{attr_name}] = [:#{enums.join(',:')}]
|
47
|
+
def self.enumerated_attributes; @@enumerated_attributes; end
|
48
|
+
def enums(attr); @@enumerated_attributes[attr.to_sym]; end
|
49
|
+
@@enumerated_attributes ||= {}
|
50
|
+
@@enumerated_attributes[:#{attr_name}] = AttributeDescriptor.new(:#{attr_name}, #{enums.inspect}, #{opts.inspect})
|
50
51
|
ATTRIB
|
51
52
|
|
52
53
|
#define_enumerated_attribute_[writer, reader] may be modified in a named Integrations module (see Integrations::ActiveRecord)
|
@@ -60,16 +61,17 @@ module EnumeratedAttribute
|
|
60
61
|
include(@integration_map[:module]) if @integration_map[:module]
|
61
62
|
|
62
63
|
def self.has_enumerated_attribute?(name)
|
63
|
-
|
64
|
+
return(false) if name.nil?
|
65
|
+
@@enumerated_attributes.key?(name.to_sym)
|
64
66
|
end
|
65
67
|
def self.enumerated_attribute_allows_nil?(name)
|
66
|
-
return
|
67
|
-
|
68
|
+
return(false) unless (descriptor = @@enumerated_attributes[name.to_sym])
|
69
|
+
descriptor.allows_nil?
|
68
70
|
end
|
69
71
|
def self.enumerated_attribute_allows_value?(name, value)
|
70
|
-
return (false) unless @@
|
71
|
-
return
|
72
|
-
|
72
|
+
return (false) unless (descriptor = @@enumerated_attributes[name.to_sym])
|
73
|
+
return descriptor.allows_nil? if (value == nil || value == '')
|
74
|
+
descriptor.allows_value?(value)
|
73
75
|
end
|
74
76
|
end
|
75
77
|
MAP
|
@@ -114,7 +116,7 @@ module EnumeratedAttribute
|
|
114
116
|
middle = meth_name.chop #remove the ?
|
115
117
|
|
116
118
|
attr = nil
|
117
|
-
@@
|
119
|
+
@@enumerated_attributes.keys.each do |name|
|
118
120
|
if middle.sub!(Regexp.new("^"+name.to_s), "")
|
119
121
|
attr = name; break
|
120
122
|
end
|
@@ -122,16 +124,16 @@ module EnumeratedAttribute
|
|
122
124
|
|
123
125
|
value = nil
|
124
126
|
attr_sym = attr ? attr.to_sym : nil
|
125
|
-
if (
|
126
|
-
|
127
|
+
if (descriptor = @@enumerated_attributes[attr_sym])
|
128
|
+
descriptor.enums.each do |v|
|
127
129
|
if middle.sub!(Regexp.new(v.to_s+"$"), "")
|
128
130
|
value = v; break
|
129
131
|
end
|
130
132
|
end
|
131
133
|
else
|
132
134
|
#search through enum values one at time and identify any ambiguities
|
133
|
-
@@
|
134
|
-
enums.each do|v|
|
135
|
+
@@enumerated_attributes.each do |attr_key,descriptor|
|
136
|
+
descriptor.enums.each do|v|
|
135
137
|
if middle.match(v.to_s+"$")
|
136
138
|
raise(AmbiguousMethod, meth_name+" is ambiguous, use something like "+attr_sym.to_s+(middle[0,1]=='_'? '' : '_')+middle+"? or "+attr_key.to_s+(middle[0,1]=='_'? '' : '_')+middle+"?", caller) if attr_sym
|
137
139
|
attr_sym = attr_key
|
@@ -170,7 +172,7 @@ module EnumeratedAttribute
|
|
170
172
|
#create state and action methods from block
|
171
173
|
initial_value = opts[:init] || initial_value
|
172
174
|
if block_given?
|
173
|
-
m = EnumeratedAttribute::MethodDefinitionDSL.new(self, attr_name, enums)
|
175
|
+
m = EnumeratedAttribute::MethodDefinitionDSL.new(self, enumerated_attributes[attr_sym]) #attr_name, enums)
|
174
176
|
m.instance_eval(&block)
|
175
177
|
initial_value = m.initial_value || initial_value
|
176
178
|
plural_name = m.pluralized_name || plural_name
|
@@ -181,16 +183,16 @@ module EnumeratedAttribute
|
|
181
183
|
#define the enum values accessor
|
182
184
|
class_eval <<-ENUM
|
183
185
|
def #{plural_name}
|
184
|
-
@@
|
186
|
+
@@enumerated_attributes[:#{attr_name}]
|
185
187
|
end
|
186
188
|
def #{incrementor}
|
187
|
-
z = @@
|
188
|
-
index = z.index(
|
189
|
+
z = @@enumerated_attributes[:#{attr_name}].enums
|
190
|
+
index = z.index(read_enumerated_attribute(:#{attr_name}))
|
189
191
|
write_enumerated_attribute(:#{attr_name}, z[index >= z.size-1 ? 0 : index+1])
|
190
192
|
end
|
191
193
|
def #{decrementor}
|
192
|
-
z = @@
|
193
|
-
index = z.index(
|
194
|
+
z = @@enumerated_attributes[:#{attr_name}].enums
|
195
|
+
index = z.index(read_enumerated_attribute(:#{attr_name}))
|
194
196
|
write_enumerated_attribute(:#{attr_name}, z[index > 0 ? index-1 : z.size-1])
|
195
197
|
end
|
196
198
|
ENUM
|