Class: ActiveRecord::XmlSerializer::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/serializers/xml_serializer.rb

Overview

:nodoc:

Direct Known Subclasses

MethodAttribute

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, record) ⇒ Attribute

Returns a new instance of Attribute.



245
246
247
248
249
250
# File 'lib/active_record/serializers/xml_serializer.rb', line 245

def initialize(name, record)
  @name, @record = name, record

  @type  = compute_type
  @value = compute_value
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



243
244
245
# File 'lib/active_record/serializers/xml_serializer.rb', line 243

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



243
244
245
# File 'lib/active_record/serializers/xml_serializer.rb', line 243

def type
  @type
end

#valueObject (readonly)

Returns the value of attribute value.



243
244
245
# File 'lib/active_record/serializers/xml_serializer.rb', line 243

def value
  @value
end

Instance Method Details

#decorations(include_types = true) ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/active_record/serializers/xml_serializer.rb', line 265

def decorations(include_types = true)
  decorations = {}

  if type == :binary
    decorations[:encoding] = 'base64'
  end

  if include_types && type != :string
    decorations[:type] = type
  end

  if value.nil?
    decorations[:nil] = true
  end

  decorations
end

#needs_encoding?Boolean

There is a significant speed improvement if the value does not need to be escaped, as #tag! escapes all values to ensure that valid XML is generated. For known binary values, it is at least an order of magnitude faster to Base64 encode binary values and directly put them in the output XML than to pass the original value or the Base64 encoded value to the #tag! method. It definitely makes no sense to Base64 encode the value and then give it to #tag!, since that just adds additional overhead.

Returns:

  • (Boolean)


261
262
263
# File 'lib/active_record/serializers/xml_serializer.rb', line 261

def needs_encoding?
  ![ :binary, :date, :datetime, :boolean, :float, :integer ].include?(type)
end