Sha256: 62e5cc828797f1352980aa72db820107624b012725fad6ad3bba88172db2221e

Contents?: true

Size: 635 Bytes

Versions: 3

Compression:

Stored size: 635 Bytes

Contents

module Aggro
  module Transform
    # Private: Transforms integer representations.
    class ID
      ID_REGEX = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/

      def initialize(generate: false)
        @generate = generate
      end

      def deserialize(value)
        value = value.to_s

        return value if value.match(ID_REGEX)

        generate_id if should_generate_id?
      end

      def serialize(value)
        deserialize value
      end

      private

      def generate_id
        SecureRandom.uuid
      end

      def should_generate_id?
        @generate == true
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
aggro-0.0.4 lib/aggro/transform/id.rb
aggro-0.0.3 lib/aggro/transform/id.rb
aggro-0.0.2 lib/aggro/transform/id.rb