Sha256: c8c4ebacac9a18408fe1a8d636dd642a5102ff8c57e0250013f7981e3600b638

Contents?: true

Size: 1.89 KB

Versions: 33

Compression:

Stored size: 1.89 KB

Contents

require 'formatador'

module Cistern::Formatter::Formatador
  def self.call(model)
    case model
    when Cistern::Collection then collection_inspect(model)
    when Cistern::Model then model_inspect(model)
    when Cistern::Singular then model_inspect(model)
    else model.inspect
    end
  end

  def self.model_inspect(model)
    Thread.current[:formatador] ||= Formatador.new
    data = "#{Thread.current[:formatador].indentation}<#{model.class.name}"
    Thread.current[:formatador].indent do
      unless model.class.attributes.empty?
        data << "\n#{Thread.current[:formatador].indentation}"
        data << model.class.attributes.map { |attribute, _| "#{attribute}=#{model.send(attribute).inspect}" }.join(",\n#{Thread.current[:formatador].indentation}")
      end
    end
    data << "\n#{Thread.current[:formatador].indentation}>"
    data
  end

  def self.collection_inspect(collection)
    Thread.current[:formatador] ||= Formatador.new
    data = "#{Thread.current[:formatador].indentation}<#{collection.class.name}\n"
    Thread.current[:formatador].indent do
      unless collection.class.attributes.empty?
        data << "#{Thread.current[:formatador].indentation}"
        data << collection.class.attributes.map {|attribute| "#{attribute}=#{send(attribute).inspect}"}.join(",\n#{Thread.current[:formatador].indentation}")
        data << "\n"
      end
      data << "#{Thread.current[:formatador].indentation}["
      unless collection.empty?
        data << "\n"
        Thread.current[:formatador].indent do
          data << collection.map {|member| member.inspect}.join(",\n")
          data << "\n"
        end
        data << Thread.current[:formatador].indentation
      end
      data << "]\n"
    end
    data << "#{Thread.current[:formatador].indentation}>"
    data
  end

  def table(attributes = nil)
    Formatador.display_table(self.map {|instance| instance.attributes}, attributes)
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
cistern-0.12.3 lib/cistern/formatter/formatador.rb
cistern-2.2.3 lib/cistern/formatter/formatador.rb
cistern-2.2.1 lib/cistern/formatter/formatador.rb
cistern-2.1.0 lib/cistern/formatter/formatador.rb
cistern-2.0.5 lib/cistern/formatter/formatador.rb
cistern-2.0.4 lib/cistern/formatter/formatador.rb
cistern-0.12.2 lib/cistern/formatter/formatador.rb
cistern-0.12.1 lib/cistern/formatter/formatador.rb
cistern-2.0.3 lib/cistern/formatter/formatador.rb
cistern-2.0.2 lib/cistern/formatter/formatador.rb
cistern-0.11.3 lib/cistern/formatter/formatador.rb
cistern-2.0.1 lib/cistern/formatter/formatador.rb
cistern-1.0.1.pre6 lib/cistern/formatter/formatador.rb
cistern-1.0.1.pre5 lib/cistern/formatter/formatador.rb
cistern-1.0.1.pre4 lib/cistern/formatter/formatador.rb
cistern-1.0.1.pre3 lib/cistern/formatter/formatador.rb
cistern-1.0.1.pre2 lib/cistern/formatter/formatador.rb
cistern-0.11.2 lib/cistern/formatter/formatador.rb
cistern-1.0.1.pre1 lib/cistern/formatter/formatador.rb
cistern-1.0.0.pre lib/cistern/formatter/formatador.rb