Class: MetasploitDataModels::Base64Serializer
- Inherits:
-
Object
- Object
- MetasploitDataModels::Base64Serializer
- Defined in:
- lib/metasploit_data_models/base64_serializer.rb
Overview
Provides ActiveRecord 3.1x-friendly serialization for descendants of ApplicationRecord. Backwards compatible with older YAML methods and will fall back to string decoding in the worst case
Constant Summary collapse
- DEFAULT =
The default for #default
{}
- COERCE_DEFAULT =
The default for #coerce
false
- LOADERS =
Deserializers for #load
- Base64 decoding and then unmarshalling the value.
- Parsing the value as YAML.
- The raw value.
[ lambda { |serialized| marshaled = serialized.unpack('m').first # Load the unpacked Marshal object first Marshal.load(marshaled) }, lambda { |serialized| # Support legacy YAML encoding for existing data YAML.safe_load(serialized, permitted_classes: Rails.application.config.active_record.yaml_column_permitted_classes) }, lambda { |serialized| # Fall back to string decoding serialized } ]
Instance Attribute Summary collapse
-
#coerce ⇒ Object
writeonly
Sets the attribute coerce.
-
#default ⇒ Object
Creates a duplicate of default value.
Instance Method Summary collapse
-
#coerce_object(value) ⇒ Object
Recursively coerce the object that has been passed in, keeping primitive types as their original type, while changing objects that cannot be serialized into a string representation of the object data.
-
#dump(value) ⇒ String
Serializes the value by marshalling the value and then base64 encodes the marshaled value.
-
#initialize(attributes = {}) ⇒ Base64Serializer
constructor
A new instance of Base64Serializer.
-
#load(value) ⇒ Object
Deserializes the value by either 1.
Constructor Details
#initialize(attributes = {}) ⇒ Base64Serializer
Returns a new instance of Base64Serializer.
88 89 90 91 92 93 |
# File 'lib/metasploit_data_models/base64_serializer.rb', line 88 def initialize(attributes={}) attributes.assert_valid_keys(:default, :coerce) @default = attributes.fetch(:default, DEFAULT) @coerce = attributes.fetch(:coerce, COERCE_DEFAULT) end |
Instance Attribute Details
#coerce=(value) ⇒ Object (writeonly)
Sets the attribute coerce
54 55 56 |
# File 'lib/metasploit_data_models/base64_serializer.rb', line 54 def coerce=(value) @coerce = value end |
#default ⇒ Object
Creates a duplicate of default value
49 50 51 |
# File 'lib/metasploit_data_models/base64_serializer.rb', line 49 def default @default.dup end |
Instance Method Details
#coerce_object(value) ⇒ Object
Recursively coerce the object that has been passed in, keeping primitive types as their original type, while changing objects that cannot be serialized into a string representation of the object data.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/metasploit_data_models/base64_serializer.rb', line 58 def coerce_object(value) case value when Hash value.transform_values { |v| coerce_object(v) } when Array value.map { |v| coerce_object(v) } when File, IO value.inspect when String, Integer, Float, TrueClass, FalseClass, NilClass, Symbol value else value.to_s end end |
#dump(value) ⇒ String
Serializes the value by marshalling the value and then base64 encodes the marshaled value.
77 78 79 80 81 82 83 84 |
# File 'lib/metasploit_data_models/base64_serializer.rb', line 77 def dump(value) # Always store data back in the Marshal format to_serialize = @coerce ? coerce_object(value) : value marshalled = Marshal.dump(to_serialize) base64_encoded = [ marshalled ].pack('m') base64_encoded end |
#load(value) ⇒ Object
Deserializes the value by either
- Base64 decoding and then unmarshalling the value.
- Parsing the value as YAML.
- Returns the raw value.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/metasploit_data_models/base64_serializer.rb', line 104 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 |