From 66b2becebd73c6df233c6752f5212abcce13cf68 Mon Sep 17 00:00:00 2001 From: Stormwind Date: Sat, 25 Apr 2015 17:02:39 +0200 Subject: [PATCH] Rework output API Now you give the data at the creation of the object to render it later. So you need to create a new output object for every new set of data. --- lib/rubella/map.rb | 3 +-- lib/rubella/output/ascii.rb | 21 ++++++++++----------- lib/rubella/output/base.rb | 15 +++++++-------- lib/rubella/output/image.rb | 24 ++++++++++++------------ 4 files changed, 30 insertions(+), 33 deletions(-) diff --git a/lib/rubella/map.rb b/lib/rubella/map.rb index 937f3bc..d8fd42a 100644 --- a/lib/rubella/map.rb +++ b/lib/rubella/map.rb @@ -52,8 +52,7 @@ module Rubella # @return binaryblob An output representation of the heatmap def process inpt weight = @weighting.new 10 - outpt = @output.new - outpt.create weight.parse(inpt) + @output.new(weight.parse(inpt)).render end # Set the input type by the given name diff --git a/lib/rubella/output/ascii.rb b/lib/rubella/output/ascii.rb index 01d467b..75d03c5 100644 --- a/lib/rubella/output/ascii.rb +++ b/lib/rubella/output/ascii.rb @@ -23,10 +23,11 @@ module Rubella # Also sets the used ascii art theme to "shades_ascii". See the # used_symbols= for further information. # + # @param data Rubella::Storage # @param field_size int Used chars for one value # @return Rubella::Output::ASCII # - def initialize field_size = 1 + def initialize data, field_size = 1 @symbols = Hash.new @symbols["shades"] = [" ", " ", "░", "░", "▒", "▒", "▓", "▓", "█", "█"] @@ -38,7 +39,7 @@ module Rubella [" ", "1", "2", "3", "4", "5", "6", "7", "8", "9"] self.used_symbols = "shades_ascii" - super field_size + super data, field_size end # Sets the used ascii theme by the given name. @@ -61,14 +62,13 @@ module Rubella @symbols.keys.join(", ") end - # Creates an ascii art representation of the given storage data. + # Creates an ascii art representation. # - # @param storage Rubella::Storage - # @return Rubella::Storage::ASCII + # @return String # - def create storage - buckets = storage.data[0].length - # columns = storage.data.length + def render + buckets = @data.data[0].length + # columns = @data.data.length # image size # x = columns*@field_size @@ -78,7 +78,7 @@ module Rubella ascii_arr = [] 0.upto(buckets-1).each { |i| ascii_arr[i] = "" } - storage.data.each do |point| + @data.data.each do |point| i = 0 point.reverse.each do |part| part = (part*10).to_i @@ -94,8 +94,7 @@ module Rubella end end - @data = ascii_arr.join("\n") - self + ascii_arr.join("\n") end end diff --git a/lib/rubella/output/base.rb b/lib/rubella/output/base.rb index 1135757..6c8c876 100644 --- a/lib/rubella/output/base.rb +++ b/lib/rubella/output/base.rb @@ -8,28 +8,27 @@ module Rubella # class Base attr_accessor :field_size - attr_reader :data # Constructor # Gets the field size to store it local. It's the size of one value of # the later created visual representation. The unit depends on the kind # of representation. # + # @param data Rubella::Storage # @param field_size int size of one value # @return Rubella::Output::Base # - def initialize field_size + def initialize data, field_size + @data = data @field_size = field_size end - # Creates a visual representation of the data in the given storage and - # stores this local. + # Creates a visual representation. # - # @param storage Rubella::Storage - # @return Rubella::Output::Base + # @return rendered data # - def create storage - raise NotImplementedError "Please override 'create' in your concrete "+ + def render + raise NotImplementedError "Please override 'render' in your concrete "+ "implementation" end end diff --git a/lib/rubella/output/image.rb b/lib/rubella/output/image.rb index e920a69..fa55eb9 100644 --- a/lib/rubella/output/image.rb +++ b/lib/rubella/output/image.rb @@ -15,31 +15,31 @@ module Rubella # Constructor # Has a default field_size of 15 pixel. # + # @param data Rubella::Storage # @param field_size int How many pixel one value has # @return Rubella::Output::Image # - def initialize field_size = 15 - super field_size + def initialize data, field_size = 15 + super data, field_size end - # Creates a pixel based graphic from the given storage data. + # Creates a pixel based graphic. # - # @param storage Rubella::Storage - # @return Rubella::Output::Image + # @return RMagick::Image # - def create storage - buckets = storage.data[0].length - columns = storage.data.length + def render + buckets = @data.data[0].length + columns = @data.data.length # image size x = columns*@field_size y = buckets*@field_size # start drawing the damn thing - @data = Magick::Image.new(x, y) { self.background_color = "white" } + image = Magick::Image.new(x, y) { self.background_color = "white" } i = 0 - storage.data.each do |point| + @data.data.each do |point| j = 0 point.reverse.each do |part| # draw a red rectangle on the white background @@ -55,13 +55,13 @@ module Rubella # Draw core.fill(Magick::Pixel.from_hsla(0, 255, l, 1).to_color) core.rectangle((i*@field_size), (j*@field_size), ((i+1)*@field_size), ((j+1)*@field_size)) - core.draw @data + core.draw image j = j + 1 end i = i + 1 end - self + image end