Module: ActiveSupport::XmlMini
Overview
XmlMini
To use the much faster libxml parser:
gem 'libxml-ruby', '=0.9.7'
XmlMini.backend = 'LibXML'
Defined Under Namespace
Modules: FileLike
Constant Summary collapse
- DEFAULT_ENCODINGS =
{ "binary" => "base64" }
- TYPE_NAMES =
{ "Symbol" => "symbol", "Integer" => "integer", "BigDecimal" => "decimal", "Float" => "float", "TrueClass" => "boolean", "FalseClass" => "boolean", "Date" => "date", "DateTime" => "dateTime", "Time" => "dateTime", "Array" => "array", "Hash" => "hash" }
- FORMATTING =
{ "symbol" => Proc.new { |symbol| symbol.to_s }, "date" => Proc.new { |date| date.to_s(:db) }, "dateTime" => Proc.new { |time| time.xmlschema }, "binary" => Proc.new { |binary| ::Base64.encode64(binary) }, "yaml" => Proc.new { |yaml| yaml.to_yaml } }
- PARSING =
{ "symbol" => Proc.new { |symbol| symbol.to_s.to_sym }, "date" => Proc.new { |date| ::Date.parse(date) }, "datetime" => Proc.new { |time| Time.xmlschema(time).utc rescue ::DateTime.parse(time).utc }, "integer" => Proc.new { |integer| integer.to_i }, "float" => Proc.new { |float| float.to_f }, "decimal" => Proc.new do |number| if String === number begin BigDecimal(number) rescue ArgumentError BigDecimal(number.to_f.to_s) end else BigDecimal(number) end end, "boolean" => Proc.new { |boolean| %w(1 true).include?(boolean.to_s.strip) }, "string" => Proc.new { |string| string.to_s }, "yaml" => Proc.new { |yaml| YAML.load(yaml) rescue yaml }, "base64Binary" => Proc.new { |bin| ::Base64.decode64(bin) }, "binary" => Proc.new { |bin, entity| _parse_binary(bin, entity) }, "file" => Proc.new { |file, entity| _parse_file(file, entity) } }
Instance Attribute Summary collapse
-
#depth ⇒ Object
Returns the value of attribute depth.
Instance Method Summary collapse
- #backend ⇒ Object
- #backend=(name) ⇒ Object
- #rename_key(key, options = {}) ⇒ Object
- #to_tag(key, value, options) ⇒ Object
- #with_backend(name) ⇒ Object
Instance Attribute Details
#depth ⇒ Object
Returns the value of attribute depth.
98 99 100 |
# File 'lib/active_support/xml_mini.rb', line 98 def depth @depth end |
Instance Method Details
#backend ⇒ Object
103 104 105 |
# File 'lib/active_support/xml_mini.rb', line 103 def backend current_thread_backend || @backend end |
#backend=(name) ⇒ Object
107 108 109 110 111 |
# File 'lib/active_support/xml_mini.rb', line 107 def backend=(name) backend = name && cast_backend_name_to_module(name) self.current_thread_backend = backend if current_thread_backend @backend = backend end |
#rename_key(key, options = {}) ⇒ Object
154 155 156 157 158 159 160 161 162 |
# File 'lib/active_support/xml_mini.rb', line 154 def rename_key(key, = {}) camelize = [:camelize] dasherize = !.has_key?(:dasherize) || [:dasherize] if camelize key = true == camelize ? key.camelize : key.camelize(camelize) end key = _dasherize(key) if dasherize key end |
#to_tag(key, value, options) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/active_support/xml_mini.rb', line 121 def to_tag(key, value, ) type_name = .delete(:type) = .merge(root: key, skip_instruct: true) if value.is_a?(::Method) || value.is_a?(::Proc) if value.arity == 1 value.call() else value.call(, key.to_s.singularize) end elsif value.respond_to?(:to_xml) value.to_xml() else type_name ||= TYPE_NAMES[value.class.name] type_name ||= value.class.name if value && !value.respond_to?(:to_str) type_name = type_name.to_s if type_name type_name = "dateTime" if type_name == "datetime" key = rename_key(key.to_s, ) attributes = [:skip_types] || type_name.nil? ? {} : { type: type_name } attributes[:nil] = true if value.nil? encoding = [:encoding] || DEFAULT_ENCODINGS[type_name] attributes[:encoding] = encoding if encoding formatted_value = FORMATTING[type_name] && !value.nil? ? FORMATTING[type_name].call(value) : value [:builder].tag!(key, formatted_value, attributes) end end |
#with_backend(name) ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/active_support/xml_mini.rb', line 113 def with_backend(name) old_backend = current_thread_backend self.current_thread_backend = name && cast_backend_name_to_module(name) yield ensure self.current_thread_backend = old_backend end |