extended_has_enumeration 0.0.2 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.4
@@ -34,16 +34,16 @@ Then /^the ([a-z_]+\?) predicate should be (true|false)$/ do |predicate, value|
34
34
  else
35
35
  expected_value = false
36
36
  end
37
- @object.color.send(predicate).should == expected_value
37
+ @object.color.send(predicate).should == expected_value
38
38
  end
39
39
 
40
40
 
41
41
  Given /^a set of objects with a variety of values for the enumeration$/ do
42
- a = ExplicitlyMappedModel.new(:color => :red)
43
- a.color = :red
44
- p a
42
+ #a = ExplicitlyMappedModel.new(:color => :red)
43
+ #a.color = :red
44
+ #p a
45
45
  @model_class.delete_all
46
-
46
+
47
47
  2.times do
48
48
  [:red, :green, :blue].each do |color|
49
49
  @model_class.create!(:color => color)
@@ -20,6 +20,10 @@ class CreateTables < ActiveRecord::Migration
20
20
  t.integer :color
21
21
  end
22
22
 
23
+ create_table :explicitly_mapped_model_with_defaults, :force => true do |t|
24
+ t.integer :color
25
+ end
26
+
23
27
  create_table :implicitly_mapped_models, :force => true do |t|
24
28
  t.string :color
25
29
  end
@@ -1,3 +1,3 @@
1
1
  class ExplicitlyMappedModel < ActiveRecord::Base
2
- has_enumeration :color, :red => 'Red color', :green => 2, :blue => 3
2
+ has_enumeration :color, {:red => 'Red color', :green => 2, :blue => 3}, {allow_blank: true}
3
3
  end
@@ -0,0 +1,3 @@
1
+ class ExplicitlyMappedModelWithDefault < ActiveRecord::Base
2
+ has_enumeration :color, {:red => 'Red color', :green => 2, :blue => 3}, {allow_blank: true, default: :red}
3
+ end
@@ -0,0 +1,3 @@
1
+ class MappedModelWithInitial < ActiveRecord::Base
2
+ has_enumeration :color, {:red => 'Red color', :green => 2, :blue => 3}, {initial: :red, allow_blank: true}
3
+ end
@@ -0,0 +1,3 @@
1
+ class ModelWithWrongDefault < ActiveRecord::Base
2
+ has_enumeration :color, {:red => 'Red color', :green => 2, :blue => 3}, {allow_nil: true, default: :yellow}
3
+ end
@@ -29,13 +29,24 @@ module ExtendedHasEnumeration
29
29
  mapping = mapping_hash
30
30
  end
31
31
 
32
+ if options[:initial].present?
33
+ class_eval do
34
+ after_initialize :"initialize_#{enumeration}"
35
+
36
+ define_method :"initialize_#{enumeration}" do
37
+ self.send("#{enumeration}=", self.send(enumeration).blank? ? options[:initial] : self.send(enumeration))
38
+ end
39
+ end
40
+ end
41
+
42
+
32
43
  # The underlying attribute
33
44
  attribute = options[:attribute] || enumeration
34
45
 
35
46
  # ActiveRecord's composed_of method will do most of the work for us.
36
47
  # All we have to do is cons up a class that implements the bidirectional
37
48
  # mapping described by the provided hash.
38
- klass = create_enumeration_mapping_class(mapping)
49
+ klass = create_enumeration_mapping_class(mapping, options)
39
50
  attr_enumeration_mapping_classes[enumeration] = klass
40
51
 
41
52
  # Bind the class to a name within the scope of this class
@@ -46,8 +57,8 @@ module ExtendedHasEnumeration
46
57
  composed_of(enumeration,
47
58
  :class_name => scoped_class_name,
48
59
  :mapping => [attribute.to_s, 'raw_value'],
49
- :converter => :from_sym,
50
- :allow_nil => true
60
+ :constructor => :constructor,
61
+ :converter => :from_sym
51
62
  )
52
63
 
53
64
  if ActiveRecord::VERSION::MAJOR >= 3 && ActiveRecord::VERSION::MINOR == 0
@@ -68,38 +79,44 @@ module ExtendedHasEnumeration
68
79
  @attr_enumeration_mapping_classes ||= {}
69
80
  end
70
81
 
71
- def create_enumeration_mapping_class(mapping)
82
+ def create_enumeration_mapping_class(mapping, options={})
72
83
  mapping = mapping.with_indifferent_access
84
+ default = options[:default]
85
+ allow_blank = options[:allow_blank]
73
86
  Class.new do
74
- attr_reader :raw_value
75
- alias_method :humanize, :raw_value
87
+ # attr_reader :raw_value
88
+ # alias_method :humanize, :raw_value
89
+ delegate :blank?, to: :raw_value
76
90
 
77
91
  define_method :initialize do |raw_value|
78
92
  @raw_value = raw_value
79
- @value = mapping[raw_value]
93
+ @value = mapping[@raw_value]
94
+ end
95
+
96
+ define_method :raw_value do
97
+ @raw_value.blank? ? default : @raw_value
80
98
  end
81
99
 
82
100
  define_method :to_sym do
83
- @value
101
+ raw_value.try(:to_sym)
84
102
  end
85
103
 
86
104
  define_method :value do
87
- @values
105
+ mapping[raw_value]
88
106
  end
89
107
 
90
108
  define_method :to_s do
91
- @value.to_s
109
+ raw_value.to_s
92
110
  end
93
111
 
94
112
  define_method :humanize do
95
- @raw_value.to_s.humanize
113
+ to_s.humanize
96
114
  end
97
115
 
98
116
  mapping.keys.each do |sym|
99
117
  predicate = "#{sym}?".to_sym
100
- value = mapping[sym]
101
118
  define_method predicate do
102
- @raw_value.to_sym == sym.to_sym
119
+ to_sym == sym.to_sym
103
120
  end
104
121
  end
105
122
 
@@ -108,13 +125,20 @@ module ExtendedHasEnumeration
108
125
  mapping
109
126
  end
110
127
  define_method :from_sym do |sym|
111
- unless mapping.has_key?(sym)
128
+ if !mapping.has_key?(sym) && !sym.blank?
112
129
  raise ArgumentError.new(
113
130
  "#{sym.inspect} is not one of {#{mapping.keys.map(&:inspect).sort.join(', ')}}"
114
131
  )
132
+ elsif !allow_blank && sym.blank?
133
+ raise ArgumentError.new(
134
+ "#{name.split('::').last} can't be blank"
135
+ )
115
136
  end
116
137
  new(sym)
117
138
  end
139
+ define_method :constructor do |sym|
140
+ new(sym)
141
+ end
118
142
  end
119
143
  end
120
144
  end
@@ -40,14 +40,14 @@ end
40
40
  describe ExtendedHasEnumeration, 'with an uninitialied value' do
41
41
  context 'in a newly-created object' do
42
42
  it 'returns nil for the value of the enumeration' do
43
- ExplicitlyMappedModel.new.color.should be_nil
43
+ ExplicitlyMappedModel.new.color.raw_value.should be_nil
44
44
  end
45
45
  end
46
46
 
47
47
  context 'in an existing object' do
48
48
  it 'returns nil for the value of the enumeration' do
49
49
  object = ExplicitlyMappedModel.find(ExplicitlyMappedModel.create!.id)
50
- object.color.should be_nil
50
+ object.color.raw_value.should be_nil
51
51
  end
52
52
  end
53
53
  end
@@ -56,45 +56,123 @@ describe ExtendedHasEnumeration, 'assignment of nil' do
56
56
  it 'sets the enumeration to nil' do
57
57
  object = ExplicitlyMappedModel.new(:color => :red)
58
58
  object.color = nil
59
- object.color.should be_nil
59
+ object.color.raw_value.should be_nil
60
60
  end
61
61
 
62
62
  it 'persists across a trip to the database' do
63
63
  object = ExplicitlyMappedModel.create!(:color => :red)
64
64
  object.color = nil
65
65
  object.save!
66
- ExplicitlyMappedModel.find(object.id).color.should be_nil
66
+ ExplicitlyMappedModel.find(object.id).color.raw_value.should be_nil
67
67
  end
68
68
  end
69
69
 
70
70
  describe ExtendedHasEnumeration, 'string formatting' do
71
- it 'returns the value as a string if to_s is called on it' do
71
+ it 'returns the value as a string if to_s is called on it' do
72
72
  object = ExplicitlyMappedModel.new(:color => :red)
73
- object.color.to_s.should == 'Red color'
73
+ object.color.to_s.should == 'red'
74
+ end
75
+ end
76
+
77
+ describe ExtendedHasEnumeration, 'symbol formatting' do
78
+ it 'returns the value as a string if to_s is called on it' do
79
+ object = ExplicitlyMappedModel.new(:color => :red)
80
+ object.color.to_sym.should == :red
81
+ end
82
+ end
83
+
84
+ describe ExtendedHasEnumeration, 'symbol formatting' do
85
+ it 'returns the value as a string if to_s is called on it' do
86
+ object = ExplicitlyMappedModel.new(:color => :red)
87
+ object.color.value.should == 'Red color'
74
88
  end
75
89
  end
76
90
 
77
91
  describe ExtendedHasEnumeration, 'hash value' do
78
- it 'returns the raw value as a string if raw_value is called on it' do
92
+ it 'returns the raw value as a string if raw_value is called on it' do
79
93
  object = ExplicitlyMappedModel.new(:color => :red)
80
94
  object.color.raw_value.should == :red
81
95
  end
82
96
 
83
- it 'returns the raw value as a string if humanize is called on it' do
97
+ it 'returns the raw value as a string if humanize is called on it' do
84
98
  object = ExplicitlyMappedModel.new(:color => :red)
85
99
  object.color.humanize.should == 'Red'
86
100
  end
87
101
  end
88
102
 
89
103
  describe ExtendedHasEnumeration, 'source' do
