Module: XmlMini

Extended by:
Forwardable, XmlMini
Included in:
XmlMini
Defined in:
lib/xml_mini.rb,
lib/xml_mini/version.rb

Defined Under Namespace

Modules: FileLike

Constant Summary collapse

DEFAULT_ENCODINGS =
{
    "binary" => "base64"
}
TYPE_NAMES =
{
    "Symbol" => "symbol",
    "Fixnum" => "integer",
    "Bignum" => "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_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 { |number| BigDecimal(number) },
    "boolean" => Proc.new { |boolean| %w(1 true).include?(boolean.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) }
}
VERSION =
"0.1.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#backendObject

Returns the value of attribute backend.



75
76
77
# File 'lib/xml_mini.rb', line 75

def backend
  @backend
end

Instance Method Details

#rename_key(key, options = {}) ⇒ Object



127
128
129
130
131
# File 'lib/xml_mini.rb', line 127

def rename_key(key, options = {})
  dasherize = !options.has_key?(:dasherize) || options[:dasherize]
  key = _dasherize(key) if dasherize
  key
end

#to_tag(key, value, options) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/xml_mini.rb', line 94

def to_tag(key, value, options)
  type_name = options.delete(:type)
  merged_options = options.merge(:root => key, :skip_instruct => true)

  if value.is_a?(::Method) || value.is_a?(::Proc)
    if value.arity == 1
      value.call(merged_options)
    else
      value.call(merged_options, key.to_s.singularize)
    end
  elsif value.respond_to?(:to_xml)
    value.to_xml(merged_options)
  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, options)

    attributes = options[:skip_types] || type_name.nil? ? {} : {:type => type_name}
    attributes[:nil] = true if value.nil?

    encoding = options[:encoding] || DEFAULT_ENCODINGS[type_name]
    attributes[:encoding] = encoding if encoding

    formatted_value = FORMATTING[type_name] && !value.nil? ?
        FORMATTING[type_name].call(value) : value

    options[:builder].tag!(key, formatted_value, attributes)
  end
end

#with_backend(name) ⇒ Object



87
88
89
90
91
92
# File 'lib/xml_mini.rb', line 87

def with_backend(name)
  old_backend, self.backend = backend, name
  yield
ensure
  self.backend = old_backend
end