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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

literal_ordinal, nice_number, ordinal, ordinalize, sentence

Constructor Details

#initializeStringBuilder

Returns a new instance of StringBuilder.



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

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.



3
4
5
# File 'lib/ice_cube/builders/string_builder.rb', line 3

def base=(value)
  @base = value
end

Class Method Details

.formatter(type) ⇒ Object



28
29
30
# File 'lib/ice_cube/builders/string_builder.rb', line 28

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

.register_formatter(type, &formatter) ⇒ Object



32
33
34
35
# File 'lib/ice_cube/builders/string_builder.rb', line 32

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

Instance Method Details

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



9
10
11
# File 'lib/ice_cube/builders/string_builder.rb', line 9

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

#to_sObject



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

def to_s
  string = @base || ""
  @types.each do |type, segments|
    if (f = self.class.formatter(type))
      current = f.call(segments)
    else
      next if segments.empty?
      current = self.class.sentence(segments)
    end
    f = IceCube::I18n.t("ice_cube.string.format")[type] ? type : "default"
    string = IceCube::I18n.t("ice_cube.string.format.#{f}", rest: string, current: current)
  end
  string
end