rails_attr_enum 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,15 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 294b0316bab39b37461ec932a8aacf826401f714
4
- data.tar.gz: 2d781e84e8ffd18095a210c26fc7a3afecd1083c
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YTZmNmQyMTg3YzVmN2VmYjgyNGFmZDJkYjZiMzhlYzFkYmRjMDRhMg==
5
+ data.tar.gz: !binary |-
6
+ NzUyMTU2NTJhMDIzYzY0NjU4MmJjMGE5MmE5ZGVjYzIwNDJiNTllOQ==
5
7
  SHA512:
6
- metadata.gz: 59f6044b759d53a721eda593a369c21c7e6507b5869522b0896b2953e84a5f9b9afc328494d28e2bbb32d75af87e733c1fbabbdeff59b5b6159f0b04f15d5eb4
7
- data.tar.gz: 8b506c2b581c60cdd6fda0b07e5f148072fd001df4be6f418f26cda960088b4606b0d492a00a28b742f0f13bfcb2ed985941b1412273522cdaadcbd2ce7a74a5
8
+ metadata.gz: !binary |-
9
+ MTM2MDQwMGRmYTU0ZWM5Njg1MTMxMzJjNWM1ZDUwZTFjNDI2MTZhMmZmZTI1
10
+ ODEwZDMyOWNhZDkyNmJlNDM5MjFiMWI0ZjQ0YmYxNTczNGI1MThkYzlhMDU4
11
+ NzRmNTQwYjZlM2JlNGU2ZmYzOThlMzhlYzJlYWMzNzI5Y2I3MGM=
12
+ data.tar.gz: !binary |-
13
+ OWVhOTEwNDM0YWJlYTg3ZTc4NGExNmRlNGRiNzhmNDcyNDQyNTY0MzdlZDdk
14
+ ZGNiM2M3NGQ1Y2QwMDIwNWE2OTU0Zjc1MjZjOTk1NTI0NDJkYWQ5NjYyNzY5
15
+ Y2ZhNzIxYzRkYmNhOTM5NWFlMmNmOGZmNGRkMmMzNjdhYWZmZTU=
data/CHANGELOG.md ADDED
@@ -0,0 +1,18 @@
1
+ ### v0.0.6
2
+ * Added example usage of extending `RailsAttrEnum` and calling
3
+ `attr_enum` in `User` model in test Rails app.
4
+ * Fixed a bug that crashed the Rails environment when a model without an
5
+ existing table used `RailsAttrEnum`. An example of this happening
6
+ would be when someone tried to run `rake db:schema:load`.
7
+ * Fixed a bug in `clear_user` in the spec_helper.rb file with calling
8
+ the private `remove_instance_variable` method on the test model
9
+ `User`.
10
+ * Added `default_user_roles` to spec_helper.rb file, so a default usage
11
+ of extending `RailsAttrEnum` and calling `attr_enum` could be
12
+ established. This allows other future specs that depend on that
13
+ default usage to not be messed up by the user_spec.rb file which
14
+ alters `RailsAttrEnum` usage in `User` for different tests.
15
+
16
+ ### v0.0.5
17
+ * Fix a bug with the 'to_json' and 'as_json' methods for a model that
18
+ has an enum attribute set up.
@@ -32,7 +32,7 @@ module RailsAttrEnum
32
32
 
33
33
  @_attr_enums ||= {}
34
34
 
35
- unless column_names.include?(attr.name)
35
+ if table_exists? && !column_names.include?(attr.name)
36
36
  raise "Invalid attribute name #{attr.name}"
37
37
  end
38
38
 
@@ -42,10 +42,14 @@ module RailsAttrEnum
42
42
 
43
43
  @_attr_enums[attr.name.to_sym] = Module.new { include Enum }.tap do |mod|
44
44
  mod.init_enum(attr.name)
45
- entries.each(&mod.method(:add))
46
45
  const_set(attr.enum_name, mod)
47
- class_eval do
48
- validates(attr.name.to_sym, validation_rules.merge(inclusion: { in: mod.values }))
46
+ validates(attr.name.to_sym, validation_rules.merge(inclusion: { in: mod.values }))
47
+
48
+ entries.each do |entry|
49
+ mod.add(entry)
50
+ class_eval <<-EOS
51
+ scope :#{attr.name}_#{entry.key}, -> { where(:#{attr.name} => #{entry.value}) }
52
+ EOS
49
53
  end
50
54
  end
51
55
 
