Sha256: 02f976962eadcb042a9de5cd28a38c5f4be82147f3cf34dfabd063141cbf656f

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

require 'hashman/version'
require 'active_support'

module HashMan
  module Inclusion
    extend ActiveSupport::Concern

    def hash_id
      self.class.generate_hash_id(id)
    end

    # extend methods for class
    def self.included(base)
      base.extend(ClassMethods)
    end

    module ClassMethods

      # generate the hash_id using the @magic_number declared in class
      # id class === String || UUIDTools::UUID || Fixnum

      def hash_length(number)
        @magic_number = number
      end

      def generate_hash_id(id)
        hasher.encode(rinse_id(id))
      end

      # reverse the has to get the original id
      # if UUID, parse it and return the UUID object

      def reverse_hash(hash)
        id = hasher.decode(hash).try(:first)
        (self.include? Dynamoid::Document) || (self.columns_hash["id"].type == :uuid) ? UUIDTools::UUID.parse_int(id) : id
      end

      private

      def hasher
        Hashman::Hasher.new("#{Rails.application.secrets.magic_number}", @magic_number || 4)
      end

      # standardize the id integer
      # we do not specify UUIDTools since comparisons break!
      # String (activeUUID tables), FixNum (normal ids), UUID (dynamoid/VideoClips)

      def rinse_id(id)
        case
          when id.class == String
            return UUIDTools::UUID.parse(id).to_i
          when id.class == Fixnum
            return id
          else
            return id.to_i
        end
      end

    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hashman-0.1.0 lib/hashman/inclusion.rb