Sha256: b48a1f7b1b8ec3a919cddc655641479efb9a7acf4baf2f7301b8e839832299af

Contents?: true

Size: 1.17 KB

Versions: 5

Compression:

Stored size: 1.17 KB

Contents

require 'awesome_print'

module Cistern::Formatter::AwesomePrint
  def self.call(model)
    model.ai
  end
end

module AwesomePrint::Cistern
  def self.included(base)
    base.send :alias_method, :cast_without_cistern, :cast
    base.send :alias_method, :cast, :cast_with_cistern
  end

  def cast_with_cistern(object, type)
    cast = cast_without_cistern(object, type)
    if object.is_a?(Cistern::Model)
      cast = :cistern_model
    elsif object.is_a?(Cistern::Singular)
      cast = :cistern_model
    elsif object.is_a?(Cistern::Collection)
      cast = :cistern_collection
    end
    cast
  end

  # Format Cistern::Model
  #------------------------------------------------------------------------------
  def awesome_cistern_model(object)
    data = object.attributes.keys.inject({}) { |r, k| r.merge(k => object.send(k)) }
    "#{object} " << awesome_hash(data)
  end

  # Format Cistern::Model
  #------------------------------------------------------------------------------
  def awesome_cistern_collection(object)
    "#{object.class.name} " << awesome_hash(attributes: object.attributes, records: object.to_a)
  end
end

AwesomePrint::Formatter.send(:include, AwesomePrint::Cistern)

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
cistern-2.3.0 lib/cistern/formatter/awesome_print.rb
cistern-2.2.7 lib/cistern/formatter/awesome_print.rb
cistern-2.2.6 lib/cistern/formatter/awesome_print.rb
cistern-2.2.5 lib/cistern/formatter/awesome_print.rb
cistern-2.2.4 lib/cistern/formatter/awesome_print.rb