Class: Erlash::Formatter
- Inherits:
-
Object
- Object
- Erlash::Formatter
- Defined in:
- lib/erlash/formatter.rb
Instance Attribute Summary collapse
-
#objs ⇒ Object
readonly
Returns the value of attribute objs.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
-
#registry ⇒ Object
readonly
Returns the value of attribute registry.
Class Method Summary collapse
-
.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.
Instance Method Summary collapse
- #<<(elem) ⇒ Object
-
#initialize(registry, options = {}) ⇒ Formatter
constructor
A new instance of Formatter.
- #to_s ⇒ Object (also: #string)
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, = {}) @registry = registry @output = StringIO.new @objs = [] @options = end |
Instance Attribute Details
#objs ⇒ Object (readonly)
Returns the value of attribute objs.
25 26 27 |
# File 'lib/erlash/formatter.rb', line 25 def objs @objs end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
25 26 27 |
# File 'lib/erlash/formatter.rb', line 25 def @options end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
25 26 27 |
# File 'lib/erlash/formatter.rb', line 25 def output @output end |
#registry ⇒ Object (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_s ⇒ Object 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 |