acts_as_constant 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +0 -0
- data/lib/acts_as_constant.rb +82 -0
- metadata +57 -0
data/README
ADDED
File without changes
|
@@ -0,0 +1,82 @@
|
|
1
|
+
module ActiveRecord #:nodoc:
|
2
|
+
module Acts #:nodoc:
|
3
|
+
|
4
|
+
module Constant
|
5
|
+
def self.included(base) # :nodoc:
|
6
|
+
base.extend ClassMethods
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def acts_as_constant(col = :name)
|
11
|
+
begin
|
12
|
+
#puts self.to_s
|
13
|
+
|
14
|
+
# let's set up a variable to represent the name of the klass
|
15
|
+
const = self.to_s
|
16
|
+
|
17
|
+
# create a constant array to hold the values
|
18
|
+
# create a nice get method to pull from the constant array
|
19
|
+
class_eval %{
|
20
|
+
private
|
21
|
+
#{const.upcase}_CONSTANTS = []
|
22
|
+
|
23
|
+
public
|
24
|
+
def self.get(id)
|
25
|
+
return nil if id.blank?
|
26
|
+
#{const.upcase}_CONSTANTS[id]
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.CONSTANTS
|
30
|
+
#{const.upcase}_CONSTANTS
|
31
|
+
end
|
32
|
+
}
|
33
|
+
|
34
|
+
# let's build methods for everything in the database.
|
35
|
+
Module.const_get(const).find(:all).each do |rec|
|
36
|
+
#puts rec.inspect
|
37
|
+
name = rec.send(col)
|
38
|
+
|
39
|
+
unless name.nil?
|
40
|
+
|
41
|
+
# let's sanitize the name a bit.
|
42
|
+
name = name.downcase
|
43
|
+
name = name.gsub(' ', '_')
|
44
|
+
|
45
|
+
# let's create two methods an all downcase method for accessing the constant and an upper case one
|
46
|
+
class_eval %{
|
47
|
+
# by using .freeze we can prevent the object from being modified.
|
48
|
+
#{const.upcase}_CONSTANTS[#{rec.id}] = rec.freeze
|
49
|
+
|
50
|
+
def self.#{name}
|
51
|
+
#{const.upcase}_CONSTANTS[#{rec.id}]
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.#{name.upcase}
|
55
|
+
#{const.upcase}_CONSTANTS[#{rec.id}]
|
56
|
+
end
|
57
|
+
}
|
58
|
+
end
|
59
|
+
end
|
60
|
+
class_eval %{
|
61
|
+
# by using .freeze we can prevent the constants array from being modified.
|
62
|
+
#{const.upcase}_CONSTANTS.freeze
|
63
|
+
}
|
64
|
+
rescue => ex
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
module ActMethods
|
71
|
+
def self.included(base) # :nodoc:
|
72
|
+
base.extend ClassMethods
|
73
|
+
end
|
74
|
+
|
75
|
+
module ClassMethods
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
ActiveRecord::Base.send :include, ActiveRecord::Acts::Constant
|
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.2
|
3
|
+
specification_version: 1
|
4
|
+
name: acts_as_constant
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 1.0.0
|
7
|
+
date: 2007-02-26 00:00:00 -05:00
|
8
|
+
summary: Acts as Constant
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
- lib
|
12
|
+
email:
|
13
|
+
homepage:
|
14
|
+
rubyforge_project:
|
15
|
+
description:
|
16
|
+
autorequire:
|
17
|
+
- acts_as_constant
|
18
|
+
default_executable:
|
19
|
+
bindir: bin
|
20
|
+
has_rdoc: false
|
21
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
22
|
+
requirements:
|
23
|
+
- - ">"
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: 0.0.0
|
26
|
+
version:
|
27
|
+
platform: ruby
|
28
|
+
signing_key:
|
29
|
+
cert_chain:
|
30
|
+
post_install_message:
|
31
|
+
authors: []
|
32
|
+
|
33
|
+
files:
|
34
|
+
- lib/acts_as_constant.rb
|
35
|
+
- README
|
36
|
+
test_files: []
|
37
|
+
|
38
|
+
rdoc_options: []
|
39
|
+
|
40
|
+
extra_rdoc_files: []
|
41
|
+
|
42
|
+
executables: []
|
43
|
+
|
44
|
+
extensions: []
|
45
|
+
|
46
|
+
requirements: []
|
47
|
+
|
48
|
+
dependencies:
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: rails
|
51
|
+
version_requirement:
|
52
|
+
version_requirements: !ruby/object:Gem::Version::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: 1.2.1
|
57
|
+
version:
|