Sha256: edde52aba54d0a707460bab437787e51fa0580956e503630efe3aed00fb158b8

Contents?: true

Size: 1.1 KB

Versions: 3

Compression:

Stored size: 1.1 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::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

3 entries across 3 versions & 1 rubygems

Version Path
cistern-0.5.8 lib/cistern/formatter/awesome_print.rb
cistern-0.5.7 lib/cistern/formatter/awesome_print.rb
cistern-0.5.6 lib/cistern/formatter/awesome_print.rb