table-name-to-class 0.0.2 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/table-name-to-class.rb +25 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 62c84a19f2ea2438eb46054d82703b4cf9a23148
|
4
|
+
data.tar.gz: abe7e707df9b27b9b7d36036f56d76eda2c9469d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f8607a8c8e1cb413b522c545daa201ec14dafe2c604b71380d777ece9a4bd182b2f2b8d6b6c1e8009c1be5da286bfaf3405e82f453ded0bd37a64a7096880193
|
7
|
+
data.tar.gz: 9a634a6d2b5417a961115218228ef0848bd9d7015b586ff19ff86538c030858086b53d3ab0b24a1ed68b48a874d2cac83d9286f3c1f25a32a9c8e06abff53b65
|
data/lib/table-name-to-class.rb
CHANGED
@@ -1,17 +1,41 @@
|
|
1
1
|
class TableNameToClass
|
2
2
|
@@conversion_hash = nil
|
3
3
|
|
4
|
+
##
|
5
|
+
# Convert a table name into a class constant (or nil if no constant for that
|
6
|
+
# table)
|
7
|
+
# ---
|
8
|
+
# Expects:
|
9
|
+
# name (object like string):: Name of database table, this can be anything
|
10
|
+
# that returns a valid table name when hit with
|
11
|
+
# to_s
|
12
|
+
# force_update (boolean):: Should the table/class correspondence be updated?
|
13
|
+
# Any none-nil value triggers a reload.
|
14
|
+
#
|
15
|
+
# Returns:
|
16
|
+
# nil if the lookup fails, the constant for the class associated with the
|
17
|
+
# table otherwise.
|
4
18
|
def self.convert(name, force_update = nil)
|
5
19
|
init_hash unless @@conversion_hash || force_update
|
6
|
-
@@conversion_hash[name]
|
20
|
+
@@conversion_hash[name.to_s]
|
7
21
|
end
|
8
22
|
|
23
|
+
##
|
24
|
+
# Debug helper
|
25
|
+
# ---
|
26
|
+
# Returns the hash of table name and class constant pairs
|
9
27
|
def self.debug
|
10
28
|
return @@conversion_hash
|
11
29
|
end
|
12
30
|
|
13
31
|
private
|
14
32
|
|
33
|
+
## :doc:
|
34
|
+
# Loads the correspondence cache
|
35
|
+
# ----
|
36
|
+
# Recursively visits every .rb file in app/models and requires them all. This
|
37
|
+
# is required to avoid failures when trying to convert the name of a table
|
38
|
+
# who's model has not yet been loaded.
|
15
39
|
def self.init_hash
|
16
40
|
# Expanded from idea posted here: http://stackoverflow.com/a/6150228
|
17
41
|
|