@@ -2,5 +2,17 @@ require 'ostruct'
2
2
 
3
3
  module RailsAttrEnum
4
4
  ## Enum entry class
5
- Entry = Struct.new(:key, :value, :label)
5
+ Entry = Struct.new(:const_name, :key, :value, :label) do
6
+ # Add a to_h method if it doesn't exist (< ruby 2.0)
7
+ unless nil.respond_to?(:to_h)
8
+ def to_h
9
+ # Hash[*(entry.members.flat_map { |key| [key, entry.send(key)] })]
10
+ Hash[members.map { |key| [key, send(key)] }]
11
+ end
12
+ end
13
+
14
+ def to_json(*args)
15
+ to_h.to_json
16
+ end
17
+ end
6
18
  end
@@ -38,6 +38,50 @@ module RailsAttrEnum
38
38
  def values; mapped(:value) end
39
39
  def labels; mapped(:label) end
40
40
 
41
+ def to_h(options = {})
42
+ default_to_include = [:key, :value, :label]
43
+
44
+ [:only, :except].each do |key|
45
+ if options.include?(key)
46
+ if options[key].is_a?(Symbol)
47
+ options[key] = [options[key]]
48
+ elsif options[key].empty?
49
+ options[key] = nil
50
+ end
51
+
52
+ unless options[key].nil? || (options[key] - default_to_include).empty?
53
+ raise 'Unknown keys for enum'
54
+ end
55
+ end
56
+ end
57
+
58
+ to_include =
59
+ if !options[:only].nil?
60
+ default_to_include & options[:only]
61
+ elsif !options[:except].nil?
62
+ default_to_include - options[:except]
63
+ else
64
+ default_to_include
65
+ end
66
+
67
+ builder =
68
+ if to_include.size == 1
69
+ to_include = to_include.first
70
+ proc { |entry| [entry.const_name, entry.send(to_include)]}
71
+ else
72
+ proc do |entry|
73
+ value = Hash[entry.to_h.select { |(key, _)| to_include.include?(key) }]
74
+ [entry.const_name, value]
75
+ end
76
+ end
77
+
78
+ Hash[@entries.map(&builder)]
79
+ end
80
+
81
+ def to_json(options = {})
82
+ to_h(options).to_json
83
+ end
84
+
41
85
  private
42
86
 
43
87
  def get_from_entries(key, value)
@@ -31,7 +31,9 @@ module RailsAttrEnum
31
31
  label = key.to_s.titleize
32
32
  end
33
33
 
34
- add_entry(key, value, label)
34
+ const_name = key.to_s.upcase
35
+
36
+ add_entry(const_name, key, value, label)
35
37
  end
36
38
 
37
39
  def validates(rules = {})
@@ -40,8 +42,8 @@ module RailsAttrEnum
40
42
 
41
43
  private
42
44
 
43
- def add_entry(key, value, label)
44
- Entry.new(key, value, label).tap do |entry|
45
+ def add_entry(const_name, key, value, label)
46
+ Entry.new(const_name, key, value, label).tap do |entry|
45
47
  revalue_entries!(value)
46
48
  @entries << entry
47
49
  @values << value
@@ -1,3 +1,3 @@
1
1
  module RailsAttrEnum
2
- VERSION = '0.0.5'
2
+ VERSION = '0.0.6'
3
3
  end
@@ -1,2 +1,5 @@
1
1
  class User < ActiveRecord::Base
2
+ extend RailsAttrEnum
3
+
4
+ attr_enum :role, :admin, :editor, :author, :user
2
5
  end
Binary file
Binary file
@@ -32,3 +32,68 @@ Migrating to CreateUsers (20131023212242)
32
32
   (0.1ms) begin transaction
33
33
  SQL (0.3ms) DELETE FROM "users" WHERE "users"."id" = ? [["id", 3]]
34
34
   (1.2ms) commit transaction
