Module: Cistern::Formatter::Formatador

Defined in:
lib/cistern/formatter/formatador.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(model) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/cistern/formatter/formatador.rb', line 4

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

.collection_inspect(collection) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cistern/formatter/formatador.rb', line 26

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

.model_inspect(model) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cistern/formatter/formatador.rb', line 13

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

Instance Method Details

#table(attributes = nil) ⇒ Object



50
51
52
# File 'lib/cistern/formatter/formatador.rb', line 50

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