Class: IceCube::StringBuilder

Inherits:
Object
  • Object
show all
Extended by:
Helpers
Defined in:
lib/ice_cube/builders/string_builder.rb

Defined Under Namespace

Modules: Helpers

Constant Summary

Constants included from Helpers

Helpers::NUMBER_SUFFIX, Helpers::SPECIAL_SUFFIX

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

nice_number, sentence

Constructor Details

#initializeStringBuilder

Returns a new instance of StringBuilder.



7
8
9
# File 'lib/ice_cube/builders/string_builder.rb', line 7

def initialize
  @types = {}
end

Instance Attribute Details

#base=(value) ⇒ Object (writeonly)

Sets the attribute base

Parameters:

  • value

    the value to set the attribute base to.



5
6
7
# File 'lib/ice_cube/builders/string_builder.rb', line 5

def base=(value)
  @base = value
end

Class Method Details

.formatter(type) ⇒ Object



26
27
28
# File 'lib/ice_cube/builders/string_builder.rb', line 26

def self.formatter(type)
  @formatters[type]
end

.register_formatter(type, &formatter) ⇒ Object



30
31
32
33
# File 'lib/ice_cube/builders/string_builder.rb', line 30

def self.register_formatter(type, &formatter)
  @formatters ||= {}
  @formatters[type] = formatter
end

Instance Method Details

#piece(type, prefix = nil, suffix = nil) ⇒ Object



11
12
13
# File 'lib/ice_cube/builders/string_builder.rb', line 11

def piece(type, prefix = nil, suffix = nil)
  @types[type] ||= []
end

#to_sObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/ice_cube/builders/string_builder.rb', line 15

def to_s
  @types.each_with_object(@base || '') do |(type, segments), str|
    if f = self.class.formatter(type)
      str << ' ' << f.call(segments)
    else
      next if segments.empty?
      str << ' ' << self.class.sentence(segments)
    end
  end
end