Skip to content

Instantly share code, notes, and snippets.

@btoews
Last active June 3, 2019 15:30
Show Gist options
  • Save btoews/ac77d0aba7a3e42873395de21484ed91 to your computer and use it in GitHub Desktop.
Save btoews/ac77d0aba7a3e42873395de21484ed91 to your computer and use it in GitHub Desktop.
encoded attribute example
module EncodedAttr
ENCODINGS = %i(raw b64 url_b64)
module ClassMethods
def encoded_attr(name, default_encoding:, encodings: ENCODINGS)
raise ArgumentError unless ENCODINGS.include?(default_encoding)
encodings.each do |encoding|
if encoding == default_encoding
alias_method "#{encoding}_#{name}", name
alias_method "#{encoding}_#{name}=", "#{name}="
else
define_method("#{encoding}_#{name}") do
encode_val(decode_val(send(name), default_encoding), encoding)
end
define_method("#{encoding}_#{name}=") do |new_val|
send("#{name}=", encode_val(decode_val(new_val, encoding), default_encoding))
end
end
end
end
end
def self.included(base_class)
base_class.extend(ClassMethods)
end
def encode_val(value, encoding)
case encoding
when :raw
value
when :b64
Base64.strict_encode64(value)
when :url_b64
Base64.urlsafe_encode64(value)
end
end
def decode_val(value, encoding)
case encoding
when :raw
value
when :b64
Base64.strict_decode64(value)
when :url_b64
Base64.urlsafe_decode64(value)
end
end
end
require "base64"
class TestClass
include EncodedAttr
attr_accessor :one, :two, :three
encoded_attr :one, default_encoding: :raw
encoded_attr :two, default_encoding: :b64
encoded_attr :three, default_encoding: :url_b64
end
def assert_equal(a, b)
raise "expected '#{a}'=='#{b}'" unless a == b
end
x = TestClass.new
x.raw_one = "one"
assert_equal x.one, "one"
assert_equal x.raw_one, "one"
assert_equal x.b64_one, "b25l"
assert_equal x.url_b64_one, "b25l"
x.b64_one = "b25l"
assert_equal x.one, "one"
assert_equal x.raw_one, "one"
assert_equal x.b64_one, "b25l"
assert_equal x.url_b64_one, "b25l"
x.url_b64_one = "b25l"
assert_equal x.one, "one"
assert_equal x.raw_one, "one"
assert_equal x.b64_one, "b25l"
assert_equal x.url_b64_one, "b25l"
x.raw_two = "two"
assert_equal x.two, "dHdv"
assert_equal x.raw_two, "two"
assert_equal x.b64_two, "dHdv"
assert_equal x.url_b64_two, "dHdv"
x.b64_two = "dHdv"
assert_equal x.two, "dHdv"
assert_equal x.raw_two, "two"
assert_equal x.b64_two, "dHdv"
assert_equal x.url_b64_two, "dHdv"
x.url_b64_two = "dHdv"
assert_equal x.two, "dHdv"
assert_equal x.raw_two, "two"
assert_equal x.b64_two, "dHdv"
assert_equal x.url_b64_two, "dHdv"
x.raw_three = "three"
assert_equal x.three, "dGhyZWU="
assert_equal x.raw_three, "three"
assert_equal x.b64_three, "dGhyZWU="
assert_equal x.url_b64_three, "dGhyZWU="
x.b64_three = "dGhyZWU="
assert_equal x.three, "dGhyZWU="
assert_equal x.raw_three, "three"
assert_equal x.b64_three, "dGhyZWU="
assert_equal x.url_b64_three, "dGhyZWU="
x.url_b64_three = "dGhyZWU="
assert_equal x.three, "dGhyZWU="
assert_equal x.raw_three, "three"
assert_equal x.b64_three, "dGhyZWU="
assert_equal x.url_b64_three, "dGhyZWU="
puts "ok"
#!/bin/bash
ruby -I. -rencoded_attr test.rb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment