enumerate_it 0.7.17 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- enumerate_it (0.7.17)
4
+ enumerate_it (1.0.0)
5
5
  activesupport (>= 3.0.0)
6
6
 
7
7
  GEM
@@ -39,6 +39,15 @@ What does it mean when we say that someone or something is '2'?
39
39
 
40
40
  Enter EnumerateIt.
41
41
 
42
+ == Documentation
43
+
44
+ http://rubydoc.info/gems/enumerate_it/1.0.0/frames
45
+
46
+ == About versions compatibility
47
+
48
+ Versions 1.x.x are NOT backwards compatible with 0.x.x versions. The biggest difference is that on 1.0.0 you need to `extend` the EnumerateIt
49
+ module inside classes that are going to have enumerated attributes, while in past versions you would use `include`.
50
+
42
51
  == Creating enumerations
43
52
 
44
53
  Enumerations are created as models, but you can put then anywhere in your application. In Rails
@@ -84,7 +93,7 @@ This will create some nice stuff:
84
93
 
85
94
  RelationshipStatus.each_value { |value| # ... }
86
95
 
87
- * You can iterate over the list of the enumeration's translations:
96
+ * You can iterate over the list of the enumeration's translations:
88
97
 
89
98
  RelationshipStatus.each_translation { |translation| # ... }
90
99
 
@@ -119,7 +128,7 @@ The cool part is that you can use these enumerations with any class, be it an Ac
119
128
  or not.
120
129
 
121
130
  class Person
122
- include EnumerateIt
131
+ extend EnumerateIt
123
132
  attr_accessor :relationship_status
124
133
 
125
134
  has_enumeration_for :relationship_status, :with => RelationshipStatus
@@ -133,7 +142,7 @@ This will create:
133
142
 
134
143
  p = Person.new
135
144
  p.relationship_status = RelationshipStatus::DIVORCED
136
- p.relationsip_status_humanize # => 'Divorced'
145
+ p.relationship_status_humanize # => 'Divorced'
137
146
 
138
147
  * If you don't supply a humanized string to represent an option, EnumerateIt will use a 'humanized' version of the hash's key to humanize the attribute's value:
139
148
 
@@ -163,7 +172,7 @@ This will create:
163
172
  p.married? #=> true
164
173
  p.divorced? #=> false
165
174
 
166
- * It's also possible to "namespace" the created helper methods, passing a hash to the :create_helpers option.
175
+ * It's also possible to "namespace" the created helper methods, passing a hash to the :create_helpers option.
167
176
  This can be useful when two or more of the enumerations used share the same constants.
168
177
 
169
178
  class Person < ActiveRecord::Base
@@ -194,7 +203,7 @@ This will create:
194
203
 
195
204
  Person.married.to_sql # => SELECT "people".* FROM "people" WHERE "people"."relationship_status" = 1
196
205
 
197
- NOTE: The :create_scopes option can only be used for Rails.version >= 3.0.0.
206
+ NOTE: The :create_scopes option can only be used for Rails.version >= 3.0.0.
198
207
 
199
208
  * If your class can manage validations and responds to :validates_inclusion_of, it will create this validation:
200
209
 
@@ -261,7 +270,7 @@ You can also translate specific values:
261
270
 
262
271
  * Create an initializer with the following code:
263
272
 
264
- ActiveRecord::Base.send :include, EnumerateIt
273
+ ActiveRecord::Base.extend EnumerateIt
265
274
 
266
275
  * Add the 'enumerate_it' gem as a dependency in your environment.rb (Rails 2.3.x) or Gemfile (if you're using Bundler)
267
276
 
@@ -273,7 +282,7 @@ An interesting approach to use it in Rails apps is to create an app/enumerations
273
282
  end
274
283
  end
275
284
 
276
- There is also a Rails Generator that you can use to generate enumerations and their locale files. Take a look at how to use it running
285
+ There is also a Rails Generator that you can use to generate enumerations and their locale files. Take a look at how to use it running
277
286
 
278
287
  rails generate enumerate_it --help
279
288
 
@@ -78,7 +78,7 @@
78
78
  #
79
79
  # RelationshipStatus.each_value { |value| # ... }
80
80
  #
81
- # You can iterate over the list of the enumeration's translations:
81
+ # You can iterate over the list of the enumeration's translations:
82
82
  #
83
83
  # RelationshipStatus.each_translation { |translation| # ... }
84
84
  #
@@ -91,17 +91,17 @@
91
91
  # RelationshipStatus.enumeration # returns the exact hash used to define the enumeration
92
92
  #
93
93
  # You can also create enumerations in the following ways:
94
- #
94
+ #
95
95
  # * Passing an array of symbols, so that the respective value for each symbol will be the stringified version of the symbol itself:
96
- #
96
+ #
97
97
  # class RelationshipStatus < EnumerateIt::Base
98
98
  # associate_values :married, :single
99
99
  # end
100
- #
100
+ #
101
101
  # RelationshipStatus::MARRIED # returns "married" and so on
102
- #
102
+ #
103
103
  # * Passing hashes where the value for each key/pair does not include a translation. In this case, the I18n feature will be used (more on this below):
104
- #
104
+ #
105
105
  # class RelationshipStatus < EnumerateIt::Base
106
106
  # associate_values :married => 1, :single => 2
107
107
  # end
@@ -112,7 +112,7 @@
112
112
  # or not.
113
113
  #
114
114
  # class Person
115
- # include EnumerateIt
115
+ # extend EnumerateIt
116
116
  # attr_accessor :relationship_status
117
117
  #
118
118
  # has_enumeration_for :relationship_status, :with => RelationshipStatus
@@ -158,7 +158,7 @@
158
158
  # p.married? #=> true
159
159
  # p.divorced? #=> false
160
160
  #
161
- # - It's also possible to "namespace" the created helper methods, passing a hash to the :create_helpers option.
161
+ # - It's also possible to "namespace" the created helper methods, passing a hash to the :create_helpers option.
162
162
  # This can be useful when two or more of the enumerations used share the same constants.
163
163
  #
164
164
  # class Person < ActiveRecord::Base
@@ -205,7 +205,7 @@
205
205
  #
206
206
  # * Create an initializer with the following code:
207
207
  #
208
- # ActiveRecord::Base.send :include, EnumerateIt
208
+ # ActiveRecord::Base.extend EnumerateIt
209
209
  #
210
210
  # * Add the 'enumerate_it' gem as a dependency in your environment.rb (Rails 2.3.x) or Gemfile (if you're using Bundler)
211
211
  #
@@ -223,173 +223,11 @@
223
223
  #
224
224
 
225
225
  require "active_support/core_ext/class/attribute"
226
- module EnumerateIt
227
- class Base
228
- @@registered_enumerations = {}
229
-
230
- def self.associate_values(*args)
231
- values_hash = args.first.is_a?(Hash) ? args.first : args.inject({}) { |h, v| h[v] = v.to_s; h }
232
-
233
- register_enumeration normalize_enumeration(values_hash)
234
- values_hash.each_pair { |value_name, attributes| define_enumeration_constant value_name, attributes[0] }
235
- end
236
-
237
- def self.list
238
- enumeration.values.map { |value| value[0] }.sort
239
- end
240
-
241
- def self.enumeration
242
- @@registered_enumerations[self]
243
- end
244
-
245
- def self.to_a
246
- enumeration.values.map {|value| [translate(value[1]), value[0]] }.sort_by { |value| value[0] }
247
- end
248
-
249
- def self.length
250
- list.length
251
- end
252
-
253
- def self.each_translation
254
- each_value { |value| yield t(value) }
255
- end
256
-
257
- def self.each_value
258
- list.each { |value| yield value }
259
- end
260
-
261
- def self.to_json
262
- enumeration.values.collect {|value| { :value => value[0], :label => translate(value[1]) } }.to_json
263
- end
264
-
265
- def self.t(value)
266
- target = to_a.detect { |item| item[1] == value }
267
- target ? target[0] : value
268
- end
269
-
270
- def self.values_for(values)
271
- values.map { |v| self.const_get(v.to_sym) }
272
- end
273
-
274
- def self.value_for(value)
275
- self.const_get(value.to_sym)
276
- end
277
-
278
- def self.key_for(value)
279
- enumeration.map {|e| e[0] if e[1][0] == value }.compact.first
280
- end
281
-
282
- def self.to_range
283
- (list.min..list.max)
284
- end
285
-
286
- private
287
- def self.translate(value)
288
- return value unless value.is_a? Symbol
226
+ require "enumerate_it/base"
227
+ require "enumerate_it/class_methods"
289
228
 
290
- default = value.to_s.gsub(/_/, ' ').split.map(&:capitalize).join(' ')
291
- I18n.t("enumerations.#{self.name.underscore}.#{value.to_s.underscore}", :default => default)
292
- end
293
-
294
- def self.normalize_enumeration(values_hash)
295
- values_hash.each_pair do |key, value|
296
- unless value.is_a? Array
297
- values_hash[key] = [value, key]
298
- end
299
- end
300
- end
301
-
302
- def self.register_enumeration(values_hash)
303
- @@registered_enumerations[self] = values_hash
304
- end
305
-
306
- def self.define_enumeration_constant(name, value)
307
- const_set name.to_s.upcase, value
308
- end
309
- end
310
-
311
- module ClassMethods
312
- def has_enumeration_for(attribute, options = {})
313
- self.enumerations = self.enumerations.dup
314
-
315
- define_enumeration_class attribute, options
316
- set_validations attribute, options
317
- create_enumeration_humanize_method options[:with], attribute
318
- store_enumeration options[:with], attribute
319
- if options[:create_helpers]
320
- create_helper_methods options[:with], attribute, options[:create_helpers]
321
- create_mutator_methods options[:with], attribute, options[:create_helpers]
322
- end
323
-
324
- if options[:create_scopes]
325
- create_scopes options[:with], attribute
326
- end
327
- end
328
-
329
- private
330
- def store_enumeration(klass, attribute)
331
- enumerations[attribute] = klass
332
- end
333
-
334
- def create_enumeration_humanize_method(klass, attribute_name)
335
- class_eval do
336
- define_method "#{attribute_name}_humanize" do
337
- values = klass.enumeration.values.detect { |v| v[0] == self.send(attribute_name) }
338
-
339
- values ? klass.translate(values[1]) : nil
340
- end
341
- end
342
- end
343
-
344
- def create_helper_methods(klass, attribute_name, helpers)
345
- prefix_name = "#{attribute_name}_" if helpers.is_a?(Hash) && helpers[:prefix]
346
-
347
- class_eval do
348
- klass.enumeration.keys.each do |option|
349
- define_method "#{prefix_name}#{option}?" do
350
- self.send(attribute_name) == klass.enumeration[option].first
351
- end
352
- end
353
- end
354
- end
355
-
356
- def create_scopes(klass, attribute_name)
357
- klass.enumeration.keys.each do |option|
358
- if respond_to? :scope
359
- scope option, lambda { where(attribute_name => klass.enumeration[option].first)}
360
- end
361
- end
362
- end
363
-
364
- def create_mutator_methods(klass, attribute_name, helpers)
365
- prefix_name = "#{attribute_name}_" if helpers.is_a?(Hash) && helpers[:prefix]
366
-
367
- class_eval do
368
- klass.enumeration.each_pair do |key, values|
369
- define_method "#{prefix_name}#{key}!" do
370
- self.send "#{attribute_name}=", values.first
371
- end
372
- end
373
- end
374
- end
375
-
376
- def define_enumeration_class(attribute, options)
377
- if options[:with].nil?
378
- options[:with] = attribute.to_s.camelize.constantize
379
- end
380
- end
381
-
382
- def set_validations(attribute, options)
383
- validates_inclusion_of(attribute, :in => options[:with].list, :allow_blank => true) if self.respond_to?(:validates_inclusion_of)
384
-
385
- if options[:required] && respond_to?(:validates_presence_of)
386
- opts = options[:required].is_a?(Hash) ? options[:required] : {}
387
- validates_presence_of(attribute, opts)
388
- end
389
- end
390
- end
391
-
392
- def self.included(receiver)
229
+ module EnumerateIt
230
+ def self.extended(receiver)
393
231
  receiver.class_attribute :enumerations, :instance_writer => false, :instance_reader => false
394
232
  receiver.enumerations = {}
395
233
 
@@ -0,0 +1,86 @@
1
+ # encoding: utf-8
2
+ module EnumerateIt
3
+ class Base
4
+ @@registered_enumerations = {}
5
+
6
+ def self.associate_values(*args)
7
+ values_hash = args.first.is_a?(Hash) ? args.first : args.inject({}) { |h, v| h[v] = v.to_s; h }
8
+
9
+ register_enumeration normalize_enumeration(values_hash)
10
+ values_hash.each_pair { |value_name, attributes| define_enumeration_constant value_name, attributes[0] }
11
+ end
12
+
13
+ def self.list
14
+ enumeration.values.map { |value| value[0] }.sort
15
+ end
16
+
17
+ def self.enumeration
18
+ @@registered_enumerations[self]
19
+ end
20
+
21
+ def self.to_a
22
+ enumeration.values.map {|value| [translate(value[1]), value[0]] }.sort_by { |value| value[0] }
23
+ end
24
+
25
+ def self.length
26
+ list.length
27
+ end
28
+
29
+ def self.each_translation
30
+ each_value { |value| yield t(value) }
31
+ end
32
+
33
+ def self.each_value
34
+ list.each { |value| yield value }
35
+ end
36
+
37
+ def self.to_json
38
+ enumeration.values.collect {|value| { :value => value[0], :label => translate(value[1]) } }.to_json
39
+ end
40
+
41
+ def self.t(value)
42
+ target = to_a.detect { |item| item[1] == value }
43
+ target ? target[0] : value
44
+ end
45
+
46
+ def self.values_for(values)
47
+ values.map { |v| self.const_get(v.to_sym) }
48
+ end
49
+
50
+ def self.value_for(value)
51
+ self.const_get(value.to_sym)
52
+ end
53
+
54
+ def self.key_for(value)
55
+ enumeration.map {|e| e[0] if e[1][0] == value }.compact.first
56
+ end
57
+
58
+ def self.to_range
59
+ (list.min..list.max)
60
+ end
61
+
62
+ private
63
+ def self.translate(value)
64
+ return value unless value.is_a? Symbol
65
+
66
+ default = value.to_s.gsub(/_/, ' ').split.map(&:capitalize).join(' ')
67
+ I18n.t("enumerations.#{self.name.underscore}.#{value.to_s.underscore}", :default => default)
68
+ end
69
+
70
+ def self.normalize_enumeration(values_hash)
71
+ values_hash.each_pair do |key, value|
72
+ unless value.is_a? Array
73
+ values_hash[key] = [value, key]
74
+ end
75
+ end
76
+ end
77
+
78
+ def self.register_enumeration(values_hash)
79
+ @@registered_enumerations[self] = values_hash
80
+ end
81
+
82
+ def self.define_enumeration_constant(name, value)
83
+ const_set name.to_s.upcase, value
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,83 @@
1
+ # encoding: utf-8
2
+ module EnumerateIt
3
+ module ClassMethods
4
+ def has_enumeration_for(attribute, options = {})
5
+ self.enumerations = self.enumerations.dup
6
+
7
+ define_enumeration_class attribute, options
8
+ set_validations attribute, options
9
+ create_enumeration_humanize_method options[:with], attribute
10
+ store_enumeration options[:with], attribute
11
+ if options[:create_helpers]
12
+ create_helper_methods options[:with], attribute, options[:create_helpers]
13
+ create_mutator_methods options[:with], attribute, options[:create_helpers]
14
+ end
15
+
16
+ if options[:create_scopes]
17
+ create_scopes options[:with], attribute
18
+ end
19
+ end
20
+
21
+ private
22
+ def store_enumeration(klass, attribute)
23
+ enumerations[attribute] = klass
24
+ end
25
+
26
+ def create_enumeration_humanize_method(klass, attribute_name)
27
+ class_eval do
28
+ define_method "#{attribute_name}_humanize" do
29
+ values = klass.enumeration.values.detect { |v| v[0] == self.send(attribute_name) }
30
+
31
+ values ? klass.translate(values[1]) : nil
32
+ end
33
+ end
34
+ end
35
+
36
+ def create_helper_methods(klass, attribute_name, helpers)
37
+ prefix_name = "#{attribute_name}_" if helpers.is_a?(Hash) && helpers[:prefix]
38
+
39
+ class_eval do
40
+ klass.enumeration.keys.each do |option|
41
+ define_method "#{prefix_name}#{option}?" do
42
+ self.send(attribute_name) == klass.enumeration[option].first
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ def create_scopes(klass, attribute_name)
49
+ klass.enumeration.keys.each do |option|
50
+ if respond_to? :scope
51
+ scope option, lambda { where(attribute_name => klass.enumeration[option].first)}
52
+ end
53
+ end
54
+ end
55
+
56
+ def create_mutator_methods(klass, attribute_name, helpers)
57
+ prefix_name = "#{attribute_name}_" if helpers.is_a?(Hash) && helpers[:prefix]
58
+
59
+ class_eval do
60
+ klass.enumeration.each_pair do |key, values|
61
+ define_method "#{prefix_name}#{key}!" do
62
+ self.send "#{attribute_name}=", values.first
63
+ end
64
+ end
65
+ end
66
+ end
67
+
68
+ def define_enumeration_class(attribute, options)
69
+ if options[:with].nil?
70
+ options[:with] = attribute.to_s.camelize.constantize
71
+ end
72
+ end
73
+
74
+ def set_validations(attribute, options)
75
+ validates_inclusion_of(attribute, :in => options[:with].list, :allow_blank => true) if self.respond_to?(:validates_inclusion_of)
76
+
77
+ if options[:required] && respond_to?(:validates_presence_of)
78
+ opts = options[:required].is_a?(Hash) ? options[:required] : {}
79
+ validates_presence_of(attribute, opts)
80
+ end
81
+ end
82
+ end
83
+ end
@@ -1,3 +1,3 @@
1
1
  module EnumerateIt
2
- VERSION = "0.7.17"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -37,14 +37,14 @@ class Foobar < EnumerateIt::Base
37
37
  end
38
38
 
39
39
  class BaseClass
40
- include EnumerateIt
40
+ extend EnumerateIt
41
41
  has_enumeration_for :foobar, :with => TestEnumeration
42
42
  end
43
43
 
44
44
  describe EnumerateIt do
45
45
  before :each do
46
46
  class TestClass
47
- include EnumerateIt
47
+ extend EnumerateIt
48
48
  attr_accessor :foobar
49
49
  has_enumeration_for :foobar, :with => TestEnumeration
50
50
 
@@ -104,7 +104,7 @@ describe EnumerateIt do
104
104
  context "passing the value of each option without the human string (just the value, without an array)" do
105
105
  before :each do
106
106
  class TestClassForEnumerationWithoutArray
107
- include EnumerateIt
107
+ extend EnumerateIt
108
108
  attr_accessor :foobar
109
109
  has_enumeration_for :foobar, :with => TestEnumerationWithoutArray
110
110
 
@@ -130,7 +130,7 @@ describe EnumerateIt do
130
130
  context "without passing the enumeration class" do
131
131
  before :each do
132
132
  class FooBar
133
- include EnumerateIt
133
+ extend EnumerateIt
134
134
  attr_accessor :test_enumeration
135
135
  has_enumeration_for :test_enumeration
136
136
  def initialize(test_enumeration_value)
@@ -367,7 +367,7 @@ describe EnumerateIt::Base do
367
367
  end
368
368
 
369
369
  ActiveRecordStub.stub!(:validates_inclusion_of).and_return(true)
370
- ActiveRecordStub.send :include, EnumerateIt
370
+ ActiveRecordStub.extend EnumerateIt
371
371
  end
372
372
 
373
373
  it "creates a validation for inclusion" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enumerate_it
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.17
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-24 00:00:00.000000000 Z
12
+ date: 2012-11-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -95,6 +95,8 @@ files:
95
95
  - Rakefile
96
96
  - enumerate_it.gemspec
97
97
  - lib/enumerate_it.rb
98
+ - lib/enumerate_it/base.rb
99
+ - lib/enumerate_it/class_methods.rb
98
100
  - lib/enumerate_it/version.rb
99
101
  - lib/generators/USAGE
100
102
  - lib/generators/enumerate_it_generator.rb
@@ -119,7 +121,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
119
121
  version: '0'
120
122
  segments:
121
123
  - 0
122
- hash: -392427821220719135
124
+ hash: -1315505449336147275
123
125
  required_rubygems_version: !ruby/object:Gem::Requirement
124
126
  none: false
125
127
  requirements:
@@ -128,7 +130,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
128
130
  version: '0'
129
131
  segments:
130
132
  - 0
131
- hash: -392427821220719135
133
+ hash: -1315505449336147275
132
134
  requirements: []
133
135
  rubyforge_project:
134
136
  rubygems_version: 1.8.24