acts_as_constant 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README +35 -0
  2. data/lib/acts_as_constant.rb +4 -1
  3. metadata +2 -2
data/README CHANGED
@@ -0,0 +1,35 @@
1
+ This dynamic method will create a set of constants for the class/model from the table it’s associated with. The model class must have a table with two columns ( name = “constant name”, id = “constant id”).
2
+
3
+
4
+ These constants can be accessed by class methods. Ex: Role.editor() will get the instance created from the row with name of “editor”
5
+
6
+
7
+ Usage in Model class:
8
+
9
+ acts_as_constant # default constant name will be in the table's column 'name'
10
+ acts_as_constant :fooby # constant name will be in the table's column 'fooby'
11
+
12
+
13
+ Usage by client:
14
+
15
+ Role.get(2) - Get the Role instance created from row 2 in the roles table.
16
+
17
+ Role.contributer - Get the Role instance created from the row with a name of "contributer".
18
+
19
+ Implementation
20
+
21
+ Create an array class attribute named _CONSTANTS. ex: ROLE_CONSTANTS
22
+ Create 2 public class methods. ex:
23
+ Role.get(id) – get the Role instance in ROLE_CONSTANTS[id]
24
+ Role.CONSTANTS – get the entire ROLE_CONSTANTS array
25
+ Create an instance of the class for each row in the class’s table.
26
+ Role instance 1 = {“name” => “contributer”, “id” => “1” }
27
+ Role instance 2 = {“name” => “editor”, “id” => “2” }
28
+ Add each of these Role instances to the ROLE_CONSTANT array. The instance will be indexed in the array by the row’s id value.
29
+ ROLE_CONSTANT1 = {“name” => “contributer”, “index” => “1” }
30
+ ROLE_CONSTANT2 = {“name” => “editor”, “index” => “2” }
31
+ Freeze each Role instance.
32
+ Add 2 more public class methods.
33
+ Role.contributer – returns the Role instance in ROLE_CONSTANTS1
34
+ Role.CONTRIBUTER – returns the Role instance in ROLE_CONSTANTS1
35
+ Freeze the array class attribute. ROLE_CONSTANTS
@@ -13,6 +13,7 @@ module ActiveRecord #:nodoc:
13
13
 
14
14
  # let's set up a variable to represent the name of the klass
15
15
  const = self.to_s
16
+ #puts "const = #{const}"
16
17
 
17
18
  # create a constant array to hold the values
18
19
  # create a nice get method to pull from the constant array
@@ -32,7 +33,9 @@ module ActiveRecord #:nodoc:
32
33
  }
33
34
 
34
35
  # let's build methods for everything in the database.
35
- Module.const_get(const).find(:all).each do |rec|
36
+ rows = eval "#{const}.find(:all)"
37
+
38
+ rows.each do |rec|
36
39
  #puts rec.inspect
37
40
  name = rec.send(col)
38
41
 
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.0
7
- date: 2007-02-26 00:00:00 -05:00
6
+ version: 1.0.1
7
+ date: 2007-02-27 00:00:00 -05:00
8
8
  summary: Acts as Constant
9
9
  require_paths:
10
10
  - lib