Sha256: 81ad27bf768d867d2728ab07496aa63a0940dd30f196310682d0de0f3ea994c9

Contents?: true

Size: 1.79 KB

Versions: 1

Compression:

Stored size: 1.79 KB

Contents

module JsonbAccessor
  CONSTANT_SEPARATOR = "::"
  module TypeHelper
    ARRAY_MATCHER = /_array\z/
    UnknownType = Class.new(StandardError)

    class << self
      def fetch(type)
        case type
        when :array
          new_array(value)
        when ARRAY_MATCHER
          fetch_active_record_array_type(type)
        when :value
          value
        else
          fetch_active_record_type(type)
        end
      end

      def type_cast_as_jsonb(suspect)
        user_cast_method_name, db_case_method_name = ActiveRecord::VERSION::MAJOR == 5 ? [:cast, :serialize] : [:type_cast_from_user, :type_cast_for_database]
        type_cast_hash = jsonb.public_send(user_cast_method_name, suspect)
        jsonb.public_send(db_case_method_name, type_cast_hash)
      end

      private

      def jsonb
        @jsonb ||= fetch(:jsonb)
      end

      def fetch_active_record_array_type(type)
        subtype = type.to_s.sub(ARRAY_MATCHER, "")
        new_array(fetch_active_record_type(subtype))
      end

      def fetch_active_record_type(type)
        class_name = type.to_s.camelize
        klass = value_descendants.find do |ar_type|
          ar_type.to_s.split(CONSTANT_SEPARATOR).last == class_name
        end

        if klass
          klass.new
        else
          raise JsonbAccessor::TypeHelper::UnknownType
        end
      end

      def value_descendants
        grouped_types = ActiveRecord::Type::Value.descendants.group_by do |ar_type|
          !!ar_type.to_s.match(ActiveRecord::ConnectionAdapters::PostgreSQL::OID.to_s)
        end

        grouped_types[true] + grouped_types[false]
      end

      def value
        ActiveRecord::Type::Value.new
      end

      def new_array(subtype)
        ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array.new(subtype)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jsonb_accessor-0.4.0.beta lib/jsonb_accessor/type_helper.rb