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.
-
#strftime_format ⇒ Object
Builds the equivalent strftime format string.
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
38 39 40 |
# File 'lib/stamp/emitters/composite.rb', line 38 def -(others) emitters - Array(others) end |
#<<(emitter) ⇒ Object
30 31 32 |
# File 'lib/stamp/emitters/composite.rb', line 30 def <<(emitter) Array(emitter).each { |e| emitters << e } end |
#each(&block) ⇒ Object
34 35 36 |
# File 'lib/stamp/emitters/composite.rb', line 34 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.
43 44 45 46 47 48 49 |
# File 'lib/stamp/emitters/composite.rb', line 43 def replace_each! emitters.each_with_index do |emitter, index| emitters[index] = yield(emitter) end self end |
#strftime_format ⇒ Object
Builds the equivalent strftime format string.
23 24 25 26 27 28 |
# File 'lib/stamp/emitters/composite.rb', line 23 def strftime_format emitters.map do |e| e.strftime_directive || raise(ArgumentError, "#{e.class.name.split('::').last} (e.g. \"1st\") has no strftime equivalent") end.join end |