Class: Diagrams::PieDiagram
Overview
Represents a Pie Chart diagram consisting of slices.
Instance Attribute Summary collapse
-
#slices ⇒ Object
readonly
Returns the value of attribute slices.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Attributes inherited from Base
Class Method Summary collapse
-
.from_h(data_hash, version:, checksum:) ⇒ PieDiagram
Class method to create a PieDiagram from a hash.
Instance Method Summary collapse
-
#add_slice(slice, update_checksum: true, initial_load: false) ⇒ Element::Slice
Adds a slice to the diagram.
-
#find_slice(label) ⇒ Element::Slice?
Finds a slice by its label.
-
#identifiable_elements ⇒ Hash{Symbol => Array<Diagrams::Elements::Slice>}
Returns a hash mapping element types to their collections for diffing.
-
#initialize(title: '', slices: [], version: 1) ⇒ PieDiagram
constructor
Initializes a new PieDiagram.
-
#to_h_content ⇒ Hash{Symbol => String | Array<Hash>}
Returns the specific content of the pie diagram as a hash.
-
#total_value ⇒ Float
Calculates the total raw value of all slices.
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.
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
#slices ⇒ Object (readonly)
Returns the value of attribute slices.
6 7 8 |
# File 'lib/diagrams/pie_diagram.rb', line 6 def slices @slices end |
#title ⇒ Object (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`.
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
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.
46 47 48 |
# File 'lib/diagrams/pie_diagram.rb', line 46 def find_slice(label) @slices.find { |s| s.label == label } end |
#identifiable_elements ⇒ Hash{Symbol => Array<Diagrams::Elements::Slice>}
Returns a hash mapping element types to their collections for diffing.
71 72 73 74 75 |
# File 'lib/diagrams/pie_diagram.rb', line 71 def identifiable_elements { slices: @slices } end |
#to_h_content ⇒ Hash{Symbol => String | Array<Hash>}
Returns the specific content of the pie diagram as a hash. Called by ‘Diagrams::Base#to_h`.
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_value ⇒ Float
Calculates the total raw value of all slices.
52 53 54 |
# File 'lib/diagrams/pie_diagram.rb', line 52 def total_value @slices.sum(&:value) end |