Sha256: 56b4b02d884b8151513506813f3ca0f3bf92c05aaa1e64d3b3567caf2b00c7f8

Contents?: true

Size: 916 Bytes

Versions: 5

Compression:

Stored size: 916 Bytes

Contents

module HaveCode
  module ARCode
    def self.included(base)
      base.extend ClassMethods
    end

    module ClassMethods
      def have_code(mod, a, b)
        write_inheritable_attribute :have_code_cipher, Affine::Cipher.new(mod, a, b)

        extend OptionalClassMethods
        include InstanceMethods
      end
    end

    module OptionalClassMethods
      def find_by_code(code)
        return nil unless code.is_a? String
        cipher = read_inheritable_attribute(:have_code_cipher)
        candidate_id = cipher.decipher code.to_i(36)
        object = self.find candidate_id
        return object if object.code == code
        return nil
      rescue
        nil
      end
    end

    module InstanceMethods
      def have_code_cipher
        self.class.read_inheritable_attribute :have_code_cipher
      end

      def code
        have_code_cipher.encipher(id).to_s(36)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
bkerley-have-code-0.1.1 lib/have-code/ar_code.rb
bkerley-have-code-0.1.2 lib/have-code/ar_code.rb
bkerley-have-code-0.1.3 lib/have-code/ar_code.rb
have-code-0.1.2 lib/have-code/ar_code.rb
have-code-0.1.3 lib/have-code/ar_code.rb