class MetasploitDataModels::Base64Serializer
Provides ActiveRecord 3.1x-friendly serialization for descendants of ActiveRecord::Base. Backwards compatible with older YAML methods and will fall back to string decoding in the worst case
@example Using default default of {}
serialize :foo, MetasploitDataModels::Base64Serializer.new
@example Overriding default to []
serialize :bar, MetasploitDataModels::Base64Serializer.new(:default => [])
Constants
- DEFAULT
The default for {#default}
- LOADERS
Deserializers for {#load}
-
Base64 decoding and then unmarshalling the value.
-
Parsing the value as YAML.
-
The raw value.
-
Attributes
Public Class Methods
@param attributes [Hash] attributes @option attributes [Object] :default ({}) Value to use for {#default}.
# File lib/metasploit_data_models/base64_serializer.rb, line 65 def initialize(attributes={}) attributes.assert_valid_keys(:default) @default = attributes.fetch(:default, DEFAULT) end
Public Instance Methods
Creates a duplicate of default value
@return
# File lib/metasploit_data_models/base64_serializer.rb, line 45 def default @default.dup end
Serializes the value by marshalling the value and then base64 encodes the marshaled value.
@param value [Object] value to serialize @return [String]
# File lib/metasploit_data_models/base64_serializer.rb, line 55 def dump(value) # Always store data back in the Marshal format marshalled = Marshal.dump(value) base64_encoded = [ marshalled ].pack('m') base64_encoded end
Deserializes the value by either
-
Base64 decoding and then unmarshalling the value.
-
Parsing the value as YAML.
-
Returns the raw value.
@param value [String] serialized value @return [Object]
@see default
# File lib/metasploit_data_models/base64_serializer.rb, line 80 def load(value) loaded = nil if value.blank? loaded = default else LOADERS.each do |loader| begin loaded = loader.call(value) rescue next else break end end end loaded end