enumerated_attribute 0.2.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (107) hide show
  1. data/.gitignore +16 -0
  2. data/CHANGELOG.rdoc +49 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +555 -0
  5. data/Rakefile +104 -0
  6. data/init.rb +1 -0
  7. data/lib/enumerated_attribute.rb +24 -0
  8. data/lib/enumerated_attribute/attribute.rb +78 -0
  9. data/lib/enumerated_attribute/attribute/arguments.rb +48 -0
  10. data/lib/enumerated_attribute/attribute/attribute_descriptor.rb +51 -0
  11. data/lib/enumerated_attribute/attribute/class_methods.rb +42 -0
  12. data/lib/enumerated_attribute/attribute/instance_methods.rb +96 -0
  13. data/lib/enumerated_attribute/integrations.rb +33 -0
  14. data/lib/enumerated_attribute/integrations/active_record.rb +109 -0
  15. data/lib/enumerated_attribute/integrations/datamapper.rb +6 -0
  16. data/lib/enumerated_attribute/integrations/default.rb +25 -0
  17. data/lib/enumerated_attribute/integrations/object.rb +47 -0
  18. data/lib/enumerated_attribute/method_definition_dsl.rb +142 -0
  19. data/lib/enumerated_attribute/rails_helpers.rb +97 -0
  20. data/lib/jeffp-enumerated_attribute.rb +1 -0
  21. data/spec/active_record/active_record_spec.rb +363 -0
  22. data/spec/active_record/association_test_classes.rb +40 -0
  23. data/spec/active_record/associations_spec.rb +130 -0
  24. data/spec/active_record/cfg.rb +6 -0
  25. data/spec/active_record/inheritance_classes.rb +19 -0
  26. data/spec/active_record/inheritance_spec.rb +60 -0
  27. data/spec/active_record/race_car.rb +15 -0
  28. data/spec/active_record/single_table_inheritance_spec.rb +0 -0
  29. data/spec/active_record/sti_classes.rb +10 -0
  30. data/spec/active_record/sti_spec.rb +41 -0
  31. data/spec/active_record/test_in_memory.rb +71 -0
  32. data/spec/car.rb +67 -0
  33. data/spec/cfg.rb +8 -0
  34. data/spec/inheritance_classes.rb +16 -0
  35. data/spec/inheritance_spec.rb +80 -0
  36. data/spec/new_and_method_missing_spec.rb +73 -0
  37. data/spec/plural.rb +8 -0
  38. data/spec/poro_spec.rb +397 -0
  39. data/spec/rails/README +243 -0
  40. data/spec/rails/Rakefile +10 -0
  41. data/spec/rails/app/controllers/application_controller.rb +10 -0
  42. data/spec/rails/app/controllers/form_test_controller.rb +38 -0
  43. data/spec/rails/app/helpers/application_helper.rb +3 -0
  44. data/spec/rails/app/helpers/form_test_helper.rb +2 -0
  45. data/spec/rails/app/models/user.rb +9 -0
  46. data/spec/rails/app/views/form_test/form.html.erb +1 -0
  47. data/spec/rails/app/views/form_test/form_for.html.erb +10 -0
  48. data/spec/rails/app/views/form_test/form_tag.html.erb +9 -0
  49. data/spec/rails/app/views/form_test/index.html.erb +6 -0
  50. data/spec/rails/app/views/layouts/application.html.erb +11 -0
  51. data/spec/rails/config/boot.rb +110 -0
  52. data/spec/rails/config/database.yml +22 -0
  53. data/spec/rails/config/environment.rb +45 -0
  54. data/spec/rails/config/environments/development.rb +17 -0
  55. data/spec/rails/config/environments/production.rb +28 -0
  56. data/spec/rails/config/environments/test.rb +28 -0
  57. data/spec/rails/config/initializers/backtrace_silencers.rb +7 -0
  58. data/spec/rails/config/initializers/inflections.rb +10 -0
  59. data/spec/rails/config/initializers/mime_types.rb +5 -0
  60. data/spec/rails/config/initializers/new_rails_defaults.rb +19 -0
  61. data/spec/rails/config/initializers/session_store.rb +15 -0
  62. data/spec/rails/config/locales/en.yml +5 -0
  63. data/spec/rails/config/routes.rb +43 -0
  64. data/spec/rails/db/development.sqlite3 +0 -0
  65. data/spec/rails/db/migrate/20090804230221_create_sessions.rb +16 -0
  66. data/spec/rails/db/migrate/20090804230546_create_users.rb +21 -0
  67. data/spec/rails/db/schema.rb +35 -0
  68. data/spec/rails/db/test.sqlite3 +0 -0
  69. data/spec/rails/public/404.html +30 -0
  70. data/spec/rails/public/422.html +30 -0
  71. data/spec/rails/public/500.html +30 -0
  72. data/spec/rails/public/favicon.ico +0 -0
  73. data/spec/rails/public/images/rails.png +0 -0
  74. data/spec/rails/public/index.html +275 -0
  75. data/spec/rails/public/javascripts/application.js +2 -0
  76. data/spec/rails/public/javascripts/controls.js +963 -0
  77. data/spec/rails/public/javascripts/dragdrop.js +973 -0
  78. data/spec/rails/public/javascripts/effects.js +1128 -0
  79. data/spec/rails/public/javascripts/prototype.js +4320 -0
  80. data/spec/rails/public/robots.txt +5 -0
  81. data/spec/rails/public/stylesheets/scaffold.css +54 -0
  82. data/spec/rails/script/about +4 -0
  83. data/spec/rails/script/autospec +6 -0
  84. data/spec/rails/script/console +3 -0
  85. data/spec/rails/script/dbconsole +3 -0
  86. data/spec/rails/script/destroy +3 -0
  87. data/spec/rails/script/generate +3 -0
  88. data/spec/rails/script/performance/benchmarker +3 -0
  89. data/spec/rails/script/performance/profiler +3 -0
  90. data/spec/rails/script/plugin +3 -0
  91. data/spec/rails/script/runner +3 -0
  92. data/spec/rails/script/server +3 -0
  93. data/spec/rails/script/spec +10 -0
  94. data/spec/rails/script/spec_server +9 -0
  95. data/spec/rails/spec/controllers/form_test_controller_spec.rb +41 -0
  96. data/spec/rails/spec/integrations/enum_select_spec.rb +75 -0
  97. data/spec/rails/spec/matchers.rb +12 -0
  98. data/spec/rails/spec/rcov.opts +2 -0
  99. data/spec/rails/spec/spec.opts +4 -0
  100. data/spec/rails/spec/spec_helper.rb +40 -0
  101. data/spec/rails/spec/views/form_test/form.html.erb_spec.rb +12 -0
  102. data/spec/rails/spec/views/form_test/form_for.html.erb_spec.rb +12 -0
  103. data/spec/rails/spec/views/form_test/form_tag.html.erb_spec.rb +12 -0
  104. data/spec/rcov.opts +2 -0
  105. data/spec/spec.opts +4 -0
  106. data/spec/tractor.rb +48 -0
  107. metadata +182 -0
