Skip to content

Commit

Permalink
Removed Hash core addition of to_xml_attributes.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnunemaker committed Sep 17, 2011
1 parent 7e04e25 commit 3461ace
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 17 deletions.
13 changes: 0 additions & 13 deletions lib/crack/core_extensions.rb
Expand Up @@ -12,16 +12,3 @@ def blank?
respond_to?(:empty?) ? empty? : !self
end unless method_defined?(:blank?)
end # class Object

class Hash #:nodoc:
# @return <String> The hash as attributes for an XML tag.
#
# @example
# { :one => 1, "two"=>"TWO" }.to_xml_attributes
# #=> 'one="1" two="TWO"'
def to_xml_attributes
map do |k,v|
%{#{Crack::Util.snake_case(k.to_s).sub(/^(.{1,1})/) { |m| m.downcase }}="#{v.to_s.gsub('"', '&quot;')}"}
end.join(' ')
end
end
6 changes: 6 additions & 0 deletions lib/crack/util.rb
Expand Up @@ -6,6 +6,12 @@ def snake_case(str)
return $+.downcase
end

def to_xml_attributes(hash)
hash.map do |k,v|
%{#{Crack::Util.snake_case(k.to_s).sub(/^(.{1,1})/) { |m| m.downcase }}="#{v.to_s.gsub('"', '&quot;')}"}
end.join(' ')
end

extend self
end
end
2 changes: 1 addition & 1 deletion lib/crack/xml.rb
Expand Up @@ -173,7 +173,7 @@ def inner_html
# @return <String> The HTML node in text form.
def to_html
attributes.merge!(:type => @type ) if @type
"<#{name}#{attributes.to_xml_attributes}>#{@nil_element ? '' : inner_html}</#{name}>"
"<#{name}#{Crack::Util.to_xml_attributes(attributes)}>#{@nil_element ? '' : inner_html}</#{name}>"
end

# @alias #to_html #to_s
Expand Down
6 changes: 3 additions & 3 deletions test/hash_test.rb
Expand Up @@ -7,18 +7,18 @@ class CrackTest < Test::Unit::TestCase
end

should "turn the hash into xml attributes" do
attrs = @hash.to_xml_attributes
attrs = Crack::Util.to_xml_attributes(@hash)
attrs.should =~ /one="ONE"/m
attrs.should =~ /two="TWO"/m
attrs.should =~ /three="it &quot;should&quot; work"/m
end

should 'preserve _ in hash keys' do
attrs = {
attrs = Crack::Util.to_xml_attributes({
:some_long_attribute => "with short value",
:crash => :burn,
:merb => "uses extlib"
}.to_xml_attributes
})

attrs.should =~ /some_long_attribute="with short value"/
attrs.should =~ /merb="uses extlib"/
Expand Down

0 comments on commit 3461ace

Please sign in to comment.