acts_as_constant 1.0.4 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/acts_as_constant.rb +34 -26
- metadata +4 -4
data/lib/acts_as_constant.rb
CHANGED
@@ -17,6 +17,10 @@ module ActiveRecord #:nodoc:
|
|
17
17
|
const = klass.upcase.gsub("::", "_")
|
18
18
|
#puts "const = #{const}"
|
19
19
|
|
20
|
+
define_method("constantize") do
|
21
|
+
self.send(col).constantize
|
22
|
+
end
|
23
|
+
|
20
24
|
class_eval %{
|
21
25
|
def self.constant_class_name
|
22
26
|
"#{const}"
|
@@ -47,43 +51,47 @@ module ActiveRecord #:nodoc:
|
|
47
51
|
|
48
52
|
class_eval do
|
49
53
|
def self.reload_constants!
|
50
|
-
|
51
|
-
|
52
|
-
|
54
|
+
begin
|
55
|
+
self.CONSTANTS.clear
|
56
|
+
|
57
|
+
rows = eval "#{self.to_s}.find(:all)"
|
53
58
|
|
54
|
-
|
55
|
-
|
56
|
-
|
59
|
+
rows.each do |rec|
|
60
|
+
#puts rec.inspect
|
61
|
+
name = rec.send(self.constant_column)
|
57
62
|
|
58
|
-
|
63
|
+
unless name.nil?
|
59
64
|
|
60
|
-
|
61
|
-
|
62
|
-
name = name.gsub(' ', '_')
|
65
|
+
# let's sanitize the name a bit.
|
66
|
+
name = name.underscore.gsub("/", '_')
|
63
67
|
|
64
|
-
|
68
|
+
self.instance_eval do
|
65
69
|
|
66
|
-
|
67
|
-
|
68
|
-
|
70
|
+
define_method("constant_name") do
|
71
|
+
self.send(self.constant_column).downcase.gsub(' ', '_')
|
72
|
+
end
|
69
73
|
|
70
|
-
|
74
|
+
end
|
71
75
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
+
# let's create two methods an all downcase method for accessing the constant and an upper case one
|
77
|
+
class_eval %{
|
78
|
+
# by using .freeze we can prevent the object from being modified.
|
79
|
+
#{self.constant_class_name}_CONSTANTS[#{rec.id}] = rec.freeze
|
76
80
|
|
77
|
-
|
78
|
-
|
79
|
-
|
81
|
+
def self.#{name}
|
82
|
+
#{self.constant_class_name}_CONSTANTS[#{rec.id}]
|
83
|
+
end
|
80
84
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
+
def self.#{name.upcase}
|
86
|
+
#{self.constant_class_name}_CONSTANTS[#{rec.id}]
|
87
|
+
end
|
88
|
+
}
|
89
|
+
end
|
85
90
|
end
|
91
|
+
rescue Exception => e
|
92
|
+
|
86
93
|
end
|
94
|
+
|
87
95
|
end
|
88
96
|
end
|
89
97
|
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.2
|
|
3
3
|
specification_version: 1
|
4
4
|
name: acts_as_constant
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 1.0.
|
7
|
-
date: 2007-
|
6
|
+
version: 1.0.5
|
7
|
+
date: 2007-08-30 00:00:00 -04:00
|
8
8
|
summary: Acts as Constant
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -51,7 +51,7 @@ dependencies:
|
|
51
51
|
version_requirement:
|
52
52
|
version_requirements: !ruby/object:Gem::Version::Requirement
|
53
53
|
requirements:
|
54
|
-
- - "
|
54
|
+
- - ">"
|
55
55
|
- !ruby/object:Gem::Version
|
56
|
-
version:
|
56
|
+
version: 0.0.0
|
57
57
|
version:
|