Class: Commons::Lang::Builder::ToSBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/commons/lang/builder/to_s_builder.rb

Overview

This class supports the implementation of to_s method.

Example:

require 'commons/lang/builder/to_s_builder'

class Person
  def initialize
    @name = nil
    @age = nil
  end
  # ...
  def to_s
    return Commons::Lang::Builder::ToSBuilder.new(self).
      append('name', @name).
      append('age', @age).
      to_s
  end
end

Constant Summary collapse

@@default_style =
ToSStyle::DEFAULT_STYLE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, style = nil, buffer = nil) ⇒ ToSBuilder

Returns a new instance of ToSBuilder.



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/commons/lang/builder/to_s_builder.rb', line 75

def initialize(object, style = nil, buffer = nil)
  if style == nil
    style = get_default_style
  end
  if buffer == nil
    buffer = String.new('')
  end
  @buffer = buffer
  @style = style
  @object = object
  
  style.append_start(buffer, object)
end

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



60
61
62
# File 'lib/commons/lang/builder/to_s_builder.rb', line 60

def buffer
  @buffer
end

#objectObject (readonly)

Returns the value of attribute object.



60
61
62
# File 'lib/commons/lang/builder/to_s_builder.rb', line 60

def object
  @object
end

#styleObject (readonly)

Returns the value of attribute style.



60
61
62
# File 'lib/commons/lang/builder/to_s_builder.rb', line 60

def style
  @style
end

Class Method Details

.get_default_styleObject



62
63
64
# File 'lib/commons/lang/builder/to_s_builder.rb', line 62

def self.get_default_style
  return @@default_style
end

.set_default_style(style) ⇒ Object



67
68
69
70
71
72
# File 'lib/commons/lang/builder/to_s_builder.rb', line 67

def self.set_default_style(style)
  if style == nil
    raise ArgumentError, 'The style must not be nil'
  end
  @@default_style = style
end

Instance Method Details

#append(field_name, value, full_detail = true) ⇒ Object



90
91
92
93
# File 'lib/commons/lang/builder/to_s_builder.rb', line 90

def append(field_name, value, full_detail = true)
  @style.append(@buffer, field_name, value, full_detail)
  return self
end

#append_super(super_to_s) ⇒ Object



96
97
98
99
100
101
# File 'lib/commons/lang/builder/to_s_builder.rb', line 96

def append_super(super_to_s)
  if super_to_s != nil
    @style.append_super(@buffer, super_to_s)
  end
  return self
end

#append_to_s(to_s) ⇒ Object



104
105
106
107
108
109
# File 'lib/commons/lang/builder/to_s_builder.rb', line 104

def append_to_s(to_s)
  if to_s != nil
    @style.append_to_s(@buffer, to_s)
  end
  return self
end

#to_sObject



112
113
114
115
116
117
118
119
# File 'lib/commons/lang/builder/to_s_builder.rb', line 112

def to_s
  if @object == nil
    @buffer.append(@style.get_nil_text)
  else
    @style.append_end(@buffer, @object)
  end
  return @buffer.to_s
end