rivsc-xmlattributes 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1 @@
1
+ gem 'rails', '3.0.0'
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Sylvain Claudel
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWAR
data/README ADDED
@@ -0,0 +1,9 @@
1
+ This gem allow to access to xml attribute in rails3
2
+
3
+ = Xml Attributes
4
+
5
+ == Changelog
6
+
7
+ 1.0.0 - Gem xml attributes with patch https://rails.lighthouseapp.com/projects/8994/tickets/1598-preserve-xml-attributes-with-hashfrom_xml-and-activeresource
8
+
9
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 3.0.6
@@ -0,0 +1,69 @@
1
+ class Hash
2
+
3
+ def self.from_xml(xml, options = {})
4
+ typecast_xml_value(unrename_keys(ActiveSupport::XmlMini.parse(xml)), options)
5
+ end
6
+
7
+ private
8
+
9
+ def self.typecast_xml_value(value, options = {}) #+
10
+
11
+ options.symbolize_keys! #+
12
+ options.reverse_merge!(:preserve_attributes => false) #+
13
+
14
+ case value.class.to_s
15
+ when 'Hash'
16
+ if value['type'] == 'array'
17
+ _, entries = Array.wrap(value.detect { |k,v| k != 'type' })
18
+ if entries.nil? || (c = value['__content__'] && c.blank?)
19
+ []
20
+ else
21
+ case entries.class.to_s # something weird with classes not matching here. maybe singleton methods breaking is_a?
22
+ when "Array"
23
+ entries.collect { |v| typecast_xml_value(v, options) } #+
24
+ when "Hash"
25
+ [typecast_xml_value(entries, options)] #+
26
+ else
27
+ raise "can't typecast #{entries.inspect}"
28
+ end
29
+ end
30
+ elsif value.has_key?("__content__")
31
+ content = value["__content__"]
32
+ if parser = ActiveSupport::XmlMini::PARSING[value["type"]]
33
+ parser.arity == 1 ? parser.call(content) : parser.call(content, value)
34
+ elsif options[:preserve_attributes] && value.keys.size > 1 #+
35
+ value["content"] = value.delete("__content__") #+
36
+ value #+
37
+ else
38
+ content
39
+ end
40
+ elsif value['type'] == 'string' && value['nil'] != 'true'
41
+ ""
42
+ # blank or nil parsed values are represented by nil
43
+ elsif value.blank? || value['nil'] == 'true'
44
+ nil
45
+ # If the type is the only element which makes it then
46
+ # this still makes the value nil, except if type is
47
+ # a XML node(where type['value'] is a Hash)
48
+ elsif value['type'] && value.size == 1 && !value['type'].is_a?(::Hash)
49
+ nil
50
+ else
51
+ xml_value = value.inject({}) do |h,(k,v)|
52
+ h[k] = typecast_xml_value(v, options)
53
+ h
54
+ end
55
+
56
+ # Turn { :files => { :file => #<StringIO> } into { :files => #<StringIO> } so it is compatible with
57
+ # how multipart uploaded files from HTML appear
58
+ xml_value["file"].is_a?(StringIO) ? xml_value["file"] : xml_value
59
+ end
60
+ when 'Array'
61
+ value.map! { |i| typecast_xml_value(i, options) }
62
+ value.length > 1 ? value : value.first
63
+ when 'String'
64
+ value
65
+ else
66
+ raise "can't typecast #{value.class.name} - #{value.inspect}"
67
+ end
68
+ end
69
+ end
@@ -0,0 +1 @@
1
+ require 'patch/patch.rb'
@@ -0,0 +1,28 @@
1
+ require 'rubygems'
2
+
3
+ spec = Gem::Specification.new do |s|
4
+ ## package information
5
+ s.name = 'rivsc-xmlattributes'
6
+ s.author = 'Sylvain Claudel'
7
+ s.email = "claudel.sylvain@gmail.com"
8
+ s.version = ("$Release: 1.0.0" =~ /[\.\d]+/) && $&
9
+ s.platform = Gem::Platform::RUBY
10
+ s.homepage = 'http://github.com/rivsc/xmlattributes'
11
+ s.rubyforge_project = 'rivsc-xmlattributes'
12
+ s.summary = "add xml attributes in Hash.from_xml Rails3"
13
+ s.description = <<-END
14
+ add xml attributes in Hash.from_xml Rails3
15
+ END
16
+
17
+ ## files
18
+ files = []
19
+ files += Dir.glob('lib/**/*')
20
+ files += %w[Gemfile LICENSE README VERSION rivsc-xmlattributes.gemspec]
21
+ s.files = files.delete_if { |path| path =~ /\.svn/ }
22
+ s.files = files
23
+ end
24
+
25
+ if $0 == __FILE__
26
+ Gem::manage_gems
27
+ Gem::Builder.new(spec).build
28
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rivsc-xmlattributes
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
10
+ platform: ruby
11
+ authors:
12
+ - Sylvain Claudel
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-10-14 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: " add xml attributes in Hash.from_xml Rails3\n"
22
+ email: claudel.sylvain@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - lib/rivsc-xmlattributes.rb
31
+ - lib/patch/patch.rb
32
+ - Gemfile
33
+ - LICENSE
34
+ - README
35
+ - VERSION
36
+ - rivsc-xmlattributes.gemspec
37
+ has_rdoc: true
38
+ homepage: http://github.com/rivsc/xmlattributes
39
+ licenses: []
40
+
41
+ post_install_message:
42
+ rdoc_options: []
43
+
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ segments:
52
+ - 0
53
+ version: "0"
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ requirements: []
63
+
64
+ rubyforge_project: rivsc-xmlattributes
65
+ rubygems_version: 1.3.7
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: add xml attributes in Hash.from_xml Rails3
69
+ test_files: []
70
+