Class: Erlash::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/erlash/formatter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(registry, options = {}) ⇒ Formatter

Returns a new instance of Formatter.



26
27
28
29
30
31
# File 'lib/erlash/formatter.rb', line 26

def initialize(registry, options = {})
  @registry = registry
  @output = StringIO.new
  @objs = []
  @options = options
end

Instance Attribute Details

#objsObject (readonly)

Returns the value of attribute objs.



25
26
27
# File 'lib/erlash/formatter.rb', line 25

def objs
  @objs
end

#optionsObject (readonly)

Returns the value of attribute options.



25
26
27
# File 'lib/erlash/formatter.rb', line 25

def options
  @options
end

#outputObject (readonly)

Returns the value of attribute output.



25
26
27
# File 'lib/erlash/formatter.rb', line 25

def output
  @output
end

#registryObject (readonly)

Returns the value of attribute registry.



25
26
27
# File 'lib/erlash/formatter.rb', line 25

def registry
  @registry
end

Class Method Details

.format(inst, obj) ⇒ Object

def self.call(inst, obj)

registry = inst.registry
formatter = registry.find(obj.class)
if formatter
  formatter.call(inst, obj)
elsif obj.respond_to?(:to_erlash)
  obj.to_erlash
else
  obj.to_s
end

end



15
16
17
18
19
20
21
22
23
# File 'lib/erlash/formatter.rb', line 15

def self.format(inst, obj)
  registry = inst.registry
  formatter = registry.find(obj.class)
  if formatter
    formatter.format(inst, obj)
  else
    obj.to_s
  end
end

Instance Method Details

#<<(elem) ⇒ Object



33
34
35
# File 'lib/erlash/formatter.rb', line 33

def <<(elem)
  objs << elem
end

#to_sObject Also known as: string



37
38
39
40
41
42
43
# File 'lib/erlash/formatter.rb', line 37

def to_s
  return @to_s if defined?(@to_s)
  objs.each do |e|
    output.puts format_elem(e)
  end
  @to_s = output.string
end