lib/apricoteatsgorilla.rb in smacks-apricoteatsgorilla-0.2.7 vs lib/apricoteatsgorilla.rb in smacks-apricoteatsgorilla-0.2.8
- old
+ new
@@ -77,10 +77,11 @@
else
key, value = child.name, xml_node_to_hash(child)
end
key = remove_namespace(key)
+ key = to_snake_case(key)
current = this_node[key]
case current
when Array
this_node[key] << value
when nil
@@ -115,28 +116,32 @@
end
end
# Returns a sorted version of a given Hash in case :sort_keys is enabled.
def opt_order(hash)
- if sort_keys
- hash.sort_by{ |kv| kv.first }
- else
- hash
- end
+ return hash unless sort_keys
+ hash.sort_by { |kv| kv.first }
end
- # Helper to remove line breaks and whitespace between XML tags.
+ # Removes line breaks and whitespace between XML tags.
def clean_xml(xml)
xml = xml.gsub(/\n+/, "")
- xml = xml.gsub(/(>)\s*(<)/, '\1\2')
+ xml.gsub(/(>)\s*(<)/, '\1\2')
end
- # Helper to remove namespaces from XML tags.
+ # Removes namespaces from XML tags.
def remove_namespace(tag)
tag.sub(/.+:(.+)/, '\1')
end
- # Helper to convert "true" and "false" strings to boolean values.
+ # Converts CamelCase and lowerCamelCase to snake_case.
+ def to_snake_case(string)
+ string = string.gsub(/[A-Z]+/, '\1_\0').downcase
+ string = string[1, string.length-1] if string[0, 1] == "_"
+ string
+ end
+
+ # Converts "true" and "false" strings to boolean values.
# Returns the original string in case it doesn't match "true" or "false".
def booleanize(string)
return true if string == "true"
return false if string == "false"
string
\ No newline at end of file