power_enum 1.1.1 → 1.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data.tar.gz.sig +0 -0
- data/lib/active_record/virtual_enumerations.rb +16 -13
- data/lib/generators/enum/enum_generator.rb +3 -21
- data/lib/generators/enum/enum_generator_helpers/migration_number.rb +27 -0
- data/lib/generators/virtual_enumerations_initializer/templates/virtual_enumerations.rb.erb +1 -1
- data/lib/power_enum.rb +0 -2
- metadata +4 -3
- metadata.gz.sig +0 -0
data.tar.gz.sig
CHANGED
Binary file
|
@@ -65,21 +65,23 @@ module ActiveRecord # :nodoc:
|
|
65
65
|
# Patches Module#const_missing to enable us to dynamically create enum
|
66
66
|
# classes at runtime.
|
67
67
|
def self.patch_const_lookup
|
68
|
+
# Make sure we haven't patched Module already
|
69
|
+
unless ::Module.respond_to?(:enumerations_original_const_missing)
|
70
|
+
# patch Module to support VirtualEnumerations
|
71
|
+
::Module.module_eval do
|
72
|
+
|
73
|
+
alias_method :enumerations_original_const_missing, :const_missing
|
74
|
+
|
75
|
+
# Override const_missing to see if VirtualEnumerations can create it.
|
76
|
+
def const_missing(const_id)
|
77
|
+
# let rails have a go at loading it
|
78
|
+
enumerations_original_const_missing(const_id)
|
79
|
+
rescue NameError
|
80
|
+
# now it's our turn
|
81
|
+
ActiveRecord::VirtualEnumerations.synthesize_if_defined(const_id) or raise
|
82
|
+
end
|
68
83
|
|
69
|
-
# patch Module to support VirtualEnumerations
|
70
|
-
::Module.module_eval do
|
71
|
-
|
72
|
-
alias_method :enumerations_original_const_missing, :const_missing
|
73
|
-
|
74
|
-
# Override const_missing to see if VirtualEnumerations can create it.
|
75
|
-
def const_missing(const_id)
|
76
|
-
# let rails have a go at loading it
|
77
|
-
enumerations_original_const_missing(const_id)
|
78
|
-
rescue NameError
|
79
|
-
# now it's our turn
|
80
|
-
ActiveRecord::VirtualEnumerations.synthesize_if_defined(const_id) or raise
|
81
84
|
end
|
82
|
-
|
83
85
|
end
|
84
86
|
|
85
87
|
end
|
@@ -89,6 +91,7 @@ module ActiveRecord # :nodoc:
|
|
89
91
|
# each enum or enums with a given set of options.
|
90
92
|
def self.define
|
91
93
|
raise ArgumentError, "#{self.name}: must pass a block to define()" unless block_given?
|
94
|
+
patch_const_lookup
|
92
95
|
config = ActiveRecord::VirtualEnumerations::Config.new
|
93
96
|
yield config
|
94
97
|
@config = config # we only overwrite config if no exceptions were thrown
|
@@ -3,6 +3,9 @@
|
|
3
3
|
|
4
4
|
# Generator for PowerEnum
|
5
5
|
class EnumGenerator < Rails::Generators::Base
|
6
|
+
require File.expand_path('../enum_generator_helpers/migration_number', __FILE__)
|
7
|
+
include EnumGeneratorHelpers::MigrationNumber
|
8
|
+
|
6
9
|
source_root File.expand_path('../templates', __FILE__)
|
7
10
|
argument :enum_name, :type => :string
|
8
11
|
class_option :migration, :type => :boolean, :default => true, :desc => 'Generate migration for the enum'
|
@@ -35,27 +38,6 @@ class EnumGenerator < Rails::Generators::Base
|
|
35
38
|
file_name.camelize
|
36
39
|
end
|
37
40
|
|
38
|
-
# Returns the number of the last migration.
|
39
|
-
def current_migration_number
|
40
|
-
dirname = "#{Rails.root}/db/migrate/[0-9]*_*.rb"
|
41
|
-
Dir.glob(dirname).collect do |file|
|
42
|
-
File.basename(file).split("_").first.to_i
|
43
|
-
end.max.to_i
|
44
|
-
end
|
45
|
-
|
46
|
-
# Returns the next upcoming migration number. Sadly, Rails has no API for
|
47
|
-
# this, so we're reduced to copying from ActiveRecord::Generators::Migration
|
48
|
-
def next_migration_number
|
49
|
-
# Lifted directly from ActiveRecord::Generators::Migration
|
50
|
-
# Unfortunately, no API is provided by Rails at this time.
|
51
|
-
next_migration_number = current_migration_number + 1
|
52
|
-
if ActiveRecord::Base.timestamped_migrations
|
53
|
-
[Time.now.utc.strftime("%Y%m%d%H%M%S"), "%.14d" % next_migration_number].max
|
54
|
-
else
|
55
|
-
"%.3d" % next_migration_number
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
41
|
# Derives the name for the migration, something like 'create_enum_fruit'
|
60
42
|
def migration_name
|
61
43
|
"create_enum_#{file_name}"
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module EnumGeneratorHelpers
|
2
|
+
# Helper methods to figure out the migration number.
|
3
|
+
module MigrationNumber
|
4
|
+
|
5
|
+
# Returns the number of the last migration.
|
6
|
+
def current_migration_number
|
7
|
+
dirname = "#{Rails.root}/db/migrate/[0-9]*_*.rb"
|
8
|
+
Dir.glob(dirname).collect do |file|
|
9
|
+
File.basename(file).split("_").first.to_i
|
10
|
+
end.max.to_i
|
11
|
+
end
|
12
|
+
|
13
|
+
# Returns the next upcoming migration number. Sadly, Rails has no API for
|
14
|
+
# this, so we're reduced to copying from ActiveRecord::Generators::Migration
|
15
|
+
def next_migration_number
|
16
|
+
# Lifted directly from ActiveRecord::Generators::Migration
|
17
|
+
# Unfortunately, no API is provided by Rails at this time.
|
18
|
+
next_migration_number = current_migration_number + 1
|
19
|
+
if ActiveRecord::Base.timestamped_migrations
|
20
|
+
[Time.now.utc.strftime("%Y%m%d%H%M%S"), "%.14d" % next_migration_number].max
|
21
|
+
else
|
22
|
+
"%.3d" % next_migration_number
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
data/lib/power_enum.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: power_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -40,7 +40,7 @@ cert_chain:
|
|
40
40
|
amZaYUtWM2wwYTFkR2wyUVNMa2pScFFaRDFiRCtVSDdnT3F5N1piZGNzUkJM
|
41
41
|
NEg3VTV6VQpibEtkZEg2dXhDckRTTTdLYWJrelNPVmYKLS0tLS1FTkQgQ0VS
|
42
42
|
VElGSUNBVEUtLS0tLQo=
|
43
|
-
date: 2013-02-
|
43
|
+
date: 2013-02-24 00:00:00.000000000 Z
|
44
44
|
dependencies:
|
45
45
|
- !ruby/object:Gem::Dependency
|
46
46
|
name: rails
|
@@ -144,6 +144,7 @@ files:
|
|
144
144
|
- lib/active_record/virtual_enumerations.rb
|
145
145
|
- lib/generators/enum/USAGE
|
146
146
|
- lib/generators/enum/enum_generator.rb
|
147
|
+
- lib/generators/enum/enum_generator_helpers/migration_number.rb
|
147
148
|
- lib/generators/enum/templates/model.rb.erb
|
148
149
|
- lib/generators/enum/templates/rails31_migration.rb.erb
|
149
150
|
- lib/generators/virtual_enumerations_initializer/USAGE
|
@@ -172,7 +173,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
172
173
|
version: '0'
|
173
174
|
segments:
|
174
175
|
- 0
|
175
|
-
hash: -
|
176
|
+
hash: -4143040860677823590
|
176
177
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
177
178
|
none: false
|
178
179
|
requirements:
|
metadata.gz.sig
CHANGED
Binary file
|