lib/apricoteatsgorilla.rb in smacks-apricoteatsgorilla-0.3.7 vs lib/apricoteatsgorilla.rb in smacks-apricoteatsgorilla-0.3.8
- old
+ new
@@ -54,10 +54,16 @@
attr_accessor :disable_hash_keys_to_snake_case
# Flag to disable conversion of Hash keys to Symbols.
attr_accessor :disable_hash_keys_to_symbol
+ # Array of XML nodes to add a namespace to.
+ attr_accessor :nodes_to_namespace
+
+ # The namespace for nodes in :nodes_to_namespace.
+ attr_accessor :node_namespace
+
# Shortcut method for translating between XML Strings and Ruby Hashes.
# Delegates to xml_to_hash in case +source+ is of type String or delegates
# to hash_to_xml in case +source+ is of type Hash. Returns nil otherwise.
#
# ==== Parameters
@@ -164,10 +170,22 @@
yield if block_given?
end
end
end
+ # Converts a given +string+ from CamelCase/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 a given +string+ from snake_case to lowerCamelCase.
+ def to_lower_camel_case(string)
+ string.to_s.gsub(/_(.)/) { $1.upcase }
+ end
+
private
# Actual implementation for xml_to_hash. Takes and iterates through a given
# Hpricot +element+ and returns a Ruby Hash equal to the given content.
#
@@ -238,10 +256,13 @@
#
# * +name+ - The name of the XML tag.
# * +attributes+ - Optional. Hash of attributes for the XML tag.
def tag(name, attributes = {})
name = to_lower_camel_case(name) unless disable_tag_names_to_lower_camel_case
+ if nodes_to_namespace.kind_of? Array
+ name = "#{node_namespace}:#{name}" if node_namespace && nodes_to_namespace.include?(name)
+ end
return "<#{name} />" unless block_given?
attr = opt_order(attributes).map { |k, v| %Q( xmlns:#{k}="#{v}") }.to_s
body = (yield && !yield.empty?) ? yield : ""
"<#{name}#{attr}>" << body << "</#{name}>"
@@ -256,21 +277,9 @@
# Removes the namespace from a given XML +tag+.
def remove_namespace(tag)
tag.sub!(/.+:(.+)/, '\1')
tag
- end
-
- # Converts a given +string+ from CamelCase/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 a given +string+ from snake_case to lowerCamelCase.
- def to_lower_camel_case(string)
- string.to_s.gsub(/_(.)/) { $1.upcase }
end
# Checks to see if a given +string+ matches "true" or "false" and converts
# these values to actual Boolean objects. Returns the original string in
# case it does not match "true" or "false".
\ No newline at end of file