0
0
Fork 0

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.
This commit is contained in:
Stormwind 2015-04-25 17:02:39 +02:00
parent 2bb29d9e8d
commit 66b2becebd
4 changed files with 30 additions and 33 deletions

View File

@ -52,8 +52,7 @@ module Rubella
# @return binaryblob An output representation of the heatmap # @return binaryblob An output representation of the heatmap
def process inpt def process inpt
weight = @weighting.new 10 weight = @weighting.new 10
outpt = @output.new @output.new(weight.parse(inpt)).render
outpt.create weight.parse(inpt)
end end
# Set the input type by the given name # Set the input type by the given name

View File

@ -23,10 +23,11 @@ module Rubella
# Also sets the used ascii art theme to "shades_ascii". See the # Also sets the used ascii art theme to "shades_ascii". See the
# used_symbols= for further information. # used_symbols= for further information.
# #
# @param data Rubella::Storage
# @param field_size int Used chars for one value # @param field_size int Used chars for one value
# @return Rubella::Output::ASCII # @return Rubella::Output::ASCII
# #
def initialize field_size = 1 def initialize data, field_size = 1
@symbols = Hash.new @symbols = Hash.new
@symbols["shades"] = @symbols["shades"] =
[" ", " ", "", "", "", "", "", "", "", ""] [" ", " ", "", "", "", "", "", "", "", ""]
@ -38,7 +39,7 @@ module Rubella
[" ", "1", "2", "3", "4", "5", "6", "7", "8", "9"] [" ", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
self.used_symbols = "shades_ascii" self.used_symbols = "shades_ascii"
super field_size super data, field_size
end end
# Sets the used ascii theme by the given name. # Sets the used ascii theme by the given name.
@ -61,14 +62,13 @@ module Rubella
@symbols.keys.join(", ") @symbols.keys.join(", ")
end end
# Creates an ascii art representation of the given storage data. # Creates an ascii art representation.
# #
# @param storage Rubella::Storage # @return String
# @return Rubella::Storage::ASCII
# #
def create storage def render
buckets = storage.data[0].length buckets = @data.data[0].length
# columns = storage.data.length # columns = @data.data.length
# image size # image size
# x = columns*@field_size # x = columns*@field_size
@ -78,7 +78,7 @@ module Rubella
ascii_arr = [] ascii_arr = []
0.upto(buckets-1).each { |i| ascii_arr[i] = "" } 0.upto(buckets-1).each { |i| ascii_arr[i] = "" }
storage.data.each do |point| @data.data.each do |point|
i = 0 i = 0
point.reverse.each do |part| point.reverse.each do |part|
part = (part*10).to_i part = (part*10).to_i
@ -94,8 +94,7 @@ module Rubella
end end
end end
@data = ascii_arr.join("\n") ascii_arr.join("\n")
self
end end
end end

View File

@ -8,28 +8,27 @@ module Rubella
# #
class Base class Base
attr_accessor :field_size attr_accessor :field_size
attr_reader :data
# Constructor # Constructor
# Gets the field size to store it local. It's the size of one value of # 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 # the later created visual representation. The unit depends on the kind
# of representation. # of representation.
# #
# @param data Rubella::Storage
# @param field_size int size of one value # @param field_size int size of one value
# @return Rubella::Output::Base # @return Rubella::Output::Base
# #
def initialize field_size def initialize data, field_size
@data = data
@field_size = field_size @field_size = field_size
end end
# Creates a visual representation of the data in the given storage and # Creates a visual representation.
# stores this local.
# #
# @param storage Rubella::Storage # @return rendered data
# @return Rubella::Output::Base
# #
def create storage def render
raise NotImplementedError "Please override 'create' in your concrete "+ raise NotImplementedError "Please override 'render' in your concrete "+
"implementation" "implementation"
end end
end end

View File

@ -15,31 +15,31 @@ module Rubella
# Constructor # Constructor
# Has a default field_size of 15 pixel. # Has a default field_size of 15 pixel.
# #
# @param data Rubella::Storage
# @param field_size int How many pixel one value has # @param field_size int How many pixel one value has
# @return Rubella::Output::Image # @return Rubella::Output::Image
# #
def initialize field_size = 15 def initialize data, field_size = 15
super field_size super data, field_size
end end
# Creates a pixel based graphic from the given storage data. # Creates a pixel based graphic.
# #
# @param storage Rubella::Storage # @return RMagick::Image
# @return Rubella::Output::Image
# #
def create storage def render
buckets = storage.data[0].length buckets = @data.data[0].length
columns = storage.data.length columns = @data.data.length
# image size # image size
x = columns*@field_size x = columns*@field_size
y = buckets*@field_size y = buckets*@field_size
# start drawing the damn thing # 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 i = 0
storage.data.each do |point| @data.data.each do |point|
j = 0 j = 0
point.reverse.each do |part| point.reverse.each do |part|
# draw a red rectangle on the white background # draw a red rectangle on the white background
@ -55,13 +55,13 @@ module Rubella
# Draw # Draw
core.fill(Magick::Pixel.from_hsla(0, 255, l, 1).to_color) 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.rectangle((i*@field_size), (j*@field_size), ((i+1)*@field_size), ((j+1)*@field_size))
core.draw @data core.draw image
j = j + 1 j = j + 1
end end
i = i + 1 i = i + 1
end end
self image
end end