Sha256: 29e0b5fefc13964181fb71cb6aee661a98908e41549a75de2255249d81695e6a

Contents?: true

Size: 931 Bytes

Versions: 8

Compression:

Stored size: 931 Bytes

Contents

module Rasti
  module DB
    module TypeConverters
      module PostgresTypes
        class JSON
          class << self

            DB_TYPE_REGEX = /^json$/

            def to_db?(type)
              !type.match(DB_TYPE_REGEX).nil?
            end

            def to_db(value, type)
              Sequel.pg_json value
            end

            def from_db?(klass)
              to_hash?(klass) || to_array?(klass)
            end

            def from_db(value)
              to_hash?(value.class) ? value.to_h : value.to_a
            end

            private

            def to_hash?(klass)
              defined?(Sequel::Postgres::JSONHash) &&
              klass == Sequel::Postgres::JSONHash
            end

            def to_array?(klass)
              defined?(Sequel::Postgres::JSONArray) &&
              klass == Sequel::Postgres::JSONArray
            end

          end
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rasti-db-4.2.0 lib/rasti/db/type_converters/postgres_types/json.rb
rasti-db-4.1.1 lib/rasti/db/type_converters/postgres_types/json.rb
rasti-db-4.1.0 lib/rasti/db/type_converters/postgres_types/json.rb
rasti-db-4.0.0 lib/rasti/db/type_converters/postgres_types/json.rb
rasti-db-3.0.0 lib/rasti/db/type_converters/postgres_types/json.rb
rasti-db-2.3.3 lib/rasti/db/type_converters/postgres_types/json.rb
rasti-db-2.3.2 lib/rasti/db/type_converters/postgres_types/json.rb
rasti-db-2.3.1 lib/rasti/db/type_converters/postgres_types/json.rb