@@ -0,0 +1,6 @@
1
+ #irb -r cfg ==> to work configure for working in IRB
2
+ $:.unshift '../../lib'
3
+ require 'test_in_memory'
4
+ require 'enumerated_attribute'
5
+ require 'race_car'
6
+ require 'sti_classes'
@@ -0,0 +1,19 @@
1
+ require 'race_car'
2
+
3
+ class SubRaceCar < RaceCar
4
+ set_table_name "race_cars"
5
+
6
+ enum_attr :extra, %w(^one two three four)
7
+ end
8
+
9
+ class SubRaceCar2 < RaceCar
10
+ set_table_name "race_cars"
11
+
12
+ end
13
+
14
+ class SubRaceCar3 < RaceCar
15
+ set_table_name "race_cars"
16
+
17
+ enum_attr :gear, %w(reverse neutral ^first second third over_drive)
18
+ end
19
+
@@ -0,0 +1,60 @@
1
+ require 'test_in_memory'
2
+ require 'enumerated_attribute'
3
+ require 'active_record'
4
+ require 'active_record/inheritance_classes'
5
+
6
+ def one_to_four; [:one, :two, :three, :four]; end
7
+ def gear1; [:reverse, :neutral, :first, :second, :over_drive]; end
8
+ def gear2; [:reverse, :neutral, :first, :second, :third, :over_drive]; end
9
+ def choke; [:none, :medium, :full]; end
10
+
11
+ describe "SubRaceCar" do
12
+ it "should have :gear init to :neutral" do
13
+ o=SubRaceCar.new
14
+ o.gear.should == :neutral
15
+ o.enums(:gear).should == gear1
16
+ o.gear_previous.should == :reverse
17
+ end
18
+ it "should have :choke init to :none" do
19
+ o=SubRaceCar.new
20
+ o.choke.should == :none
21
+ o.enums(:choke).should == choke
22
+ o.choke_previous.should == :full
23
+ end
24
+ it "should have :extra init to :one" do
25
+ o=SubRaceCar.new
26
+ o.extra.should == :one
27
+ o.enums(:extra).should == one_to_four
28
+ o.extra_previous.should == :four
29
+ end
30
+ end
31
+
32
+ describe "SubRaceCar2" do
33
+ it "should have :gear init to :neutral" do
34
+ o=SubRaceCar2.new
35
+ o.gear.should == :neutral
36
+ o.enums(:gear).should == gear1
37
+ o.gear_previous.should == :reverse
38
+ end
39
+ it "should have :choke init to :none" do
40
+ o=SubRaceCar2.new
41
+ o.choke.should == :none
42
+ o.enums(:choke).should == choke
43
+ o.choke_previous.should == :full
44
+ end
45
+ end
46
+
47
+ describe "SubRaceCar3" do
48
+ it "should have overridden :gear init to :first" do
49
+ o=SubRaceCar3.new
50
+ o.gear.should == :first
51
+ o.enums(:gear).should == gear2
52
+ o.gear_previous.should == :neutral
53
+ end
54
+ it "should have :choke init to :none" do
55
+ o=SubRaceCar3.new
56
+ o.choke.should == :none
57
+ o.enums(:choke).should == choke
58
+ o.choke_previous.should == :full
59
+ end
60
+ end
@@ -0,0 +1,15 @@
1
+
2
+ class RaceCar < ActiveRecord::Base
3
+ enum_attr :gear, %w(reverse ^neutral first second over_drive)
4
+ enum_attr :choke, %w(^none medium full)
5
+ end
6
+
7
+ #gear = enumerated column attribute
8
+ #choke = enumerated non-column attribute
9
+ #lights = non-enumerated column attribute
10
+
11
+ =begin
12
+ t.string :name
13
+ t.string :gear
14
+ t.string :lights
15
+ =end
@@ -0,0 +1,10 @@
1
+
2
+ class StiParent < ActiveRecord::Base
3
+ enum_attr :parent_enum, %w(p1 p2 p3)
4
+
5
+ end
6
+
7
+ class StiSub < StiParent
8
+ enum_attr :sub_enum, %w(s1 s2 s3)
9
+ end
10
+
@@ -0,0 +1,41 @@
1
+ require 'test_in_memory'
2
+ require 'enumerated_attribute'
3
+ require 'active_record'
4
+ require 'active_record/sti_classes'
5
+
6
+
7
+ #contributed by Rob Chekaluk
8
+
9
+ describe "enum_attr for STI subclass" do
10
+ it "should assign, save and retrieve non-enumerated attribute" do
11
+ s = StiSub.new()
12
+ s.sub_nonenum = "value2"
13
+ s.save
14
+ s0 = StiSub.find(s.id)
15
+ s0.sub_nonenum.should == "value2"
16
+ end
17
+ it "should assign, save and retrieve subclass enum" do
18
+ s = StiSub.new()
19
+ s.sub_enum = :s3
20
+ s.sub_enum.should == :s3
21
+ s.save
22
+ s0 = StiSub.find(s.id)
23
+ s0.sub_enum.should == :s3
24
+ end
25
+ it "should assign, save and retrieve inherited enum" do
26
+ s = StiSub.new()
27
+ s.parent_enum = :p2
28
+ s.parent_enum.should == :p2
29
+ s.save
30
+ s0 = StiSub.find(s.id)
31
+ s0.parent_enum.should == :p2
32
+ end
33
+ it "should use predicate methods to access enumerated attributes" do
34
+ s=StiSub.create(:parent_enum=>:p2, :sub_enum=>:s1)
35
+ s.parent_enum_is_p2?.should be_true
36
+ s.parent_enum_is_not_p2?.should be_false
37
+ s.sub_enum_is_s2?.should be_false
38
+ s.sub_enum_is_not_s2?.should be_true
39
+ end
40
+ end
41
+
@@ -0,0 +1,71 @@
1
+ require 'rubygems'
2
+
3
+ gem 'activerecord', ENV['AR_VERSION'] ? "=#{ENV['AR_VERSION']}" : '>=2.1.0'
4
+ require 'active_record'
5
+ require 'active_support/core_ext/logger' rescue nil # rails3
6
+
7
+ require 'enumerated_attribute'
8
+
9
+ ActiveRecord::Base.establish_connection({'adapter' => 'sqlite3', 'database' => ':memory:'})
10
+ ActiveRecord::Base.logger = Logger.new("#{File.dirname(__FILE__)}/active_record.log")
11
+
12
+ connection = ActiveRecord::Base.connection
13
+ connection.create_table(:race_cars, :force=>true) do |t|
14
+ t.string :name
15
+ t.enum :gear
16
+ t.enum :lights
17
+ t.timestamps
18
+ end
19
+ connection.create_table(:bicycles, :force=>true) do |t|
20
+ t.string :name
21
+ t.enum :speed
22
+ t.enum :gear
23
+ end
24
+
25
+ #basic_associations
26
+ connection.create_table(:companies, :force=>true) do |t|
27
+ t.string :name
28
+ t.string :status
29
+ end
30
+ connection.create_table(:contract_workers, :force=>true) do |t|
31
+ t.references :company
32
+ t.references :contractor
33
+ t.string :status
34
+ end
35
+ connection.create_table(:licenses, :force=>true) do |t|
36
+ t.references :company
37
+ t.string :status
38
+ end
39
+ connection.create_table(:contractors, :force=>true) do |t|
40
+ t.string :name
41
+ t.string :status
42
+ end
43
+ connection.create_table(:employees, :force=>true) do |t|
44
+ t.references :company
45
+ t.string :name
46
+ t.string :status
47
+ end
48
+
49
+ #polymorphic_associations
50
+ connection.create_table(:comments, :force=>true) do |t|
51
+ t.references :document, :polymorphic=>true
52
+ t.text :comment
53
+ t.string :status
54
+ end
55
+ connection.create_table(:articles, :force=>true) do |t|
56
+ t.string :name
57
+ t.string :status
58
+ end
59
+ connection.create_table(:images, :force=>true) do |t|
60
+ t.string :name
61
+ t.string :status
62
+ end
63
+
64
+ #single table inheritance
65
+ connection.create_table(:sti_parents, :force=>true) do |t|
66
+ t.string :type
67
+ t.enum :parent_enum
68
+ t.string :sub_nonenum
69
+ t.enum :sub_enum
70
+ t.timestamps
71
+ end
data/spec/car.rb ADDED
@@ -0,0 +1,67 @@
1
+ require 'enumerated_attribute'
2
+
3
+ #used to test that method_missing chaining plays nice in inheritance situations
4
+
5
+ class Vehicle
6
+ def vehicle_method_missing_called?; @vehicle_method_missing_called; end
7
+ def self.vehicle_new_called?; @@vehicle_new_called; end
8
+ def self.reset; @@vehicle_new_called = false; end
9
+
10
+ @@vehicle_new_called = false
11
+ def initialize
12
+ @vehicle_method_missing_called = false
13
+ super
14
+ end
15
+
16
+ def method_missing(methId, *args)
17
+ @vehicle_method_missing_called = true
18
+ #end here
19
+ end
20
+
21
+ def self.new
22
+ @@vehicle_new_called = true
23
+ super
24
+ end
25
+
26
+ end
27
+
28
+ class CarWithMethods < Vehicle
29
+ def car_method_missing_called?; @car_method_missing_called; end
30
+ def self.car_new_called?; @@car_new_called; end
31
+ def self.reset; @@car_new_called = false; super; end
32
+
33
+ def self.new
34
+ @@car_new_called = true
35
+ super
36
+ end
37
+
38
+ def method_missing(methId, *args)
39
+ @car_method_missing_called = true
40
+ super
41
+ end
42
+
43
+ enum_attr :gear, %w(reverse ^neutral drive)
44
+
45
+ @@car_new_called = false
46
+ def initialize
47
+ @car_method_missing_called = false
48
+ super
49
+ end
50
+
51
+ end
52
+
53
+ class CarWithoutMethods < Vehicle
54
+ def car_method_missing_called?; @car_method_missing_called; end
55
+ def self.car_new_called?; @@car_new_called; end
56
+ def self.reset; @@car_new_called = false; super; end
57
+
58
+ enum_attr :gear, %w(reverse ^neutral drive)
59
+
60
+ @@car_new_called = false
61
+ def initialize
62
+ @car_method_missing_called = false
63
+ super
64
+ end
65
+
66
+ end
67
+
data/spec/cfg.rb ADDED
@@ -0,0 +1,8 @@
1
+ #irb -r cfg ==> to work configure for working in IRB
2
+ $:.unshift '../lib'
3
+ require 'enumerated_attribute'
4
+ require 'tractor'
5
+ require 'car'
6
+ require 'plural'
7
+ require 'inheritance_classes'
8
+
@@ -0,0 +1,16 @@
1
+ require 'enumerated_attribute'
2
+
3
+ class Base
4
+ enum_attr :base1, %w(^one two three four)
5
+ enum_attr :inherited1, %w(^one two three four)
6
+ enum_attr :inherited2, %w(^one two three four)
7
+ end
8
+
9
+ class Sub < Base
10
+ enum_attr :sub1, %w(one ^two three four)
11
+ enum_attr :inherited1, %w(one ^two three four)
12
+ enum_attr :inherited2, %w(^five six seven eight)
13
+ end
14
+
15
+ class Sub2 < Base
16
+ end
@@ -0,0 +1,80 @@
1
+ require 'inheritance_classes'
2
+
3
+ def one_to_four; [:one, :two, :three, :four]; end
4
+ def five_to_eight; [:five, :six, :seven, :eight]; end
5
+
6
+ describe "Base" do
7
+ it "should have :base1 init to :one" do
8
+ o=Base.new
9
+ o.base1.should == :one
10
+ o.enums(:base1).should == one_to_four
11
+ o.base1_previous.should == :four
12
+ end
13
+ it "should have :inherited1 init to :one" do
14
+ o=Base.new
15
+ o.inherited1.should == :one
16
+ o.enums(:inherited1).should == one_to_four
17
+ o.inherited1_previous.should == :four
18
+ end
19
+ it "should have :inherited2 init to :one" do
20
+ o=Base.new
21
+ o.inherited2.should == :one
22
+ o.enums(:inherited2).should == one_to_four
23
+ o.inherited2_previous.should == :four
24
+ end
25
+ end
26
+
27
+ describe "Sub<Base" do
28
+ it "should instantiate an object" do
29
+ lambda { s=Sub.new }.should_not raise_error
30
+ end
31
+ it "should have :sub1 init to :two" do
32
+ o=Sub.new
33
+ o.sub1.should == :two
34
+ o.enums(:sub1).should == one_to_four
35
+ o.sub1_previous.should == :one
36
+ end
37
+ it "should have :base1 init to :one" do
38
+ o=Sub.new
39
+ o.base1.should == :one
40
+ o.enums(:base1).should == one_to_four
41
+ o.base1_previous.should == :four
42
+ end
43
+ it "should have :inherited1 init to :two" do
44
+ o=Sub.new
45
+ o.inherited1.should == :two
46
+ o.enums(:inherited1).should == one_to_four
47
+ o.inherited1_previous.should == :one
48
+ end
49
+ it "should have :inherited2 init to :five" do
50
+ o=Sub.new
51
+ o.inherited2.should == :five
52
+ o.enums(:inherited2).should == five_to_eight
53
+ o.inherited2_previous.should == :eight
54
+ end
55
+ end
56
+
57
+ describe "Sub2<Base" do
58
+ it "should instantiate an object" do
59
+ lambda {s=Sub2.new}.should_not raise_error
60
+ end
61
+ it "should have :base1 init to :one" do
62
+ o=Sub2.new
63
+ o.base1.should == :one
64
+ o.enums(:base1).should == one_to_four
65
+ o.base1_previous.should == :four
66
+ end
67
+ it "should have :inherited1 init to :one" do
68
+ o=Sub2.new
69
+ o.inherited1.should == :one
70
+ o.enums(:inherited1).should == one_to_four
71
+ o.inherited1_previous.should == :four
72
+ end
73
+ it "should have :inherited2 init to :one" do
74
+ o=Sub2.new
75
+ o.inherited2.should == :one
76
+ o.enums(:inherited2).should == one_to_four
77
+ o.inherited2_previous.should == :four
78
+ end
79
+ end
80
+
@@ -0,0 +1,73 @@
1
+ require 'car'
2
+
3
+ #used to test that method_missing chaining plays nice in inheritance situation
4
+
5
+ describe "CarWithMethods" do
6
+
7
+ it "should initialize Car and Vehicle _method_missing_called? to false" do
8
+ c= CarWithMethods.new
9
+ c.car_method_missing_called?.should be_false
10
+ c.vehicle_method_missing_called?.should be_false
11
+ end
12
+
13
+ it "should initialize Car and Vehicle _new_called? to false" do
14
+ CarWithMethods.reset
15
+ CarWithMethods.car_new_called?.should be_false
16
+ CarWithMethods.vehicle_new_called?.should be_false
17
+ end
18
+
19
+ it "should initialize :gear to :neutral" do
20
+ c = CarWithMethods.new
21
+ c.gear.should == :neutral
22
+ end
23
+
24
+ it "should hit both new methods for Car and Vehicle on instantiation" do
25
+ CarWithMethods.reset
26
+ CarWithMethods.new
27
+ CarWithMethods.car_new_called?.should be_true
28
+ CarWithMethods.vehicle_new_called?.should be_true
29
+ end
30
+
31
+ it "should not hit method_missing when calling dynamic predicate method :gear_is_in_reverse?" do
32
+ c = CarWithMethods.new
33
+ c.gear_is_in_reverse?
34
+ c.car_method_missing_called?.should be_false
35
+ c.vehicle_method_missing_called?.should be_false
36
+ end
37
+
38
+ it "should hit Car and Vehicle method_missing when calling unsupported dynamic predicate method" do
39
+ c = CarWithMethods.new
40
+ c.parking_break_is_on?
41
+ c.car_method_missing_called?.should be_true
42
+ c.vehicle_method_missing_called?.should be_true
43
+ end
44
+ end
45
+
46
+ describe "CarWithoutMethods" do
47
+
48
+ it "should set Car and Vehicle _new_called? to false when calling :reset" do
49
+ CarWithoutMethods.reset
50
+ CarWithoutMethods.car_new_called?.should be_false
51
+ CarWithoutMethods.vehicle_new_called?.should be_false
52
+ end
53
+
54
+ it "should hit only Vehicle _new_called? on instantiation" do
55
+ CarWithoutMethods.reset
56
+ CarWithoutMethods.new
57
+ CarWithoutMethods.car_new_called?.should be_false
58
+ CarWithoutMethods.vehicle_new_called?.should be_true
59
+ end
60
+
61
+ it "should not hit Vehicle method_missing when calling dynamic predicate method :gear_is_in_first?" do
62
+ c = CarWithoutMethods.new
63
+ c.gear_is_in_drive?
64
+ c.vehicle_method_missing_called?.should be_false
65
+ end
66
+
67
+ it "should hit Vehicle method_missing when calling unsupported predicate method" do
68
+ c = CarWithoutMethods.new
69
+ c.parking_break2_is_on?
70
+ c.vehicle_method_missing_called?.should be_true
71
+ end
72
+
73
+ end