Sha256: af12f55fe4a4baf39f2e8f24c0eabbfa78582c8773dc0d03f141f208de56b6fd

Contents?: true

Size: 1.85 KB

Versions: 13

Compression:

Stored size: 1.85 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(&: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(map(&:attributes), attributes)
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
cistern-2.8.0 lib/cistern/formatter/formatador.rb
cistern-2.7.2 lib/cistern/formatter/formatador.rb
cistern-2.7.1 lib/cistern/formatter/formatador.rb
cistern-2.7.0 lib/cistern/formatter/formatador.rb
cistern-2.6.0 lib/cistern/formatter/formatador.rb
cistern-2.5.0 lib/cistern/formatter/formatador.rb
cistern-2.4.1 lib/cistern/formatter/formatador.rb
cistern-2.4.0 lib/cistern/formatter/formatador.rb
cistern-2.3.0 lib/cistern/formatter/formatador.rb
cistern-2.2.7 lib/cistern/formatter/formatador.rb
cistern-2.2.6 lib/cistern/formatter/formatador.rb
cistern-2.2.5 lib/cistern/formatter/formatador.rb
cistern-2.2.4 lib/cistern/formatter/formatador.rb