Sha256: b60a8420a87f8fc59bc0e190026e9586f094663a85684b6b5976c342e4ae45a1

Contents?: true

Size: 1.07 KB

Versions: 2

Compression:

Stored size: 1.07 KB

Contents

require 'active_support/concern'

require 'pg_serializable/version'
require 'pg_serializable/errors'
require 'pg_serializable/aliaser'
require 'pg_serializable/nodes'
require 'pg_serializable/serializer'

module PgSerializable
  extend ActiveSupport::Concern
  included do
    def json
      ActiveRecord::Base.connection.select_one(
        self.class.where(id: id).limit(1).as_json_object.to_sql
      )['json_build_object']
    end
  end

  class_methods do
    def json
      ActiveRecord::Base.connection.select_one(
        serializer.as_json_array(pg_scope, Aliaser.new).to_sql
      )['coalesce']
    end

    def as_json_array(table_alias = Aliaser.new)
      serializer.as_json_array(pg_scope, table_alias)
    end

    def as_json_object(table_alias = Aliaser.new)
      serializer.as_json_object(pg_scope, table_alias)
    end

    def serializable(&blk)
      serializer.instance_eval &blk
      serializer.check_for_cycles!
    end

    def serializer
      @serializer ||= Serializer.new(self)
    end

    def pg_scope
      respond_to?(:to_sql) ? self : all
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pg_serializable-0.1.1 lib/pg_serializable.rb
pg_serializable-0.1.0 lib/pg_serializable.rb