socrata 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/ext.rb +142 -0
- metadata +3 -2
data/lib/ext.rb
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
class String
|
2
|
+
def underscore
|
3
|
+
self.to_s.gsub(/::/, '/').
|
4
|
+
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
5
|
+
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
6
|
+
tr("-", "_").
|
7
|
+
downcase
|
8
|
+
end
|
9
|
+
|
10
|
+
def camelize(leading_caps = true)
|
11
|
+
s = self.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
|
12
|
+
s = leading_caps ? s : s[0...1].downcase + s[1..-1]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class Util
|
17
|
+
def self.symbolize_keys(obj)
|
18
|
+
case obj
|
19
|
+
when Array
|
20
|
+
obj.inject([]){|res, val|
|
21
|
+
res << case val
|
22
|
+
when Hash, Array
|
23
|
+
symbolize_keys(val)
|
24
|
+
else
|
25
|
+
val
|
26
|
+
end
|
27
|
+
res
|
28
|
+
}
|
29
|
+
when Hash
|
30
|
+
obj.inject({}){|res, (key, val)|
|
31
|
+
nkey = case key
|
32
|
+
when String
|
33
|
+
key.to_sym
|
34
|
+
else
|
35
|
+
key
|
36
|
+
end
|
37
|
+
nval = case val
|
38
|
+
when Hash, Array
|
39
|
+
symbolize_keys(val)
|
40
|
+
else
|
41
|
+
val
|
42
|
+
end
|
43
|
+
res[nkey] = nval
|
44
|
+
res
|
45
|
+
}
|
46
|
+
else
|
47
|
+
obj
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.camelize_keys(obj)
|
52
|
+
case obj
|
53
|
+
when Array
|
54
|
+
obj.inject([]){|res, val|
|
55
|
+
res << case val
|
56
|
+
when Hash, Array
|
57
|
+
camelize_keys(val)
|
58
|
+
else
|
59
|
+
val
|
60
|
+
end
|
61
|
+
res
|
62
|
+
}
|
63
|
+
when Hash
|
64
|
+
obj.inject({}){|res, (key, val)|
|
65
|
+
nkey = case key
|
66
|
+
when String
|
67
|
+
key.camelize
|
68
|
+
when Symbol
|
69
|
+
key.to_s.camelize(false)
|
70
|
+
else
|
71
|
+
key
|
72
|
+
end
|
73
|
+
nval = case val
|
74
|
+
when Hash, Array
|
75
|
+
camelize_keys(val)
|
76
|
+
else
|
77
|
+
val
|
78
|
+
end
|
79
|
+
res[nkey] = nval
|
80
|
+
res
|
81
|
+
}
|
82
|
+
else
|
83
|
+
obj
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
__END__
|
89
|
+
|
90
|
+
class Hash
|
91
|
+
def recursive_camelize(leading_caps = false)
|
92
|
+
inject({}) do |acc, (k,v)|
|
93
|
+
k = k.to_s.camelize(leading_caps)
|
94
|
+
|
95
|
+
case v
|
96
|
+
when Hash
|
97
|
+
v = v.recursive_camelize
|
98
|
+
when Array
|
99
|
+
v.inject([]) do |res, val|
|
100
|
+
res << case val
|
101
|
+
when Hash
|
102
|
+
val.recursive_camelize(leading_caps)
|
103
|
+
else
|
104
|
+
val
|
105
|
+
end
|
106
|
+
res
|
107
|
+
end
|
108
|
+
end
|
109
|
+
acc[k] = v
|
110
|
+
acc
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
__END__
|
115
|
+
|
116
|
+
def symbolize_keys
|
117
|
+
inject({}) do |acc, (k,v)|
|
118
|
+
key = String === k ? k.to_sym : k
|
119
|
+
value = Hash === v ? v.symbolize_keys : v
|
120
|
+
acc[key] = value
|
121
|
+
acc
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
class Hash
|
126
|
+
def recursive_symbolize_keys!
|
127
|
+
symbolize_keys!
|
128
|
+
# symbolize each hash in .values
|
129
|
+
values.each{|h| h.recursive_symbolize_keys! if h.is_a?(Hash) }
|
130
|
+
# symbolize each hash inside an array in .values
|
131
|
+
values.select{|v| v.is_a?(Array) }.flatten.each{|h| h.recursive_symbolize_keys! if h.is_a?(Hash) }
|
132
|
+
self
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
|
137
|
+
180: if first_letter_in_uppercase
|
138
|
+
181: lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
|
139
|
+
182: else
|
140
|
+
183: lower_case_and_underscored_word.first.downcase + camelize(lower_case_and_underscored_word)[1..-1]
|
141
|
+
184: end
|
142
|
+
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 2
|
8
|
-
-
|
9
|
-
version: 0.2.
|
8
|
+
- 1
|
9
|
+
version: 0.2.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Chris Metcalf
|
@@ -70,6 +70,7 @@ extra_rdoc_files: []
|
|
70
70
|
|
71
71
|
files:
|
72
72
|
- lib/socrata.rb
|
73
|
+
- lib/ext.rb
|
73
74
|
- lib/socrata/data.rb
|
74
75
|
- lib/socrata/user.rb
|
75
76
|
has_rdoc: true
|