35
+ User Load (0.5ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
36
+  (0.1ms) begin transaction
37
+ SQL (85.7ms) INSERT INTO "users" ("created_at", "name", "role", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Thu, 31 Oct 2013 16:03:34 UTC +00:00], ["name", "jeremy fairbank"], ["role", 1], ["updated_at", Thu, 31 Oct 2013 16:03:34 UTC +00:00]]
38
+  (1.7ms) commit transaction
39
+ User Load (0.3ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
40
+ User Load (23.3ms) SELECT "users".* FROM "users" WHERE "users"."role" = 2
41
+ User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."role" = 0
42
+ User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."role" = 2
43
+  (0.3ms) SELECT COUNT(*) FROM "users"
44
+ User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."role" = 1
45
+ User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."role" = 1 AND (name like '%j%')
46
+ User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."role" = 1
47
+ User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."role" = 1
48
+ DEPRECATION WARNING: Model.scoped is deprecated. Please use Model.all instead. (called from method_missing at /Users/jeremy/Dropbox/projects/rails_attr_enum/lib/rails_attr_enum.rb:83)
49
+ User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."role" = 1
50
+ User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."role" = 2
51
+ User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."role" = 0
52
+ User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."role" = 2
53
+ User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."role" = 1
54
+ User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."role" = 1 AND (name like '%j%')
55
+ User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."role" = 1 AND (name like '%j%')
56
+ DEPRECATION WARNING: Model.scoped is deprecated. Please use Model.all instead. (called from irb_binding at (irb):3)
57
+ User Load (0.3ms) SELECT "users".* FROM "users"
58
+ User Load (0.4ms) SELECT "users".* FROM "users"
59
+ User Load (0.4ms) SELECT "users".* FROM "users"
60
+ User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."role" = 2
61
+ User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."role" = 2
62
+ User Load (0.3ms) SELECT "users".* FROM "users" LIMIT 2
63
+ User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."role" = 2
64
+ User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."role" = 1
65
+ User Load (0.3ms) SELECT "users".* FROM "users" WHERE (("users"."role" = 2 OR "users"."role" = 1))
66
+ User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."role" IN (2, 1)
67
+ User Load (0.3ms) SELECT "users".* FROM "users" WHERE (role = 2 or role = 1)
68
+ User Load (0.3ms) SELECT "users".* FROM "users" WHERE (("users"."role" = 1 OR "users"."role" = 2))
69
+ User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."role" = 2
70
+ User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."role" = 2 AND "users"."role" = 1
71
+ User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."role" = 2
72
+ User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."role" = 1
73
+ User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."role" = 2
74
+ User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."role" = 1
75
+ User Load (0.3ms) SELECT "users".* FROM "users" WHERE (("users"."role" = 1 OR "users"."role" = 2))
76
+  (3.1ms) CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "role" integer, "created_at" datetime, "updated_at" datetime) 
77
+  (1.4ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
78
+  (1.0ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
79
+  (0.1ms) SELECT version FROM "schema_migrations"
80
+  (1.0ms) INSERT INTO "schema_migrations" (version) VALUES ('20131023212242')
81
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
82
+  (2.5ms) CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "role" integer, "created_at" datetime, "updated_at" datetime) 
83
+  (1.2ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
84
+  (1.2ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
85
+  (0.1ms) SELECT version FROM "schema_migrations"
86
+  (1.0ms) INSERT INTO "schema_migrations" (version) VALUES ('20131023212242')
87
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
88
+  (3.7ms) CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "role" integer, "created_at" datetime, "updated_at" datetime) 
89
+  (1.4ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
90
+  (1.2ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
91
+  (0.1ms) SELECT version FROM "schema_migrations"
92
+  (1.2ms) INSERT INTO "schema_migrations" (version) VALUES ('20131023212242')
93
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
94
+  (2.8ms) CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "role" integer, "created_at" datetime, "updated_at" datetime) 
95
+  (1.4ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
96
+  (1.3ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
97
+  (0.1ms) SELECT version FROM "schema_migrations"
98
+  (1.3ms) INSERT INTO "schema_migrations" (version) VALUES ('20131023212242')
99
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
@@ -330,3 +330,403 @@ Migrating to CreateUsers (20131023212242)
330
330
   (0.1ms) rollback transaction
331
331
   (0.1ms) begin transaction
332
332
   (0.1ms) rollback transaction
333
+  (1.3ms) CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "role" integer, "created_at" datetime, "updated_at" datetime) 
334
+  (1.1ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
335
+  (0.9ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
336
+  (0.1ms) SELECT version FROM "schema_migrations"
337
+  (1.0ms) INSERT INTO "schema_migrations" (version) VALUES ('20131023212242')
338
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
339
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
340
+  (0.1ms) begin transaction
341
+  (0.2ms) rollback transaction
342
+  (0.1ms) begin transaction
343
+  (0.1ms) rollback transaction
344
+  (0.1ms) begin transaction
345
+  (0.2ms) rollback transaction
346
+  (0.1ms) begin transaction
347
+  (0.1ms) rollback transaction
348
+  (0.1ms) begin transaction
349
+  (0.1ms) rollback transaction
350
+  (0.1ms) begin transaction
351
+  (0.1ms) rollback transaction
352
+  (0.1ms) begin transaction
353
+  (0.1ms) rollback transaction
354
+  (0.1ms) begin transaction
355
+  (0.1ms) rollback transaction
356
+  (0.1ms) begin transaction
357
+  (0.1ms) rollback transaction
358
+  (0.1ms) begin transaction
359
+  (0.1ms) rollback transaction
360
+  (0.1ms) begin transaction
361
+  (0.1ms) rollback transaction
362
+  (0.1ms) begin transaction
363
+  (0.1ms) rollback transaction
364
+  (0.1ms) begin transaction
365
+  (0.1ms) rollback transaction
366
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
367
+  (0.1ms) begin transaction
368
+  (0.1ms) rollback transaction
369
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
370
+  (0.1ms) begin transaction
371
+  (0.1ms) rollback transaction
372
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
373
+  (0.1ms) begin transaction
374
+  (0.1ms) rollback transaction
375
+  (0.1ms) begin transaction
376
+  (0.1ms) rollback transaction
377
+  (0.1ms) begin transaction
378
+  (0.1ms) rollback transaction
379
+  (0.1ms) begin transaction
380
+  (0.1ms) rollback transaction
381
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
382
+  (0.1ms) begin transaction
383
+  (0.1ms) rollback transaction
384
+  (0.1ms) begin transaction
385
+  (0.1ms) rollback transaction
386
+  (0.1ms) begin transaction
387
+  (0.1ms) rollback transaction
388
+  (0.1ms) begin transaction
389
+  (0.0ms) rollback transaction
390
+  (0.1ms) begin transaction
391
+  (0.1ms) rollback transaction
392
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
393
+  (0.1ms) begin transaction
394
+  (0.1ms) rollback transaction
395
+  (0.1ms) begin transaction
396
+  (0.1ms) rollback transaction
397
+  (0.0ms) begin transaction
398
+  (0.1ms) rollback transaction
399
+  (0.1ms) begin transaction
400
+  (0.1ms) rollback transaction
401
+  (0.1ms) begin transaction
402
+  (0.0ms) rollback transaction
403
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
404
+  (0.1ms) begin transaction
405
+  (0.1ms) rollback transaction
406
+  (0.1ms) begin transaction
407
+  (0.1ms) rollback transaction
408
+  (0.1ms) begin transaction
409
+  (0.1ms) rollback transaction
410
+  (0.1ms) begin transaction
411
+  (0.1ms) rollback transaction
412
+  (0.1ms) begin transaction
413
+  (0.1ms) rollback transaction
414
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
415
+  (0.1ms) begin transaction
416
+  (0.1ms) rollback transaction
417
+  (0.1ms) begin transaction
418
+  (0.1ms) rollback transaction
419
+  (0.1ms) begin transaction
420
+  (0.1ms) rollback transaction
421
+  (0.1ms) begin transaction
422
+  (0.1ms) rollback transaction
423
+  (0.1ms) begin transaction
424
+  (0.1ms) rollback transaction
425
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
426
+  (0.1ms) begin transaction
427
+  (0.1ms) rollback transaction
428
+  (0.1ms) begin transaction
429
+  (0.0ms) rollback transaction
430
+  (0.1ms) begin transaction
431
+  (0.1ms) rollback transaction
432
+  (0.1ms) begin transaction
433
+  (0.0ms) rollback transaction
434
+  (0.1ms) begin transaction
435
+  (0.1ms) rollback transaction
436
+  (0.0ms) begin transaction
437
+  (0.0ms) rollback transaction
438
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
439
+  (0.1ms) begin transaction
440
+  (0.1ms) rollback transaction
441
+  (0.1ms) begin transaction
442
+  (0.1ms) rollback transaction
443
+  (0.1ms) begin transaction
444
+  (0.1ms) rollback transaction
445
+  (0.1ms) begin transaction
446
+  (0.1ms) rollback transaction
447
+  (0.1ms) begin transaction
448
+  (0.1ms) rollback transaction
449
+  (0.0ms) begin transaction
450
+  (0.0ms) rollback transaction
451
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
452
+  (0.1ms) begin transaction
453
+  (0.1ms) rollback transaction
454
+  (0.1ms) begin transaction
455
+  (0.1ms) rollback transaction
456
+  (0.1ms) begin transaction
457
+  (0.1ms) rollback transaction
458
+  (0.0ms) begin transaction
459
+  (0.0ms) rollback transaction
460
+  (0.0ms) begin transaction
461
+  (0.0ms) rollback transaction
462
+  (0.0ms) begin transaction
463
+  (0.1ms) rollback transaction
464
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
465
+  (0.1ms) begin transaction
466
+  (0.1ms) rollback transaction
467
+  (0.1ms) begin transaction
468
+  (0.0ms) rollback transaction
469
+  (0.1ms) begin transaction
470
+  (0.1ms) rollback transaction
471
+  (0.1ms) begin transaction
472
+  (0.1ms) rollback transaction
473
+  (0.1ms) begin transaction
474
+  (0.1ms) rollback transaction
475
+  (0.1ms) begin transaction
476
+  (0.1ms) rollback transaction
477
+  (0.1ms) begin transaction
478
+  (0.0ms) rollback transaction
479
+  (0.1ms) begin transaction
480
+  (0.1ms) rollback transaction
481
+  (0.0ms) begin transaction
482
+  (0.0ms) rollback transaction
483
+  (0.0ms) begin transaction
484
+  (0.0ms) rollback transaction
485
+  (0.0ms) begin transaction
486
+  (0.0ms) rollback transaction
487
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
488
+  (0.1ms) begin transaction
489
+  (0.1ms) rollback transaction
490
+  (0.1ms) begin transaction
491
+  (0.1ms) rollback transaction
492
+  (0.1ms) begin transaction
493
+  (0.1ms) rollback transaction
494
+  (0.1ms) begin transaction
495
+  (0.1ms) rollback transaction
496
+  (0.1ms) begin transaction
497
+  (0.1ms) rollback transaction
498
+  (0.1ms) begin transaction
499
+  (0.1ms) rollback transaction
500
+  (0.0ms) begin transaction
501
+  (0.0ms) rollback transaction
502
+  (0.1ms) begin transaction
503
+  (0.0ms) rollback transaction
504
+  (0.1ms) begin transaction
505
+  (0.1ms) rollback transaction
506
+  (0.1ms) begin transaction
507
+  (0.0ms) rollback transaction
508
+  (0.0ms) begin transaction
509
+  (0.1ms) rollback transaction
510
+  (0.0ms) begin transaction
511
+  (0.1ms) rollback transaction
512
+  (0.1ms) begin transaction
513
+  (0.0ms) rollback transaction
514
+  (0.1ms) begin transaction
515
+  (0.1ms) rollback transaction
516
+  (0.1ms) begin transaction
517
+  (0.1ms) rollback transaction
518
+  (0.1ms) begin transaction
519
+  (0.1ms) rollback transaction
520
+  (0.1ms) begin transaction
521
+  (0.1ms) rollback transaction
522
+  (0.1ms) begin transaction
523
+  (0.1ms) rollback transaction
524
+  (0.1ms) begin transaction
525
+  (0.1ms) rollback transaction
526
+  (0.1ms) begin transaction
527
+  (0.1ms) rollback transaction
528
+  (0.1ms) begin transaction
529
+  (0.1ms) rollback transaction
530
+  (0.1ms) begin transaction
531
+  (0.1ms) rollback transaction
532
+  (0.1ms) begin transaction
533
+  (0.0ms) rollback transaction
534
+  (0.1ms) begin transaction
535
+  (0.1ms) rollback transaction
536
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
537
+  (0.1ms) begin transaction
538
+  (0.0ms) rollback transaction
539
+  (0.1ms) begin transaction
540
+  (0.0ms) rollback transaction
541
+  (0.1ms) begin transaction
542
+  (0.1ms) rollback transaction
543
+  (0.1ms) begin transaction
544
+  (0.1ms) rollback transaction
545
+  (0.1ms) begin transaction
546
+  (0.1ms) rollback transaction
547
+  (0.1ms) begin transaction
548
+  (0.0ms) rollback transaction
549
+  (0.1ms) begin transaction
550
+  (0.0ms) rollback transaction
551
+  (0.0ms) begin transaction
552
+  (0.0ms) rollback transaction
553
+  (0.0ms) begin transaction
554
+  (0.1ms) rollback transaction
555
+  (0.0ms) begin transaction
556
+  (0.0ms) rollback transaction
557
+  (0.1ms) begin transaction
558
+  (0.0ms) rollback transaction
559
+  (0.0ms) begin transaction
560
+  (0.0ms) rollback transaction
561
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
562
+  (0.1ms) begin transaction
563
+  (0.1ms) rollback transaction
564
+  (0.1ms) begin transaction
565
+  (0.1ms) rollback transaction
566
+  (0.1ms) begin transaction
567
+  (0.1ms) rollback transaction
568
+  (0.1ms) begin transaction
569
+  (0.2ms) rollback transaction
570
+  (0.1ms) begin transaction
571
+  (0.1ms) rollback transaction
572
+  (0.1ms) begin transaction
573
+  (0.1ms) rollback transaction
574
+  (0.1ms) begin transaction
575
+  (0.1ms) rollback transaction
576
+  (0.1ms) begin transaction
577
+  (0.1ms) rollback transaction
578
+  (0.1ms) begin transaction
579
+  (0.1ms) rollback transaction
580
+  (0.1ms) begin transaction
581
+  (0.1ms) rollback transaction
582
+  (0.0ms) begin transaction
583
+  (0.1ms) rollback transaction
584
+  (0.1ms) begin transaction
585
+  (0.1ms) rollback transaction
586
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
587
+  (0.1ms) begin transaction
588
+  (0.1ms) rollback transaction
589
+  (0.1ms) begin transaction
590
+  (0.1ms) rollback transaction
591
+  (0.1ms) begin transaction
592
+  (0.1ms) rollback transaction
593
+  (0.1ms) begin transaction
594
+  (0.1ms) rollback transaction
595
+  (0.1ms) begin transaction
596
+  (0.1ms) rollback transaction
597
+  (0.1ms) begin transaction
598
+  (0.1ms) rollback transaction
599
+  (0.1ms) begin transaction
600
+  (0.1ms) rollback transaction
601
+  (0.1ms) begin transaction
602
+  (0.1ms) rollback transaction
603
+  (0.1ms) begin transaction
604
+  (0.1ms) rollback transaction
605
+  (0.0ms) begin transaction
606
+  (0.1ms) rollback transaction
607
+  (0.1ms) begin transaction
608
+  (0.1ms) rollback transaction
609
+  (0.1ms) begin transaction
610
+  (0.1ms) rollback transaction
611
+  (0.1ms) begin transaction
612
+  (0.1ms) rollback transaction
613
+  (0.1ms) begin transaction
614
+  (0.1ms) rollback transaction
615
+  (0.1ms) begin transaction
616
+  (0.1ms) rollback transaction
617
+  (0.1ms) begin transaction
618
+  (0.1ms) rollback transaction
619
+  (0.1ms) begin transaction
620
+  (0.0ms) rollback transaction
621
+  (0.1ms) begin transaction
622
+  (0.1ms) rollback transaction
623
+  (0.1ms) begin transaction
624
+  (0.1ms) rollback transaction
625
+  (0.1ms) begin transaction
626
+  (0.1ms) rollback transaction
627
+  (0.1ms) begin transaction
628
+  (0.1ms) rollback transaction
629
+  (0.1ms) begin transaction
630
+  (0.1ms) rollback transaction
631
+  (0.1ms) begin transaction
632
+  (0.1ms) rollback transaction
633
+  (0.1ms) begin transaction
634
+  (0.1ms) rollback transaction
635
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
636
+  (0.1ms) begin transaction
637
+  (0.1ms) rollback transaction
638
+  (0.1ms) begin transaction
639
+  (0.1ms) rollback transaction
640
+  (0.1ms) begin transaction
641
+  (0.1ms) rollback transaction
642
+  (0.1ms) begin transaction
643
+  (0.1ms) rollback transaction
644
+  (0.0ms) begin transaction
645
+  (0.0ms) rollback transaction
646
+  (0.0ms) begin transaction
647
+  (0.1ms) rollback transaction
648
+  (0.1ms) begin transaction
649
+  (0.1ms) rollback transaction
650
+  (0.1ms) begin transaction
651
+  (0.1ms) rollback transaction
652
+  (0.1ms) begin transaction
653
+  (0.1ms) rollback transaction
654
+  (0.1ms) begin transaction
655
+  (0.1ms) rollback transaction
656
+  (0.1ms) begin transaction
657
+  (0.1ms) rollback transaction
658
+  (0.1ms) begin transaction
659
+  (0.1ms) rollback transaction
660
+  (0.1ms) begin transaction
661
+  (0.1ms) rollback transaction
662
+  (0.1ms) begin transaction
663
+  (0.1ms) rollback transaction
664
+  (0.1ms) begin transaction
665
+  (0.1ms) rollback transaction
666
+  (0.1ms) begin transaction
667
+  (0.1ms) rollback transaction
668
+  (0.1ms) begin transaction
669
+  (0.1ms) rollback transaction
670
+  (0.1ms) begin transaction
671
+  (0.1ms) rollback transaction
672
+  (0.1ms) begin transaction
673
+  (0.1ms) rollback transaction
674
+  (0.1ms) begin transaction
675
+  (0.1ms) rollback transaction
676
+  (0.1ms) begin transaction
677
+  (0.1ms) rollback transaction
678
+  (0.0ms) begin transaction
679
+  (0.1ms) rollback transaction
680
+  (0.1ms) begin transaction
681
+  (0.1ms) rollback transaction
682
+  (0.1ms) begin transaction
683
+  (0.0ms) rollback transaction
684
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
685
+  (0.1ms) begin transaction
686
+  (0.1ms) rollback transaction
687
+  (0.1ms) begin transaction
688
+  (0.1ms) rollback transaction
689
+  (0.1ms) begin transaction
690
+  (0.1ms) rollback transaction
691
+  (0.1ms) begin transaction
692
+  (0.1ms) rollback transaction
693
+  (0.1ms) begin transaction
694
+  (0.1ms) rollback transaction
695
+  (0.1ms) begin transaction
696
+  (0.1ms) rollback transaction
697
+  (0.0ms) begin transaction
698
+  (0.0ms) rollback transaction
699
+  (0.1ms) begin transaction
700
+  (0.1ms) rollback transaction
701
+  (0.1ms) begin transaction
702
+  (0.1ms) rollback transaction
703
+  (0.1ms) begin transaction
704
+  (0.1ms) rollback transaction
705
+  (0.0ms) begin transaction
706
+  (0.0ms) rollback transaction
707
+  (0.1ms) begin transaction
708
+  (0.1ms) rollback transaction
709
+  (0.1ms) begin transaction
710
+  (0.1ms) rollback transaction
711
+  (0.0ms) begin transaction
712
+  (0.1ms) rollback transaction
713
+  (0.1ms) begin transaction
714
+  (0.1ms) rollback transaction
715
+  (0.1ms) begin transaction
716
+  (0.1ms) rollback transaction
717
+  (0.0ms) begin transaction
718
+  (0.1ms) rollback transaction
719
+  (0.1ms) begin transaction
720
+  (0.1ms) rollback transaction
721
+  (0.1ms) begin transaction
722
+  (0.1ms) rollback transaction
723
+  (0.1ms) begin transaction
724
+  (0.1ms) rollback transaction
725
+  (0.1ms) begin transaction
726
+  (0.1ms) rollback transaction
727
+  (0.1ms) begin transaction
728
+  (0.1ms) rollback transaction
729
+  (0.1ms) begin transaction
730
+  (0.1ms) rollback transaction
731
+  (0.0ms) begin transaction
732
+  (0.1ms) rollback transaction
@@ -47,6 +47,15 @@ def clear_user
47
47
  end
48
48
 
49
49
  if User.instance_variable_defined?(:@_attr_enums)
50
- User.remove_instance_variable(:@_attr_enums)
50
+ User.send(:remove_instance_variable, :@_attr_enums)
51
+ end
52
+ end
53
+
54
+ def default_user_roles
55
+ clear_user
56
+
57
+ User.class_eval do
58
+ extend RailsAttrEnum
59
+ attr_enum :role, :admin, :editor, :author, :user
51
60
  end
52
61
  end
@@ -0,0 +1,95 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'User::Role.to_h' do
4
+ let(:enum) { User::Role }
5
+
6
+ before { default_user_roles }
7
+
8
+ let :only_value do
9
+ {
10
+ 'ADMIN' => 0,
11
+ 'EDITOR' => 1,
12
+ 'AUTHOR' => 2,
13
+ 'USER' => 3
14
+ }
15
+ end
16
+
17
+ let :value_and_label do
18
+ {
19
+ 'ADMIN' => { value: 0, label: 'Admin' },
20
+ 'EDITOR' => { value: 1, label: 'Editor' },
21
+ 'AUTHOR' => { value: 2, label: 'Author' },
22
+ 'USER' => { value: 3, label: 'User' }
23
+ }
24
+ end
25
+
26
+ let :all_keys do
27
+ {
28
+ 'ADMIN' => { key: :admin, label: 'Admin', value: 0 },
29
+ 'EDITOR' => { key: :editor, label: 'Editor', value: 1 },
30
+ 'AUTHOR' => { key: :author, label: 'Author', value: 2 },
31
+ 'USER' => { key: :user, label: 'User', value: 3}
32
+ }
33
+ end
34
+
35
+ context 'without options' do
36
+ subject { enum.to_h }
37
+ it { should eq all_keys }
38
+ end
39
+
40
+ context 'with option `only` as a symbol' do
41
+ subject { enum.to_h(only: :value) }
42
+ it { should eq only_value }
43
+ end
44
+
45
+ context 'with option `only` as an array' do
46
+ context 'with no elements' do
47
+ subject { enum.to_h(only: []) }
48
+ it { should eq all_keys }
49
+ end
50
+
51
+ context 'with one element' do
52
+ subject { enum.to_h(only: [:value]) }
53
+ it { should eq only_value }
54
+ end
55
+
56
+ context 'with more than one element' do
57
+ subject { enum.to_h(only: [:label, :value]) }
58
+ it { should eq value_and_label }
59
+ end
60
+
61
+ context 'with an unknown key' do
62
+ it 'should raise an error' do
63
+ expect { enum.to_h(only: :foo) }.to raise_error('Unknown keys for enum')
64
+ end
65
+ end
66
+ end
67
+
68
+ context 'with option `except` as a symbol' do
69
+ subject { enum.to_h(except: :key) }
70
+ it { should eq value_and_label }
71
+ end
72
+
73
+ context 'with option `except` as an array' do
74
+ context 'with no elements' do
75
+ subject { enum.to_h(except: []) }
76
+ it { should eq all_keys }
77
+ end
78
+
79
+ context 'with one element' do
80
+ subject { enum.to_h(except: [:key]) }
81
+ it { should eq value_and_label }
82
+ end
83
+
84
+ context 'with more than one element' do
85
+ subject { enum.to_h(except: [:label, :key]) }
86
+ it { should eq only_value }
87
+ end
88
+
89
+ context 'with an unknown key' do
90
+ it 'should raise an error' do
91
+ expect { enum.to_h(except: :foo) }.to raise_error('Unknown keys for enum')
92
+ end
93
+ end
94
+ end
95
+ end
metadata CHANGED
@@ -1,41 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_attr_enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Fairbank
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-24 00:00:00.000000000 Z
11
+ date: 2013-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ! '>='
18
18
  - !ruby/object:Gem::Version
19
19
  version: 3.2.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ! '>='
25
25
  - !ruby/object:Gem::Version
26
26
  version: 3.2.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: sqlite3
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ! '>='
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ! '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  description: Create enums for Rails model attributes like enums in C languages
@@ -53,6 +53,7 @@ files:
53
53
  - lib/rails_attr_enum/version.rb
54
54
  - lib/rails_attr_enum.rb
55
55
  - lib/tasks/rails_attr_enum_tasks.rake
56
+ - CHANGELOG.md
56
57
  - LICENSE
57
58
  - Rakefile
58
59
  - README.md
@@ -97,6 +98,7 @@ files:
97
98
  - test/dummy/spec/json_spec.rb
98
99
  - test/dummy/spec/models/user_spec.rb
99
100
  - test/dummy/spec/spec_helper.rb
101
+ - test/dummy/spec/to_h_spec.rb
100
102
  homepage: https://github.com/jfairbank/rails_attr_enum
101
103
  licenses:
102
104
  - MIT
@@ -107,12 +109,12 @@ require_paths:
107
109
  - lib
108
110
  required_ruby_version: !ruby/object:Gem::Requirement
109
111
  requirements:
110
- - - '>='
112
+ - - ! '>='
111
113
  - !ruby/object:Gem::Version
112
114
  version: '0'
113
115
  required_rubygems_version: !ruby/object:Gem::Requirement
114
116
  requirements:
115
- - - '>='
117
+ - - ! '>='
116
118
  - !ruby/object:Gem::Version
117
119
  version: '0'
118
120
  requirements: []
@@ -163,3 +165,4 @@ test_files:
163
165
  - test/dummy/spec/json_spec.rb
164
166
  - test/dummy/spec/models/user_spec.rb
165
167
  - test/dummy/spec/spec_helper.rb
168
+ - test/dummy/spec/to_h_spec.rb