Class: Diagrams::PieDiagram

Inherits:
Base
  • Object
show all
Defined in:
lib/diagrams/pie_diagram.rb

Overview

Represents a Pie Chart diagram consisting of slices.

Instance Attribute Summary collapse

Attributes inherited from Base

#checksum, #version

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#diff, from_hash, from_json, #to_h, #to_json

Constructor Details

#initialize(title: '', slices: [], version: 1) ⇒ PieDiagram

Initializes a new PieDiagram.

Parameters:

  • title (String) (defaults to: '')

    The title of the pie chart.

  • slices (Array<Element::Slice>) (defaults to: [])

    An array of slice objects.

  • version (String, Integer, nil) (defaults to: 1)

    User-defined version identifier.



13
14
15
16
17
18
19
20
21
# File 'lib/diagrams/pie_diagram.rb', line 13

def initialize(title: '', slices: [], version: 1)
  super(version: version) # Corrected super call
  @title = title.is_a?(String) ? title : ''
  @slices = [] # Initialize empty
  # Add initial slices using the corrected add_slice method
  Array(slices).each { |s| add_slice(s, update_checksum: false, initial_load: true) }
  recalculate_percentages! # Calculate initial percentages
  update_checksum! # Calculate final checksum after initial load
end

Instance Attribute Details

#slicesObject (readonly)

Returns the value of attribute slices.



6
7
8
# File 'lib/diagrams/pie_diagram.rb', line 6

def slices
  @slices
end

#titleObject (readonly)

Returns the value of attribute title.



6
7
8
# File 'lib/diagrams/pie_diagram.rb', line 6

def title
  @title
end

Class Method Details

.from_h(data_hash, version:, checksum:) ⇒ PieDiagram

Class method to create a PieDiagram from a hash. Used by the deserialization factory in ‘Diagrams::Base`.

Parameters:

  • data_hash (Hash)

    Hash containing ‘:title` and `:slices` array.

  • version (String, Integer, nil)

    Diagram version.

  • checksum (String, nil)

    Expected checksum (optional, for verification).

Returns:



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/diagrams/pie_diagram.rb', line 84

def self.from_h(data_hash, version:, checksum:)
  title = data_hash[:title] || data_hash['title'] || ''
  slices_data = data_hash[:slices] || data_hash['slices'] || []

  # Initialize with raw values, percentage will be recalculated by `new` -> `add_slice` -> `recalculate_percentages!`
  slices = slices_data.map do |slice_h|
    Diagrams::Elements::Slice.new(slice_h.transform_keys(&:to_sym).except(:percentage))
  end

  diagram = new(title: title, slices: slices, version: version)

  # Optional: Verify checksum if provided AFTER initialization is complete
  if checksum && diagram.checksum != checksum
    warn "Checksum mismatch for loaded PieDiagram (version: #{version}). Expected #{checksum}, got #{diagram.checksum}."
    # Or raise an error: raise "Checksum mismatch..."
  end

  diagram
end

Instance Method Details

#add_slice(slice, update_checksum: true, initial_load: false) ⇒ Element::Slice

Adds a slice to the diagram.

Added initial_load flag to skip checksum update during initialize loop

Parameters:

  • slice (Element::Slice)

    The slice object to add.

Returns:

  • (Element::Slice)

    The added slice.

Raises:

  • (ArgumentError)

    if a slice with the same label already exists.



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

def add_slice(slice, update_checksum: true, initial_load: false)
  raise ArgumentError, 'Slice must be a Diagrams::Elements::Slice' unless slice.is_a?(Diagrams::Elements::Slice)
  raise ArgumentError, "Slice with label '#{slice.label}' already exists" if find_slice(slice.label)

  # Store a new instance to hold the calculated percentage later
  # Ensure percentage is nil initially
  new_slice_instance = slice.class.new(slice.attributes.except(:percentage))
  @slices << new_slice_instance
  recalculate_percentages! # Update percentages for all slices
  update_checksum! if update_checksum && !initial_load # Avoid multiple checksums during init
  new_slice_instance # Return the instance added to the array
end

#find_slice(label) ⇒ Element::Slice?

Finds a slice by its label.

Parameters:

  • label (String)

    The label of the slice to find.

Returns:

  • (Element::Slice, nil)

    The found slice or nil.



46
47
48
# File 'lib/diagrams/pie_diagram.rb', line 46

def find_slice(label)
  @slices.find { |s| s.label == label }
end

#identifiable_elementsHash{Symbol => Array<Diagrams::Elements::Slice>}

Returns a hash mapping element types to their collections for diffing.

Returns:

See Also:



71
72
73
74
75
# File 'lib/diagrams/pie_diagram.rb', line 71

def identifiable_elements
  {
    slices: @slices
  }
end

#to_h_contentHash{Symbol => String | Array<Hash>}

Returns the specific content of the pie diagram as a hash. Called by ‘Diagrams::Base#to_h`.

Returns:

  • (Hash{Symbol => String | Array<Hash>})


60
61
62
63
64
65
66
# File 'lib/diagrams/pie_diagram.rb', line 60

def to_h_content
  {
    title: @title,
    # Ensure slices include calculated percentage in their hash
    slices: @slices.map(&:to_h)
  }
end

#total_valueFloat

Calculates the total raw value of all slices.

Returns:

  • (Float)


52
53
54
# File 'lib/diagrams/pie_diagram.rb', line 52

def total_value
  @slices.sum(&:value)
end