Class: IceCube::StringBuilder
- Inherits:
-
Object
- Object
- IceCube::StringBuilder
- Extended by:
- Helpers
- Defined in:
- lib/ice_cube/builders/string_builder.rb
Defined Under Namespace
Modules: Helpers
Instance Attribute Summary collapse
-
#base ⇒ Object
writeonly
Sets the attribute base.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ StringBuilder
constructor
A new instance of StringBuilder.
- #piece(type, prefix = nil, suffix = nil) ⇒ Object
- #to_s ⇒ Object
Methods included from Helpers
literal_ordinal, nice_number, ordinal, ordinalize, sentence
Constructor Details
#initialize ⇒ StringBuilder
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
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_s ⇒ Object
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 |