Sha256: 1314b419717838780cd7d79191889125dbeca9fa1e25268bde647d18cdb32b28

Contents?: true

Size: 1.16 KB

Versions: 5

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

require 'extra/enum'
require 'extra/uuid'

class Enum

  class << self

    pre_verbose = $VERBOSE
    $VERBOSE = nil

    alias :old_parse :parse

    def parse src
      return nil if src == nil
      return src if self === src
      case src
      when Integer, Symbol
        self[src]
      when String
        self.old_parse src, case_sensitive: false
      end
    end

    $VERBOSE = pre_verbose

    def ddl
      if any? { |v| Integer === v.data }
        {
          name: :TEXT,
          data: :INTEGER
        }
      else
        :TEXT
      end
    end

    def from_db src
      return nil if src == nil
      return self[src.intern] if String === src || Symbol === src
      name = src[:name]&.intern
      return nil if name == nil
      self[name]
    end

  end

  def to_db
    if self.class.any? { |v| Integer === v.data }
      {
        name: name.to_s,
        data: data
      }
    else
      name.to_s
    end
  end

  def to_query
    to_s
  end

end

class UUID

  class << self

    def ddl
      :TEXT
    end

    def from_db src
      parse src
    end

  end

  def to_db
    to_s
  end

  def to_query
    to_s
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
inat-get-0.8.0.15 lib/inat/data/types/extras.rb
inat-get-0.8.0.14 lib/inat/data/types/extras.rb
inat-get-0.8.0.13 lib/inat/data/types/extras.rb
inat-get-0.8.0.12 lib/inat/data/types/extras.rb
inat-get-0.8.0.11 lib/inat/data/types/extras.rb