Class: Stamp::Emitters::Composite
- Inherits:
-
Object
- Object
- Stamp::Emitters::Composite
- Includes:
- Enumerable
- Defined in:
- lib/stamp/emitters/composite.rb
Instance Attribute Summary collapse
-
#emitters ⇒ Object
readonly
Returns the value of attribute emitters.
Instance Method Summary collapse
- #-(others) ⇒ Object
- #<<(emitter) ⇒ Object
- #each(&block) ⇒ Object
- #format(target) ⇒ Object
-
#initialize(emitters = []) ⇒ Composite
constructor
A new instance of Composite.
-
#replace_each! ⇒ Object
Replace each element as we iterate with the result of the given block.
Constructor Details
#initialize(emitters = []) ⇒ Composite
Returns a new instance of Composite.
8 9 10 |
# File 'lib/stamp/emitters/composite.rb', line 8 def initialize(emitters=[]) @emitters = emitters end |
Instance Attribute Details
#emitters ⇒ Object (readonly)
Returns the value of attribute emitters.
6 7 8 |
# File 'lib/stamp/emitters/composite.rb', line 6 def emitters @emitters end |
Instance Method Details
#-(others) ⇒ Object
28 29 30 |
# File 'lib/stamp/emitters/composite.rb', line 28 def -(others) emitters - Array(others) end |
#<<(emitter) ⇒ Object
20 21 22 |
# File 'lib/stamp/emitters/composite.rb', line 20 def <<(emitter) Array(emitter).each { |e| emitters << e } end |
#each(&block) ⇒ Object
24 25 26 |
# File 'lib/stamp/emitters/composite.rb', line 24 def each(&block) emitters.each(&block) end |
#format(target) ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/stamp/emitters/composite.rb', line 12 def format(target) # NOTE using #each to build string because benchmarking shows # that it's ~20% faster than .map.join('') result = '' emitters.each { |e| result << e.format(target).to_s } result end |
#replace_each! ⇒ Object
Replace each element as we iterate with the result of the given block.
33 34 35 36 37 38 39 |
# File 'lib/stamp/emitters/composite.rb', line 33 def replace_each! emitters.each_with_index do |emitter, index| emitters[index] = yield(emitter) end self end |