lib/whirled_peas/graphics/composer.rb in whirled_peas-0.8.0 vs lib/whirled_peas/graphics/composer.rb in whirled_peas-0.9.0

- old
+ new

@@ -1,10 +1,12 @@ require 'whirled_peas/settings/box_settings' +require 'whirled_peas/settings/graph_settings' require 'whirled_peas/settings/grid_settings' require 'whirled_peas/settings/text_settings' require_relative 'box_painter' +require_relative 'graph_painter' require_relative 'grid_painter' require_relative 'text_painter' module WhirledPeas module Graphics @@ -43,22 +45,37 @@ def add_text(name=self.class.next_name, &block) child_settings = Settings::TextSettings.inherit(painter.settings) child = TextPainter.new(name, child_settings) # TextPainters are not composable, so yield nil content = yield nil, child_settings + child_settings.validate! unless self.class.stringable?(content) raise ArgumentError, "Unsupported type for text: #{content.class}" end child.content = content.to_s painter.add_child(child) end + def add_graph(name=self.class.next_name, &block) + child_settings = Settings::GraphSettings.inherit(painter.settings) + child = GraphPainter.new(name, child_settings) + # GraphPainters are not composable, so yield nil + content = yield nil, child_settings + child_settings.validate! + unless content.is_a?(Array) && content.length > 0 + raise ArgumentError, 'Graphs require a non-empty array as the content' + end + child.content = content + painter.add_child(child) + end + def add_box(name=self.class.next_name, &block) child_settings = Settings::BoxSettings.inherit(painter.settings) child = BoxPainter.new(name, child_settings) composer = self.class.new(child) value = yield composer, child.settings + child_settings.validate! painter.add_child(child) if !child.children? && self.class.stringable?(value) composer.add_text("#{name}-Text") { value.to_s } end end @@ -66,9 +83,10 @@ def add_grid(name=self.class.next_name, &block) child_settings = Settings::GridSettings.inherit(painter.settings) child = GridPainter.new(name, child_settings) composer = self.class.new(child) values = yield composer, child.settings + child_settings.validate! painter.add_child(child) if !child.children? && values.is_a?(Array) values.each.with_index do |value, index| composer.add_text("#{name}-Text-#{index}") { value.to_s } end