Class: Progress::WithProgress
- Inherits:
-
Object
- Object
- Progress::WithProgress
- Includes:
- Enumerable
- Defined in:
- lib/progress/with_progress.rb
Instance Attribute Summary collapse
-
#enumerable ⇒ Object
readonly
Returns the value of attribute enumerable.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Instance Method Summary collapse
-
#each ⇒ Object
each object with progress.
-
#initialize(enumerable, title, length = nil, &block) ⇒ WithProgress
constructor
initialize with object responding to each, title and optional length if block is provided, it is passed to each.
-
#length ⇒ Object
determine number of objects.
-
#with_progress(title = nil, &block) ⇒ Object
returns self but changes title.
Constructor Details
#initialize(enumerable, title, length = nil, &block) ⇒ WithProgress
initialize with object responding to each, title and optional length if block is provided, it is passed to each
11 12 13 14 |
# File 'lib/progress/with_progress.rb', line 11 def initialize(enumerable, title, length = nil, &block) @enumerable, @title, @length = enumerable, title, length each(&block) if block end |
Instance Attribute Details
#enumerable ⇒ Object (readonly)
Returns the value of attribute enumerable.
7 8 9 |
# File 'lib/progress/with_progress.rb', line 7 def enumerable @enumerable end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
7 8 9 |
# File 'lib/progress/with_progress.rb', line 7 def title @title end |
Instance Method Details
#each ⇒ Object
each object with progress
17 18 19 20 21 22 23 24 25 |
# File 'lib/progress/with_progress.rb', line 17 def each Progress.start(@title, length) do @enumerable.each do |object| Progress.step do yield object end end end end |
#length ⇒ Object
determine number of objects
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/progress/with_progress.rb', line 28 def length @length ||= if @enumerable.respond_to?(:length) && !@enumerable.is_a?(String) @enumerable.length elsif @enumerable.respond_to?(:count) @enumerable.count elsif @enumerable.respond_to?(:to_a) @enumerable.to_a.length else @enumerable.inject(0){ |length, obj| length + 1 } end end |
#with_progress(title = nil, &block) ⇒ Object
returns self but changes title
41 42 43 |
# File 'lib/progress/with_progress.rb', line 41 def with_progress(title = nil, &block) self.class.new(@enumerable, title, @length, &block) end |