Class: Moneta::Transforms::Base64
- Inherits:
-
Moneta::Transform
- Object
- Moneta::Transform
- Moneta::Transforms::Base64
- Defined in:
- lib/moneta/transforms/base64.rb
Overview
Encodes text as Base64 using the stdlib base64
library
Direct Known Subclasses
Instance Method Summary collapse
-
#decode(value) ⇒ String
Decode from Base64.
-
#encode(value) ⇒ String
Encode string to Base64.
-
#initialize(url_safe: false, strict: false, padding: true, **options) ⇒ Base64
constructor
A new instance of Base64.
Methods inherited from Moneta::Transform
#decodable?, delegate_to, #method_missing, #respond_to_missing?
Constructor Details
#initialize(url_safe: false, strict: false, padding: true, **options) ⇒ Base64
Returns a new instance of Base64.
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/moneta/transforms/base64.rb', line 12 def initialize(url_safe: false, strict: false, padding: true, **) super raise "Cannot use strict and url_safe together" if url_safe && strict raise "padding: false is only valid with url_safe: true" if !padding && !url_safe @url_safe = url_safe @strict = strict @padding = padding end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Moneta::Transform
Instance Method Details
#decode(value) ⇒ String
Decode from Base64
41 42 43 44 45 46 47 48 49 |
# File 'lib/moneta/transforms/base64.rb', line 41 def decode(value) if @url_safe ::Base64.urlsafe_decode64(value) elsif @strict ::Base64.strict_decode64(value) else ::Base64.decode64(value) end end |
#encode(value) ⇒ String
Encode string to Base64
27 28 29 30 31 32 33 34 35 |
# File 'lib/moneta/transforms/base64.rb', line 27 def encode(value) if @url_safe ::Base64.urlsafe_encode64(value, padding: @padding) elsif @strict ::Base64.strict_encode64(value) else ::Base64.encode64(value) end end |