class Numerals::FormattingStream

Auxiliar class to implement << & >> operators on Format class and Format instances as a shortcut for the Format#write and read formatting operators.

Public Class Methods

[](*args) click to toggle source
# File lib/numerals/format/sugar.rb, line 33
def self.[](*args)
  new *args
end
new(format) click to toggle source
# File lib/numerals/format/sugar.rb, line 26
def initialize(format)
  @format = format
  @text = nil
  @type = nil
  @output = []
end

Public Instance Methods

<<(*objects) click to toggle source
# File lib/numerals/format/sugar.rb, line 57
def <<(*objects)
  objects.each do |object|
    case object
    when Format, Hash, Array
      @format.set! object
    when String
      if @type
        @output << @format.read(object, type: @type)
      else
        @output << object
      end
    else
      if @text
        @output << @format.read(@text, type: object)
      elsif object.is_a?(Class)
        @type = object
      else
        @output << @format.write(object)
      end
    end
  end
  self
end
>>(*objects) click to toggle source
# File lib/numerals/format/sugar.rb, line 81
def >>(*objects)
  objects.each do |object|
    case object
    when Format, Hash, Array
      @format.set! object
    when String
      @text = object
      if @type
        @output << @format.read(object, type: @type)
      end
    else
      if @text
        @output << @format.read(@text, type: object)
      elsif object.is_a?(Class)
        @type = object
      else
        @output << @format.write(object)
      end
    end
  end
  self
end
clear() click to toggle source
# File lib/numerals/format/sugar.rb, line 104
def clear
  @output.clear
end
to_a() click to toggle source
# File lib/numerals/format/sugar.rb, line 37
def to_a
  @output
end
to_s() click to toggle source
# File lib/numerals/format/sugar.rb, line 41
def to_s
  to_a.join
end
to_str() click to toggle source
# File lib/numerals/format/sugar.rb, line 45
def to_str
  to_s
end
value() click to toggle source
# File lib/numerals/format/sugar.rb, line 49
def value
  if @output.size > 1
    @output
  else
    @output.first
  end
end