acts_as_constant 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/acts_as_constant.rb +13 -9
- metadata +2 -2
data/lib/acts_as_constant.rb
CHANGED
@@ -12,28 +12,30 @@ module ActiveRecord #:nodoc:
|
|
12
12
|
#puts self.to_s
|
13
13
|
|
14
14
|
# let's set up a variable to represent the name of the klass
|
15
|
-
|
15
|
+
klass = self.to_s
|
16
|
+
#puts "klass = #{klass}"
|
17
|
+
const = klass.upcase.gsub("::", "_")
|
16
18
|
#puts "const = #{const}"
|
17
19
|
|
18
20
|
# create a constant array to hold the values
|
19
21
|
# create a nice get method to pull from the constant array
|
20
22
|
class_eval %{
|
21
23
|
private
|
22
|
-
#{const
|
24
|
+
#{const}_CONSTANTS = []
|
23
25
|
|
24
26
|
public
|
25
27
|
def self.get(id)
|
26
28
|
return nil if id.blank?
|
27
|
-
#{const
|
29
|
+
#{const}_CONSTANTS[id]
|
28
30
|
end
|
29
31
|
|
30
32
|
def self.CONSTANTS
|
31
|
-
#{const
|
33
|
+
#{const}_CONSTANTS
|
32
34
|
end
|
33
35
|
}
|
34
36
|
|
35
37
|
# let's build methods for everything in the database.
|
36
|
-
rows = eval "#{
|
38
|
+
rows = eval "#{klass}.find(:all)"
|
37
39
|
|
38
40
|
rows.each do |rec|
|
39
41
|
#puts rec.inspect
|
@@ -48,23 +50,25 @@ module ActiveRecord #:nodoc:
|
|
48
50
|
# let's create two methods an all downcase method for accessing the constant and an upper case one
|
49
51
|
class_eval %{
|
50
52
|
# by using .freeze we can prevent the object from being modified.
|
51
|
-
#{const
|
53
|
+
#{const}_CONSTANTS[#{rec.id}] = rec.freeze
|
52
54
|
|
53
55
|
def self.#{name}
|
54
|
-
#{const
|
56
|
+
#{const}_CONSTANTS[#{rec.id}]
|
55
57
|
end
|
56
58
|
|
57
59
|
def self.#{name.upcase}
|
58
|
-
#{const
|
60
|
+
#{const}_CONSTANTS[#{rec.id}]
|
59
61
|
end
|
60
62
|
}
|
61
63
|
end
|
62
64
|
end
|
63
65
|
class_eval %{
|
64
66
|
# by using .freeze we can prevent the constants array from being modified.
|
65
|
-
#{const
|
67
|
+
#{const}_CONSTANTS.freeze
|
66
68
|
}
|
67
69
|
rescue => ex
|
70
|
+
puts "Error in acts_as_constant: #{ex.message}"
|
71
|
+
puts ex.backtrace
|
68
72
|
end
|
69
73
|
|
70
74
|
end
|
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-02-
|
6
|
+
version: 1.0.2
|
7
|
+
date: 2007-02-28 00:00:00 -05:00
|
8
8
|
summary: Acts as Constant
|
9
9
|
require_paths:
|
10
10
|
- lib
|