Class: ProgressBarSnapshot
- Inherits:
-
Object
- Object
- ProgressBarSnapshot
- Defined in:
- lib/progress_bar_snapshot.rb
Overview
A library for rendering the image of a progress bar.
Used to generate an progress bar images to a file or in RAILS apps.
RMagick library required.
Authors
Xiaoke Zhang
General Usage and Defaults
License
Licensed under the MIT license.
Constant Summary collapse
- VERSION =
'0.0.4'
Instance Method Summary collapse
-
#initialize(width, height, percentages, theme = {}) ⇒ ProgressBarSnapshot
constructor
A new instance of ProgressBarSnapshot.
- #render ⇒ Object
- #to_blob(format = 'png') ⇒ Object
- #to_file(filename) ⇒ Object
Constructor Details
#initialize(width, height, percentages, theme = {}) ⇒ ProgressBarSnapshot
Returns a new instance of ProgressBarSnapshot.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/progress_bar_snapshot.rb', line 28 def initialize (width, height, percentages, theme = {}) @width = [width, 0].max @height = [height, 0].max @percentages_segments = percentages.select{|p| p >= 0} # default theme if none was passed in @default_theme = { :border_color => "#666666", :progress_colors => ["skyblue", 'grey' ,'salmon'], :background => "white", :font_family => "arial", :font_color => "black", :border_width => 1 } @default_theme.update(theme) end |
Instance Method Details
#render ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/progress_bar_snapshot.rb', line 55 def render # new image canvas = Magick::ImageList.new canvas.new_image(@width, @height) # make a new draw object = # make a new text object text = draw_percentage_text # progress bar total_percentage = [@percentages_segments.inject{|sum, n| sum + n}, 100].min draw_inner_progress() if ((1.0..100.0).include?(total_percentage)) # draw the progress bar on canvas .draw(canvas) # draw the percentage number on the progress bar text.annotate(canvas, 0,0,0,0, "#{total_percentage.to_i}%") canvas end |
#to_blob(format = 'png') ⇒ Object
45 46 47 48 49 |
# File 'lib/progress_bar_snapshot.rb', line 45 def to_blob(format = 'png') image = render image.format = format image.to_blob {self.depth = 8} end |
#to_file(filename) ⇒ Object
51 52 53 |
# File 'lib/progress_bar_snapshot.rb', line 51 def to_file(filename) render.write(filename) {self.depth = 8} end |