Updated README.rdoc again
[feedcatcher.git] / vendor / rails / activesupport / lib / active_support / core_ext / hash / conversions.rb
1 require 'date'
2
3 module ActiveSupport #:nodoc:
4 module CoreExtensions #:nodoc:
5 module Hash #:nodoc:
6 module Conversions
7 # This module exists to decorate files deserialized using Hash.from_xml with
8 # the <tt>original_filename</tt> and <tt>content_type</tt> methods.
9 module FileLike #:nodoc:
10 attr_writer :original_filename, :content_type
11
12 def original_filename
13 @original_filename || 'untitled'
14 end
15
16 def content_type
17 @content_type || 'application/octet-stream'
18 end
19 end
20
21 XML_TYPE_NAMES = {
22 "Symbol" => "symbol",
23 "Fixnum" => "integer",
24 "Bignum" => "integer",
25 "BigDecimal" => "decimal",
26 "Float" => "float",
27 "TrueClass" => "boolean",
28 "FalseClass" => "boolean",
29 "Date" => "date",
30 "DateTime" => "datetime",
31 "Time" => "datetime",
32 "ActiveSupport::TimeWithZone" => "datetime"
33 } unless defined?(XML_TYPE_NAMES)
34
35 XML_FORMATTING = {
36 "symbol" => Proc.new { |symbol| symbol.to_s },
37 "date" => Proc.new { |date| date.to_s(:db) },
38 "datetime" => Proc.new { |time| time.xmlschema },
39 "binary" => Proc.new { |binary| ActiveSupport::Base64.encode64(binary) },
40 "yaml" => Proc.new { |yaml| yaml.to_yaml }
41 } unless defined?(XML_FORMATTING)
42
43 # TODO: use Time.xmlschema instead of Time.parse;
44 # use regexp instead of Date.parse
45 unless defined?(XML_PARSING)
46 XML_PARSING = {
47 "symbol" => Proc.new { |symbol| symbol.to_sym },
48 "date" => Proc.new { |date| ::Date.parse(date) },
49 "datetime" => Proc.new { |time| ::Time.parse(time).utc rescue ::DateTime.parse(time).utc },
50 "integer" => Proc.new { |integer| integer.to_i },
51 "float" => Proc.new { |float| float.to_f },
52 "decimal" => Proc.new { |number| BigDecimal(number) },
53 "boolean" => Proc.new { |boolean| %w(1 true).include?(boolean.strip) },
54 "string" => Proc.new { |string| string.to_s },
55 "yaml" => Proc.new { |yaml| YAML::load(yaml) rescue yaml },
56 "base64Binary" => Proc.new { |bin| ActiveSupport::Base64.decode64(bin) },
57 "file" => Proc.new do |file, entity|
58 f = StringIO.new(ActiveSupport::Base64.decode64(file))
59 f.extend(FileLike)
60 f.original_filename = entity['name']
61 f.content_type = entity['content_type']
62 f
63 end
64 }
65
66 XML_PARSING.update(
67 "double" => XML_PARSING["float"],
68 "dateTime" => XML_PARSING["datetime"]
69 )
70 end
71
72 def self.included(klass)
73 klass.extend(ClassMethods)
74 end
75
76 # Converts a hash into a string suitable for use as a URL query string. An optional <tt>namespace</tt> can be
77 # passed to enclose the param names (see example below).
78 #
79 # ==== Examples
80 # { :name => 'David', :nationality => 'Danish' }.to_query # => "name=David&nationality=Danish"
81 #
82 # { :name => 'David', :nationality => 'Danish' }.to_query('user') # => "user%5Bname%5D=David&user%5Bnationality%5D=Danish"
83 def to_query(namespace = nil)
84 collect do |key, value|
85 value.to_query(namespace ? "#{namespace}[#{key}]" : key)
86 end.sort * '&'
87 end
88
89 alias_method :to_param, :to_query
90
91 def to_xml(options = {})
92 require 'builder' unless defined?(Builder)
93
94 options[:indent] ||= 2
95 options.reverse_merge!({ :builder => Builder::XmlMarkup.new(:indent => options[:indent]),
96 :root => "hash" })
97 options[:builder].instruct! unless options.delete(:skip_instruct)
98 root = rename_key(options[:root].to_s, options)
99
100 options[:builder].__send__(:method_missing, root) do
101 each do |key, value|
102 case value
103 when ::Hash
104 value.to_xml(options.merge({ :root => key, :skip_instruct => true }))
105 when ::Array
106 value.to_xml(options.merge({ :root => key, :children => key.to_s.singularize, :skip_instruct => true}))
107 when ::Method, ::Proc
108 # If the Method or Proc takes two arguments, then
109 # pass the suggested child element name. This is
110 # used if the Method or Proc will be operating over
111 # multiple records and needs to create an containing
112 # element that will contain the objects being
113 # serialized.
114 if 1 == value.arity
115 value.call(options.merge({ :root => key, :skip_instruct => true }))
116 else
117 value.call(options.merge({ :root => key, :skip_instruct => true }), key.to_s.singularize)
118 end
119 else
120 if value.respond_to?(:to_xml)
121 value.to_xml(options.merge({ :root => key, :skip_instruct => true }))
122 else
123 type_name = XML_TYPE_NAMES[value.class.name]
124
125 key = rename_key(key.to_s, options)
126
127 attributes = options[:skip_types] || value.nil? || type_name.nil? ? { } : { :type => type_name }
128 if value.nil?
129 attributes[:nil] = true
130 end
131
132 options[:builder].tag!(key,
133 XML_FORMATTING[type_name] ? XML_FORMATTING[type_name].call(value) : value,
134 attributes
135 )
136 end
137 end
138 end
139
140 yield options[:builder] if block_given?
141 end
142
143 end
144
145 def rename_key(key, options = {})
146 camelize = options.has_key?(:camelize) && options[:camelize]
147 dasherize = !options.has_key?(:dasherize) || options[:dasherize]
148 key = key.camelize if camelize
149 dasherize ? key.dasherize : key
150 end
151
152 module ClassMethods
153 def from_xml(xml)
154 typecast_xml_value(unrename_keys(XmlMini.parse(xml)))
155 end
156
157 private
158 def typecast_xml_value(value)
159 case value.class.to_s
160 when 'Hash'
161 if value['type'] == 'array'
162 child_key, entries = value.detect { |k,v| k != 'type' } # child_key is throwaway
163 if entries.nil? || (c = value['__content__'] && c.blank?)
164 []
165 else
166 case entries.class.to_s # something weird with classes not matching here. maybe singleton methods breaking is_a?
167 when "Array"
168 entries.collect { |v| typecast_xml_value(v) }
169 when "Hash"
170 [typecast_xml_value(entries)]
171 else
172 raise "can't typecast #{entries.inspect}"
173 end
174 end
175 elsif value.has_key?("__content__")
176 content = value["__content__"]
177 if parser = XML_PARSING[value["type"]]
178 if parser.arity == 2
179 XML_PARSING[value["type"]].call(content, value)
180 else
181 XML_PARSING[value["type"]].call(content)
182 end
183 else
184 content
185 end
186 elsif value['type'] == 'string' && value['nil'] != 'true'
187 ""
188 # blank or nil parsed values are represented by nil
189 elsif value.blank? || value['nil'] == 'true'
190 nil
191 # If the type is the only element which makes it then
192 # this still makes the value nil, except if type is
193 # a XML node(where type['value'] is a Hash)
194 elsif value['type'] && value.size == 1 && !value['type'].is_a?(::Hash)
195 nil
196 else
197 xml_value = value.inject({}) do |h,(k,v)|
198 h[k] = typecast_xml_value(v)
199 h
200 end
201
202 # Turn { :files => { :file => #<StringIO> } into { :files => #<StringIO> } so it is compatible with
203 # how multipart uploaded files from HTML appear
204 xml_value["file"].is_a?(StringIO) ? xml_value["file"] : xml_value
205 end
206 when 'Array'
207 value.map! { |i| typecast_xml_value(i) }
208 case value.length
209 when 0 then nil
210 when 1 then value.first
211 else value
212 end
213 when 'String'
214 value
215 else
216 raise "can't typecast #{value.class.name} - #{value.inspect}"
217 end
218 end
219
220 def unrename_keys(params)
221 case params.class.to_s
222 when "Hash"
223 params.inject({}) do |h,(k,v)|
224 h[k.to_s.underscore.tr("-", "_")] = unrename_keys(v)
225 h
226 end
227 when "Array"
228 params.map { |v| unrename_keys(v) }
229 else
230 params
231 end
232 end
233 end
234 end
235 end
236 end
237 end