acts_as_constant 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/acts_as_constant.rb +55 -32
- metadata +2 -2
data/lib/acts_as_constant.rb
CHANGED
@@ -17,55 +17,78 @@ module ActiveRecord #:nodoc:
|
|
17
17
|
const = klass.upcase.gsub("::", "_")
|
18
18
|
#puts "const = #{const}"
|
19
19
|
|
20
|
+
class_eval %{
|
21
|
+
def self.constant_class_name
|
22
|
+
"#{const}"
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.constant_column
|
26
|
+
"#{col}"
|
27
|
+
end
|
28
|
+
}
|
29
|
+
|
20
30
|
# create a constant array to hold the values
|
21
31
|
# create a nice get method to pull from the constant array
|
22
32
|
class_eval %{
|
23
33
|
private
|
24
|
-
#{
|
34
|
+
#{self.constant_class_name}_CONSTANTS = []
|
25
35
|
|
26
36
|
public
|
27
37
|
def self.get(id)
|
28
38
|
return nil if id.blank?
|
29
|
-
#{
|
39
|
+
#{self.constant_class_name}_CONSTANTS[id]
|
30
40
|
end
|
31
41
|
|
32
42
|
def self.CONSTANTS
|
33
|
-
#{
|
43
|
+
#{self.constant_class_name}_CONSTANTS
|
34
44
|
end
|
35
|
-
|
36
|
-
|
37
|
-
# let's build methods for everything in the database.
|
38
|
-
rows = eval "#{klass}.find(:all)"
|
45
|
+
|
46
|
+
}
|
39
47
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
unless name.nil?
|
48
|
+
class_eval do
|
49
|
+
def self.reload_constants!
|
50
|
+
self.CONSTANTS.clear
|
45
51
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
52
|
+
rows = eval "#{self.to_s}.find(:all)"
|
53
|
+
|
54
|
+
rows.each do |rec|
|
55
|
+
#puts rec.inspect
|
56
|
+
name = rec.send(self.constant_column)
|
57
|
+
|
58
|
+
unless name.nil?
|
59
|
+
|
60
|
+
# let's sanitize the name a bit.
|
61
|
+
name = name.downcase
|
62
|
+
name = name.gsub(' ', '_')
|
63
|
+
|
64
|
+
self.instance_eval do
|
65
|
+
|
66
|
+
define_method("constant_name") do
|
67
|
+
self.send(self.constant_column).downcase.gsub(' ', '_')
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
# let's create two methods an all downcase method for accessing the constant and an upper case one
|
73
|
+
class_eval %{
|
74
|
+
# by using .freeze we can prevent the object from being modified.
|
75
|
+
#{self.constant_class_name}_CONSTANTS[#{rec.id}] = rec.freeze
|
76
|
+
|
77
|
+
def self.#{name}
|
78
|
+
#{self.constant_class_name}_CONSTANTS[#{rec.id}]
|
79
|
+
end
|
80
|
+
|
81
|
+
def self.#{name.upcase}
|
82
|
+
#{self.constant_class_name}_CONSTANTS[#{rec.id}]
|
83
|
+
end
|
84
|
+
}
|
61
85
|
end
|
62
|
-
|
86
|
+
end
|
63
87
|
end
|
64
88
|
end
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
}
|
89
|
+
|
90
|
+
self.reload_constants!
|
91
|
+
|
69
92
|
rescue => ex
|
70
93
|
#puts "Error in acts_as_constant: #{ex.message}"
|
71
94
|
#puts ex.backtrace
|
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-03-
|
6
|
+
version: 1.0.4
|
7
|
+
date: 2007-03-15 00:00:00 -04:00
|
8
8
|
summary: Acts as Constant
|
9
9
|
require_paths:
|
10
10
|
- lib
|