90
- it 'returns the passed hash' do
91
- ExplicitlyMappedModel::Color.source.should == {'red'=>'Red color', 'green'=>2, 'blue'=>3}
104
+ it 'returns the passed hash' do
105
+ ExplicitlyMappedModel::Color.source.should == {'red' => 'Red color', 'green' => 2, 'blue' => 3}
92
106
  end
93
107
  end
94
108
 
95
109
  describe ExtendedHasEnumeration, 'has hash with with_indifferent_access' do
96
- it 'it allows assign string' do
110
+ it 'allows assign string' do
97
111
  object = ExplicitlyMappedModel.create!(:color => 'red')
98
112
  object.color.raw_value.should == 'red'
99
113
  end
100
114
  end
115
+
116
+ describe ExtendedHasEnumeration, 'constructor' do
117
+ it 'returns class without passing value' do
118
+ object = ExplicitlyMappedModel.create!
119
+ object.color.class.should == ExplicitlyMappedModel::Color
120
+ end
121
+
122
+ it 'returns nil on raw_value without passing value' do
123
+ object = ExplicitlyMappedModel.create! color: nil
124
+ object.color.raw_value.should be_blank
125
+ end
126
+ end
127
+
128
+ describe ExtendedHasEnumeration, 'default value' do
129
+
130
+ it 'returns true when calls red? on it' do
131
+ object = ExplicitlyMappedModelWithDefault.create!
132
+ object.color.red?.should be_true
133
+ end
134
+
135
+ it 'returns default value' do
136
+ object = ExplicitlyMappedModelWithDefault.create!
137
+ object.color.raw_value.should == :red
138
+ end
139
+
140
+ it 'returns default value of hash' do
141
+ object = ExplicitlyMappedModelWithDefault.create!
142
+ object.color.value.should == 'Red color'
143
+ end
144
+
145
+ it 'returns nil if default is wrong' do
146
+ object = ModelWithWrongDefault.create!
147
+ object.color.value.should be_nil
148
+ end
149
+ end
150
+
151
+ describe ExtendedHasEnumeration, 'initial value' do
152
+
153
+ it 'returns true when calls red? on it' do
154
+ object = MappedModelWithInitial.create!
155
+ object.color.red?.should be_true
156
+ end
157
+
158
+ it 'returns initial value' do
159
+ object = MappedModelWithInitial.create!
160
+ object.color.raw_value.should == :red
161
+ end
162
+
163
+ it 'returns initial value of hash' do
164
+ object = MappedModelWithInitial.create!
165
+ object.color.value.should == 'Red color'
166
+ end
167
+
168
+ it 'returns nil after seting to it' do
169
+ object = MappedModelWithInitial.create!
170
+ object.color = nil
171
+ object.color.raw_value.should be_nil
172
+ end
173
+
174
+ it 'returns nil if default is wrong' do
175
+ object = ModelWithWrongDefault.create!
176
+ object.color.value.should be_nil
177
+ end
178
+ end
data/spec/spec_helper.rb CHANGED
@@ -6,6 +6,9 @@ require 'rspec'
6
6
 
7
7
  require File.expand_path('../lib/extended_has_enumeration', File.dirname(__FILE__))
8
8
  require File.expand_path('../features/support/explicitly_mapped_model', File.dirname(__FILE__))
9
+ require File.expand_path('../features/support/explicitly_mapped_model_with_default', File.dirname(__FILE__))
10
+ require File.expand_path('../features/support/model_with_wrong_default', File.dirname(__FILE__))
11
+ require File.expand_path('../features/support/mapped_model_with_initial', File.dirname(__FILE__))
9
12
 
10
13
  ActiveRecord::Base.establish_connection(
11
14
  :adapter => defined?(JRUBY_VERSION) ? 'jdbcsqlite3': 'sqlite3',
@@ -18,6 +21,18 @@ end
18
21
 
19
22
  class CreateTables < ActiveRecord::Migration
20
23
  create_table :explicitly_mapped_models, :force => true do |t|
21
- t.integer :color
24
+ t.string :color
25
+ end
26
+
27
+ create_table :explicitly_mapped_model_with_defaults, :force => true do |t|
28
+ t.string :color
29
+ end
30
+
31
+ create_table :model_with_wrong_defaults, :force => true do |t|
32
+ t.string :color
33
+ end
34
+
35
+ create_table :mapped_model_with_initials, :force => true do |t|
36
+ t.string :color
22
37
  end
23
38
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: extended_has_enumeration
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -85,6 +85,9 @@ files:
85
85
  - features/step_definitions/has_enumeration_steps.rb
86
86
  - features/support/env.rb
87
87
  - features/support/explicitly_mapped_model.rb
88
+ - features/support/explicitly_mapped_model_with_default.rb
89
+ - features/support/model_with_wrong_default.rb
90
+ - features/support/mapped_model_with_initial.rb
88
91
  - features/support/implicitly_mapped_model.rb
89
92
  - features/support/nonstandard_attribute_model.rb
90
93
  - install_gemsets.sh