2011-11-10 21:41:20 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
|
2010-04-25 00:33:38 +00:00
|
|
|
## String
|
2011-11-10 21:41:20 +00:00
|
|
|
|
|
|
|
class String #:nodoc
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2011-06-21 20:03:24 +00:00
|
|
|
def permalink
|
|
|
|
self.parameterize('-')
|
2010-04-25 00:33:38 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2011-06-21 20:03:24 +00:00
|
|
|
def permalink!
|
|
|
|
replace(self.permalink)
|
2010-04-30 14:05:53 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2011-06-21 20:03:24 +00:00
|
|
|
alias :parameterize! :permalink!
|
2010-09-23 23:00:13 +00:00
|
|
|
|
2010-07-09 10:38:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
## Hash
|
|
|
|
|
2011-11-10 21:41:20 +00:00
|
|
|
class Hash #:nodoc
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-09 10:38:50 +00:00
|
|
|
def underscore_keys
|
|
|
|
new_hash = {}
|
2010-07-23 20:09:54 +00:00
|
|
|
|
|
|
|
self.each_pair do |key, value|
|
2010-07-09 10:38:50 +00:00
|
|
|
if value.respond_to?(:collect!) # Array
|
|
|
|
value.collect do |item|
|
|
|
|
if item.respond_to?(:each_pair) # Hash item within
|
|
|
|
item.underscore_keys
|
|
|
|
else
|
|
|
|
item
|
|
|
|
end
|
|
|
|
end
|
|
|
|
elsif value.respond_to?(:each_pair) # Hash
|
|
|
|
value = value.underscore_keys
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-09 10:38:50 +00:00
|
|
|
new_key = key.is_a?(String) ? key.underscore : key # only String keys
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-09 10:38:50 +00:00
|
|
|
new_hash[new_key] = value
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-09 10:38:50 +00:00
|
|
|
self.replace(new_hash)
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
|
|
|
end
|
2011-01-18 14:24:42 +00:00
|
|
|
|
2011-11-10 21:41:20 +00:00
|
|
|
class Boolean #:nodoc
|
|
|
|
BOOLEAN_MAP = {
|
|
|
|
true => true, "true" => true, "TRUE" => true, "1" => true, 1 => true, 1.0 => true,
|
|
|
|
false => false, "false" => false, "FALSE" => false, "0" => false, 0 => false, 0.0 => false
|
|
|
|
}
|
|
|
|
|
|
|
|
def self.set(value)
|
|
|
|
value = BOOLEAN_MAP[value]
|
|
|
|
value.nil? ? nil : value
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.get(value)
|
|
|
|
value
|
|
|
|
end
|
|
|
|
end
|
2011-01-18 14:24:42 +00:00
|
|
|
|