edave-enumerated_attribute 0.2.18

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 +15 -0
  2. data/CHANGELOG.rdoc +49 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +558 -0
  5. data/Rakefile +30 -0
  6. data/init.rb +1 -0
  7. data/lib/enumerated_attribute.rb +24 -0
  8. data/lib/enumerated_attribute/attribute.rb +79 -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 +45 -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 +114 -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 +99 -0
  20. data/spec/active_record/active_record_spec.rb +384 -0
  21. data/spec/active_record/association_test_classes.rb +40 -0
  22. data/spec/active_record/associations_spec.rb +130 -0
  23. data/spec/active_record/cfg.rb +6 -0
  24. data/spec/active_record/df.rb +12 -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 +420 -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 +190 -0
@@ -0,0 +1,6 @@
1
+ module EnumerateAttribute
2
+ module Integrations
3
+ module Datamapper
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,25 @@
1
+ module EnumeratedAttribute
2
+ module Integrations
3
+
4
+ module Default
5
+ def self.included(klass); klass.extend(ClassMethods); end
6
+
7
+ module ClassMethods
8
+ def define_enumerated_attribute_writer_method(name)
9
+ method_name = "#{name}=".to_sym
10
+ class_eval do
11
+ define_method(method_name) {|val| write_enumerated_attribute(name.to_sym, val) }
12
+ end
13
+ end
14
+
15
+ def define_enumerated_attribute_reader_method(name)
16
+ name = name.to_sym
17
+ class_eval do
18
+ define_method(name) { read_enumerated_attribute(name) }
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,47 @@
1
+ module EnumeratedAttribute
2
+ module Integrations
3
+
4
+ module Object
5
+ def self.included(klass)
6
+ klass.extend(ClassMethods)
7
+ end
8
+
9
+ def write_enumerated_attribute(name, val)
10
+ name = name.to_s
11
+ val = nil if val == ''
12
+ val = val.to_sym if val
13
+ unless self.class.enumerated_attribute_allows_value?(name, val)
14
+ raise(InvalidEnumeration, "nil is not allowed on '#{name}' attribute, set :nil=>true option", caller) unless val
15
+ raise(InvalidEnumeration, ":#{val} is not a defined enumeration value for the '#{name}' attribute", caller)
16
+ end
17
+ instance_variable_set('@'+name, val)
18
+ end
19
+
20
+ def read_enumerated_attribute(name)
21
+ instance_variable_get('@'+name.to_s)
22
+ end
23
+
24
+ module ClassMethods
25
+ private
26
+
27
+ def define_enumerated_attribute_new_method
28
+ class_eval do
29
+ class << self
30
+ unless method_defined?(:new_without_enumerated_attribute)
31
+ alias_method :new_without_enumerated_attribute, :new
32
+ def new(*args, &block)
33
+ result = new_without_enumerated_attribute(*args)
34
+ result.initialize_enumerated_attributes
35
+ yield result if block_given?
36
+ result
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ end
44
+
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,142 @@
1
+ module EnumeratedAttribute
2
+
3
+ class MethodDefinition
4
+ attr_accessor :method_name, :negated, :argument
5
+
6
+ def initialize(name, arg, negated=false)
7
+ @method_name, @negated, @argument = name, negated, arg
8
+ end
9
+
10
+ def is_predicate_method?; @method_name[-1, 1] == '?'; end
11
+ def has_method_name?; !!@method_name; end
12
+ def has_argument?; !!@argument; end
13
+ end
14
+
15
+ class MethodDefinitionDSL
16
+ attr_reader :initial_value, :pluralized_name, :decrementor_name, :incrementor_name
17
+
18
+ def initialize(class_obj, descriptor)
19
+ @class_obj = class_obj
20
+ @attr_name = descriptor.name
21
+ @attr_descriptor = descriptor #this is the enums array
22
+ end
23
+
24
+ #we'll by pass this - they can use it if it helps make code more readable - not enforced - should it be??
25
+ def define
26
+ end
27
+
28
+ def is_not(*args)
29
+ arg = args.first if args.length > 0
30
+ MethodDefinition.new(nil, arg, true)
31
+ end
32
+ alias :isnt :is_not
33
+
34
+ def is(*args)
35
+ arg = args.first if args.length > 0
36
+ MethodDefinition.new(nil, arg)
37
+ end
38
+
39
+ def method_missing(methId, *args, &block)
40
+ meth_name = methId.id2name
41
+
42
+ meth_def = nil
43
+ if args.size > 0
44
+ arg = args.first
45
+ if arg.instance_of?(EnumeratedAttribute::MethodDefinition)
46
+ if arg.has_method_name?
47
+ raise_method_syntax_error(meth_name, arg.method_name)
48
+ end
49
+ meth_def = arg
50
+ meth_def.method_name = meth_name
51
+ else
52
+ meth_def = MethodDefinition.new(meth_name, arg)
53
+ end
54
+ elsif block_given?
55
+ meth_def = MethodDefinition.new(meth_name, block)
56
+ else
57
+ raise_method_syntax_error(meth_name)
58
+ end
59
+ evaluate_method_definition(meth_def)
60
+ end
61
+
62
+ def init(value)
63
+ if (!@attr_descriptor.empty? && !@attr_descriptor.include?(value.to_sym))
64
+ raise(InvalidDefinition, "'#{value}' in method 'init' is not an enumeration value for :#{@attr_name} attribute", caller)
65
+ end
66
+ @initial_value = value
67
+ end
68
+
69
+ def decrementor(value); @decrementor_name = value; end
70
+ def incrementor(value); @incrementor_name = value; end
71
+ def enums_accessor(value); @pluralized_name = value; end
72
+ alias_method :inc, :incrementor
73
+ alias_method :dec, :decrementor
74
+ alias_method :enums, :enums_accessor
75
+ alias_method :plural, :enums_accessor
76
+
77
+ def label(hash)
78
+ raise(InvalidDefinition, "label or labels keyword should be followed by a hash of :enum_value=>'label'", caller) unless hash.is_a?(Hash)
79
+ hash.each do |k,v|
80
+ raise(InvalidDefinition, "#{k} is not an enumeration value for :#{@attr_name} attribute", caller) unless (k.is_a?(Symbol) && @attr_descriptor.include?(k))
81
+ raise(InvalidDefinition, "#{v} is not a string. Labels should be strings", caller) unless v.is_a?(String)
82
+ @attr_descriptor.set_label(k, v)
83
+ end
84
+ end
85
+ alias_method :labels, :label
86
+
87
+ private
88
+
89
+ def raise_method_syntax_error(meth_name, offending_token=nil)
90
+ suffix = offending_token ? "found '#{offending_token}'" : "found nothing"
91
+ followed_by = (meth_name[-1,1] == '?' ? "is_not, an enumeration value, an array of enumeration values, " : "") + "a proc, lambda or code block"
92
+ raise InvalidDefinition, "'#{meth_name}' should be followed by #{followed_by} -- but #{suffix}"
93
+ end
94
+
95
+ def evaluate_method_definition(mdef)
96
+ unless mdef.has_argument?
97
+ return raise_method_syntax_error(mdef.method_name)
98
+ end
99
+
100
+ if mdef.is_predicate_method?
101
+ case mdef.argument
102
+ when String
103
+ return create_custom_method_for_symbol_or_string(mdef)
104
+ when Symbol
105
+ return create_custom_method_for_symbol_or_string(mdef)
106
+ when Array
107
+ return create_custom_method_for_array_of_enums(mdef)
108
+ when Proc
109
+ return create_custom_method_for_proc(mdef)
110
+ end
111
+ else #action method
112
+ if mdef.argument.instance_of?(Proc)
113
+ return create_custom_method_for_proc(mdef)
114
+ end
115
+ end
116
+ raise_method_syntax_error(mdef.method_name, mdef.argument)
117
+ end
118
+
119
+ def create_custom_method_for_proc(mdef)
120
+ @class_obj.send(:define_method, mdef.method_name, mdef.argument)
121
+ end
122
+
123
+ def create_custom_method_for_symbol_or_string(mdef)
124
+ if (!@attr_descriptor.empty? && !@attr_descriptor.include?(mdef.argument.to_sym))
125
+ raise(InvalidDefinition, "'#{mdef.argument}' in method '#{mdef.method_name}' is not an enumeration value for :#{@attr_name} attribute", caller)
126
+ end
127
+ @class_obj.class_eval("def #{mdef.method_name}; @#{@attr_name} #{mdef.negated ? '!=' : '=='} :#{mdef.argument}; end")
128
+ end
129
+
130
+ def create_custom_method_for_array_of_enums(mdef)
131
+ if !@attr_descriptor.empty?
132
+ mdef.argument.each do |m|
133
+ if !@attr_descriptor.include?(m.to_sym)
134
+ raise(InvalidDefinition, "'#{m}' in method '#{mdef.method_name}' is not an enumeration value for :#{@attr_name} attribute", caller)
135
+ end
136
+ end
137
+ end
138
+ @class_obj.class_eval("def #{mdef.method_name}; #{mdef.negated ? '!' : ''}[:#{mdef.argument.join(',:')}].include?(@#{@attr_name}); end")
139
+ end
140
+ end
141
+
142
+ end
@@ -0,0 +1,99 @@
1
+ require 'active_record/connection_adapters/abstract/schema_definitions'
2
+
3
+ if defined?(ActiveRecord)
4
+ module ActiveRecord
5
+ module ConnectionAdapters
6
+ class TableDefinition
7
+ def column_with_enumerated_attribute(name, type, options = {})
8
+ type = 'string' if type.to_s == 'enum'
9
+ column_without_enumerated_attribute(name, type, options)
10
+ end
11
+ safe_alias_method_chain :column, :enumerated_attribute
12
+
13
+ def enum(*args)
14
+ options = args.extract_options!
15
+ column_names = args
16
+ column_names.each { |name| column(name, 'string', options) }
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ #ARGV is used by generators -- if it contains one of these generator commands - add enumeration support
24
+ #unless ((ARGV || []) & ["scaffold", "rspec_scaffold", "nifty_scaffold"]).empty?
25
+ if ((ARGV || []).any?{|o| o =~ /scaffold/ })
26
+ begin
27
+ require 'rails/generators'
28
+ require 'rails/generators/generated_attribute'
29
+ rescue
30
+ rescue Exception => ex
31
+ end
32
+
33
+ module Rails
34
+ module Generators
35
+ class GeneratedAttribute
36
+ def field_type_with_enumerated_attribute
37
+ return (@field_type = :enum_select) if type == :enum
38
+ field_type_without_enumerated_attribute
39
+ end
40
+ alias_method_chain :field_type, :enumerated_attribute
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ if defined?(ActionView::Base)
47
+ module ActionView
48
+ module Helpers
49
+
50
+ #form_options_helper.rb
51
+ module FormOptionsHelper
52
+ #def select
53
+ def enum_select(object, method, options={}, html_options={})
54
+ InstanceTag.new(object, method, self, options.delete(:object)).to_enum_select_tag(options, html_options)
55
+ end
56
+ end
57
+
58
+ class InstanceTag
59
+ def to_enum_select_tag(options, html_options={})
60
+ choices = []
61
+ if self.object.respond_to?(:enums)
62
+ enums = self.object.enums(method_name.to_sym)
63
+ choices = enums ? enums.select_options : []
64
+ if (value = self.object.__send__(method_name.to_sym))
65
+ options[:selected] ||= value.to_s
66
+ else
67
+ options[:include_blank] = enums.allows_nil? if options[:include_blank].nil?
68
+ end
69
+ end
70
+ to_select_tag(choices, options, html_options)
71
+ end
72
+
73
+ #initialize record_name, method, self
74
+ if respond_to?(:to_tag)
75
+ def to_tag_with_enumerated_attribute(options={})
76
+ #look for an enum
77
+ if (column_type == :string &&
78
+ self.object.class.respond_to?(:has_enumerated_attribute?) &&
79
+ self.object.class.has_enumerated_attribute?(method_name.to_sym))
80
+ to_enum_select_tag(options)
81
+ else
82
+ to_tag_without_enumerated_attribute(options)
83
+ end
84
+ end
85
+ alias_method_chain :to_tag, :enumerated_attribute
86
+ end
87
+ end
88
+
89
+ class FormBuilder
90
+ def enum_select(method, options={}, html_options={})
91
+ @template.enum_select(@object_name, method, objectify_options(options), @default_options.merge(html_options))
92
+ end
93
+ end
94
+
95
+ end
96
+ end
97
+ end
98
+
99
+
@@ -0,0 +1,384 @@
1
+ require 'test_in_memory'
2
+ require 'active_record'
3
+ require 'enumerated_attribute'
4
+ require 'race_car'
5
+
6
+ describe "RaceCar" do
7
+
8
+ it "should have default labels for :gear attribute" do
9
+ labels_hash = {:reverse=>'Reverse', :neutral=>'Neutral', :first=>'First', :second=>'Second', :over_drive=>'Over drive'}
10
+ labels = ['Reverse', 'Neutral', 'First', 'Second', 'Over drive']
11
+ select_options = [['Reverse', 'reverse'], ['Neutral', 'neutral'], ['First', 'first'], ['Second', 'second'], ['Over drive', 'over_drive']]
12
+ r=RaceCar.new
13
+ r.gears.labels.should == labels
14
+ labels_hash.each do |k,v|
15
+ r.gears.label(k).should == v
16
+ end
17
+ r.gears.hash.should == labels_hash
18
+ r.gears.select_options.should == select_options
19
+ end
20
+
21
+ it "should retrieve :gear enums through enums method" do
22
+ r=RaceCar.new
23
+ r.enums(:gear).should == r.gears
24
+ end
25
+
26
+ it "should return a Symbol type from reader methods" do
27
+ r=RaceCar.new
28
+ r.gear.should be_an_instance_of(Symbol)
29
+ end
30
+
31
+ it "should increment and decrement :gear attribute correctly" do
32
+ r=RaceCar.new
33
+ r.gear = :neutral
34
+ r.gear_next.should == :first
35
+ r.gear_next.should == :second
36
+ r.gear_next.should == :over_drive
37
+ r.gear_next.should == :reverse
38
+ r.gear_next.should == :neutral
39
+ r.gear.should == :neutral
40
+ r.gear_previous.should == :reverse
41
+ r.gear_previous.should == :over_drive
42
+ r.gear_previous.should == :second
43
+ r.gear_previous
44
+ r.gear.should == :first
45
+ end
46
+
47
+ it "should have dynamic predicate methods for :gear attribute" do
48
+ r=RaceCar.new
49
+ r.gear = :second
50
+ r.gear_is_in_second?.should be_true
51
+ r.gear_not_in_second?.should be_false
52
+ r.gear_is_nil?.should be_false
53
+ r.gear_is_not_nil?.should be_true
54
+ end
55
+
56
+ it "should have working dynamic predicate methods on retrieved objects" do
57
+ r=RaceCar.new
58
+ r.gear = :second
59
+ r.save!
60
+
61
+ s=RaceCar.find r.id
62
+ s.should_not be_nil
63
+ s.gear_is_in_second?.should be_true
64
+ s.gear_is_not_in_second?.should be_false
65
+ s.gear_is_nil?.should be_false
66
+ s.gear_is_not_nil?.should be_true
67
+ end
68
+
69
+ it "should be created and found with dynamic find or creator method" do
70
+ s = RaceCar.find_or_create_by_name_and_gear('specialty', :second)
71
+ s.should_not be_nil
72
+ s.gear.should == :second
73
+ s.name.should == 'specialty'
74
+
75
+ s0 = RaceCar.find_or_create_by_name_and_gear('specialty', :second)
76
+ s0.gear.should == :second
77
+ s0.id.should == s.id
78
+ end
79
+ it "should be initialized with dynamic find or initialize method" do
80
+ s = RaceCar.find_or_initialize_by_name_and_gear('myspecialty', :second)
81
+ s.should_not be_nil
82
+ s.gear.should == :second
83
+ s.name.should == 'myspecialty'
84
+ lambda { s.save! }.should_not raise_exception
85
+
86
+ s0 = RaceCar.find_or_initialize_by_name_and_gear('myspecialty', :second)
87
+ s0.gear.should == :second
88
+ s0.id.should == s.id
89
+ end
90
+ it "should find record using dynamic finder by enumerated column :gear attributes" do
91
+ r=RaceCar.new
92
+ r.gear = :second
93
+ r.name = 'special'
94
+ r.save!
95
+
96
+ s=RaceCar.find_by_gear_and_name(:second, 'special')
97
+ s.should_not be_nil
98
+ s.id.should == r.id
99
+ end
100
+
101
+ it "should initialize according to enumerated attribute definitions" do
102
+ r = RaceCar.new
103
+ r.gear.should == :neutral
104
+ r.choke.should == :none
105
+ end
106
+
107
+ it "should create new instance using block" do
108
+ r = RaceCar.new do |r|
109
+ r.gear = :first
110
+ r.choke = :medium
111
+ r.lights = 'on'
112
+ end
113
+ r.gear.should == :first
114
+ r.lights.should == 'on'
115
+ r.choke.should == :medium
116
+ end
117
+
118
+ it "should initialize using parameter hash with symbol keys" do
119
+ r=RaceCar.new(:name=>'FastFurious', :gear=>:second, :lights=>'on', :choke=>:medium)
120
+ r.gear.should == :second
121
+ r.lights.should == 'on'
122
+ r.choke.should == :medium
123
+ end
124
+
125
+ it "should initialize using parameter hash with string keys" do
126
+ r=RaceCar.new({'name'=>'FastFurious', 'gear'=>'second', 'lights'=>'on', 'choke'=>'medium'})
127
+ r.gear.should == :second
128
+ r.lights.should == 'on'
129
+ r.choke.should == :medium
130
+ end
131
+
132
+
133
+ it "should convert non-column enumerated attributes from string to symbols" do
134
+ r=RaceCar.new
135
+ r.choke = 'medium'
136
+ r.choke.should == :medium
137
+ r.save!
138
+ end
139
+
140
+ it "should convert enumerated column attributes from string to symbols" do
141
+ r=RaceCar.new
142
+ r.gear = 'second'
143
+ r.gear.should == :second
144
+ r.save!
145
+
146
+ s=RaceCar.find r.id
147
+ s.gear.should == :second
148
+ end
149
+
150
+ it "should not convert non-enumerated column attributes from string to symbols" do
151
+ r=RaceCar.new
152
+ r.lights = 'off'
153
+ r.lights.should == 'off'
154
+ r.save!
155
+
156
+ s=RaceCar.find r.id
157
+ s.lights.should == 'off'
158
+ end
159
+
160
+ it "should raise InvalidEnumeration when parametrically initialized with :gear=>:drive" do
161
+ r=RaceCar.new
162
+ lambda{ r.gear= :drive}.should raise_error(EnumeratedAttribute::InvalidEnumeration)
163
+ end
164
+
165
+ it "should raise InvalidEnumeration when parametrically initialized with :choke=>:all" do
166
+ r=RaceCar.new
167
+ lambda{ r.choke= :all}.should raise_error(EnumeratedAttribute::InvalidEnumeration)
168
+ end
169
+
170
+ it "should return non-column enumerated attributes from [] method" do
171
+ r = RaceCar.new
172
+ r[:choke].should == :none
173
+ end
174
+
175
+ it "should return enumerated column attributes from [] method" do
176
+ r=RaceCar.new
177
+ r.gear = :neutral
178
+ r[:gear].should == :neutral
179
+ end
180
+
181
+ it "should set non-column enumerated attributes with []= method" do
182
+ r=RaceCar.new
183
+ r[:choke] = :medium
184
+ r.choke.should == :medium
185
+ end
186
+
187
+ it "should set enumerated column attriubtes with []= method" do
188
+ r=RaceCar.new
189
+ r[:gear] = :second
190
+ r.gear.should == :second
191
+ end
192
+
193
+ it "should raise InvalidEnumeration when setting enumerated column attribute with []= method" do
194
+ r=RaceCar.new
195
+ lambda{ r[:gear]= :drive }.should raise_error(EnumeratedAttribute::InvalidEnumeration)
196
+ end
197
+
198
+ it "should set and retrieve string for non-enumerated column attributes with []=" do
199
+ r=RaceCar.new
200
+ r[:lights] = 'on'
201
+ r.lights.should == 'on'
202
+ r[:lights].should == 'on'
203
+ end
204
+
205
+ it "should set and retrieve symbol for non-enumerated column attributes with []=" do
206
+ r=RaceCar.new
207
+ r[:lights] = :on
208
+ r.lights.should == :on
209
+ r[:lights].should == :on
210
+ end
211
+
212
+ it "should raise InvalidEnumeration for invalid enum passed to attributes=" do
213
+ r=RaceCar.new
214
+ lambda { r.attributes = {:lights=>'off', :gear =>:drive} }.should raise_error(EnumeratedAttribute::InvalidEnumeration)
215
+ end
216
+
217
+ =begin
218
+ #do not write symbols to enumerated column attributes using write_attribute
219
+ #do not user read_attribute to read an enumerated column attribute
220
+ it "should write enumeration with write_attribute" do
221
+ r=RaceCar.new
222
+ r.write_attribute(:gear, :first)
223
+ r.gear.should == :first
224
+ r.save!
225
+
226
+ s=RaceCar.find r.id
227
+ s.gear.should == :first
228
+ s.write_attribute(:gear, :second)
229
+ s.save!
230
+
231
+ t=RaceCar.find s.id
232
+ t.gear.should == :second
233
+ end
234
+
235
+ it "should raise error when setting enumerated column attribute to invalid enum using write_attribute" do
236
+ r=RaceCar.new
237
+ lambda { r.write_attribute(:gear, :yo) }.should raise_error
238
+ end
239
+ =end
240
+
241
+ it "should retrieve symbols for enumerations from ActiveRecord :attributes method" do
242
+ r=RaceCar.new
243
+ r.gear = :second
244
+ r.choke = :medium
245
+ r.lights = 'on'
246
+ r.save!
247
+
248
+ s = RaceCar.find(r.id)
249
+ s.attributes['gear'].should == :second
250
+ s.attributes['lights'].should == 'on'
251
+ end
252
+
253
+ it "should update_attribute for enumerated column attribute" do
254
+ r=RaceCar.new
255
+ r.gear = :first
256
+ r.save!
257
+ r.update_attribute(:gear, :second)
258
+ r.gear.should == :second
259
+
260
+ s=RaceCar.find r.id
261
+ s.gear.should == :second
262
+ end
263
+
264
+ it "should update_attribute for non-enumerated column attribute" do
265
+ r=RaceCar.new
266
+ r.lights = 'on'
267
+ r.save!
268
+ r.update_attribute(:lights, 'off')
269
+ r.lights.should == 'off'
270
+
271
+ s=RaceCar.find r.id
272
+ s.lights.should == 'off'
273
+ end
274
+
275
+ it "should update_attributes for both non- and enumerated column attributes" do
276
+ r=RaceCar.new
277
+ r.gear = :first
278
+ r.lights = 'off'
279
+ r.save!
280
+ r.update_attributes({:gear=>:second, :lights=>'on'})
281
+ s=RaceCar.find r.id
282
+ s.gear.should == :second
283
+ s.lights.should == 'on'
284
+ s.update_attributes({:gear=>'over_drive', :lights=>'off'})
285
+ t=RaceCar.find s.id
286
+ t.gear.should == :over_drive
287
+ t.lights.should == 'off'
288
+ end
289
+
290
+ it "should provide symbol values for enumerated column attributes from the :attributes method" do
291
+ r=RaceCar.new
292
+ r.lights = 'on'
293
+ r.save!
294
+
295
+ s=RaceCar.find r.id
296
+ s.attributes['gear'].should == :neutral
297
+ end
298
+
299
+ it "should provide normal values for non-enumerated column attributes from the :attributes method" do
300
+ r=RaceCar.new
301
+ r.lights = 'on'
302
+ r.save!
303
+
304
+ s=RaceCar.find r.id
305
+ s.attributes['lights'].should == 'on'
306
+ end
307
+
308
+ it "should raise InvalidEnumeration when setting invalid enumertion value with :attributes= method" do
309
+ r=RaceCar.new
310
+ lambda { r.attributes = {:gear=>:yo, :lights=>'on'} }.should raise_error(EnumeratedAttribute::InvalidEnumeration)
311
+ end
312
+
313
+ it "should not set init value for enumerated column attribute saved as nil" do
314
+ r=RaceCar.new
315
+ r.gear = nil
316
+ r.lights = 'on'
317
+ r.save!
318
+
319
+ s=RaceCar.find r.id
320
+ s.gear.should == nil
321
+ s.lights.should == 'on'
322
+ end
323
+
324
+ it "should not set init value for enumerated column attributes saved as value" do
325
+ r=RaceCar.new
326
+ r.gear = :second
327
+ r.lights = 'all'
328
+ r.save!
329
+
330
+ s=RaceCar.find r.id
331
+ s.gear.should == :second
332
+ s.lights.should == 'all'
333
+ end
334
+
335
+ it "should save and retrieve its name" do
336
+ r = RaceCar.new
337
+ r.name= 'Green Meanie'
338
+ r.save!
339
+
340
+ s = RaceCar.find r.id
341
+ s.should_not be_nil
342
+ s.name.should == 'Green Meanie'
343
+ end
344
+
345
+ it "should save and retrieve symbols for enumerated column attribute" do
346
+ r = RaceCar.new
347
+ r.gear = :over_drive
348
+ r.save!
349
+
350
+ s = RaceCar.find r.id
351
+ #s.should_not be_nil
352
+ s.gear.should == :over_drive
353
+ end
354
+
355
+ it "should not save values for non-column enumerated attributes" do
356
+ r=RaceCar.new
357
+ r.choke = :medium
358
+ r.save!
359
+
360
+ s=RaceCar.find r.id
361
+ s.choke.should == :none
362
+ end
363
+
364
+ it "should save string and retrieve string for non-enumerated column attributes" do
365
+ r =RaceCar.new
366
+ r.lights = 'on'
367
+ r.save!
368
+
369
+ s = RaceCar.find r.id
370
+ s.lights.should == 'on'
371
+ s[:lights].should == 'on'
372
+ end
373
+
374
+ it "should save symbol and retrieve string for non-enumerated column attributes" do
375
+ r =RaceCar.new
376
+ r.lights = :off
377
+ r.save!
378
+
379
+ s = RaceCar.find r.id
380
+ s.lights.should == "--- :off\n"
381
+ s[:lights].should == "--- :off\n"
382
+ end
383
+
384
+ end