Class: WhirledPeas::Graphics::Composer

Inherits:
Object
  • Object
show all
Defined in:
lib/whirled_peas/graphics/composer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(painter) ⇒ Composer

Returns a new instance of Composer.



44
45
46
# File 'lib/whirled_peas/graphics/composer.rb', line 44

def initialize(painter)
  @painter = painter
end

Instance Attribute Details

#painterObject (readonly)

Returns the value of attribute painter.



42
43
44
# File 'lib/whirled_peas/graphics/composer.rb', line 42

def painter
  @painter
end

Class Method Details

.build(theme_name = nil, &block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/whirled_peas/graphics/composer.rb', line 29

def self.build(theme_name=nil, &block)
  theme_name ||= Settings::ThemeLibrary.default_name
  theme = Settings::ThemeLibrary.get(theme_name)
  settings = Settings::BoxSettings.new(theme)
  template = BoxPainter.new('TEMPLATE', settings)
  composer = Composer.new(template)
  value = yield composer, settings
  if !template.children? && stringable?(value)
    composer.add_text { value.to_s }
  end
  template
end

.next_nameObject



23
24
25
26
27
# File 'lib/whirled_peas/graphics/composer.rb', line 23

def self.next_name
  @counter ||= 0
  @counter += 1
  "Element-#{@counter}"
end

.stringable?(value) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/whirled_peas/graphics/composer.rb', line 19

def self.stringable?(value)
  STRINGALBE_CLASSES.include?(value.class)
end

Instance Method Details

#add_box(name = self.class.next_name, &block) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/whirled_peas/graphics/composer.rb', line 78

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

#add_component(component) ⇒ Object



74
75
76
# File 'lib/whirled_peas/graphics/composer.rb', line 74

def add_component(component)
  component.compose(self, settings)
end

#add_graph(name = self.class.next_name, &block) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/whirled_peas/graphics/composer.rb', line 61

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

#add_grid(name = self.class.next_name, &block) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/whirled_peas/graphics/composer.rb', line 90

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
  end
end

#add_text(name = self.class.next_name, &block) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/whirled_peas/graphics/composer.rb', line 48

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