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.



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



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

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

.register_formatter(type, &formatter) ⇒ Object



34
35
36
37
# File 'lib/ice_cube/builders/string_builder.rb', line 34

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
25
26
27
28
# File 'lib/ice_cube/builders/string_builder.rb', line 15

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