Class: Numerals::FormattingStream
- Inherits:
-
Object
- Object
- Numerals::FormattingStream
- Defined in:
- lib/numerals/format/sugar.rb
Overview
Auxiliar class to implement << & >> operators on Format class and Format instances as a shortcut for the Format#write and #read formatting operators.
Class Method Summary collapse
Instance Method Summary collapse
- #<<(*objects) ⇒ Object
- #>>(*objects) ⇒ Object
- #clear ⇒ Object
-
#initialize(format) ⇒ FormattingStream
constructor
A new instance of FormattingStream.
- #to_a ⇒ Object
- #to_s ⇒ Object
- #to_str ⇒ Object
- #value ⇒ Object
Constructor Details
#initialize(format) ⇒ FormattingStream
Returns a new instance of FormattingStream.
26 27 28 29 30 31 |
# File 'lib/numerals/format/sugar.rb', line 26 def initialize(format) @format = format @text = nil @type = nil @output = [] end |
Class Method Details
.[](*args) ⇒ Object
33 34 35 |
# File 'lib/numerals/format/sugar.rb', line 33 def self.[](*args) new *args end |
Instance Method Details
#<<(*objects) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# 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) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# 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 ⇒ Object
104 105 106 |
# File 'lib/numerals/format/sugar.rb', line 104 def clear @output.clear end |
#to_a ⇒ Object
37 38 39 |
# File 'lib/numerals/format/sugar.rb', line 37 def to_a @output end |
#to_s ⇒ Object
41 42 43 |
# File 'lib/numerals/format/sugar.rb', line 41 def to_s to_a.join end |
#to_str ⇒ Object
45 46 47 |
# File 'lib/numerals/format/sugar.rb', line 45 def to_str to_s end |
#value ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/numerals/format/sugar.rb', line 49 def value if @output.size > 1 @output else @output.first end end |