Sha256: dccfee7468c6095239a52d78e1122ef2011f522d293cb81a32c4ae4823d50f43

Contents?: true

Size: 915 Bytes

Versions: 5

Compression:

Stored size: 915 Bytes

Contents

module HaveCode
  module DMCode
    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.get 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/dm_code.rb
bkerley-have-code-0.1.2 lib/have-code/dm_code.rb
bkerley-have-code-0.1.3 lib/have-code/dm_code.rb
have-code-0.1.2 lib/have-code/dm_code.rb
have-code-0.1.3 lib/have-code/dm_code.rb