Class: Marktable::Row
- Inherits:
-
Object
- Object
- Marktable::Row
- Defined in:
- lib/marktable/row.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
Class Method Summary collapse
-
.parse(row_string) ⇒ Object
Parse a markdown row string into an array of values.
-
.separator?(row_string) ⇒ Boolean
Check if a row string represents a separator row.
-
.separator_row(column_widths) ⇒ Object
Generate a separator row for markdown table with specified widths.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
-
#initialize(data = {}, headers: nil) ⇒ Row
constructor
A new instance of Row.
- #keys ⇒ Object
- #to_a ⇒ Object
- #to_h ⇒ Object
-
#to_markdown(column_widths) ⇒ Object
Convert a row to markdown format with specified column widths.
- #values ⇒ Object
Constructor Details
#initialize(data = {}, headers: nil) ⇒ Row
Returns a new instance of Row.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/marktable/row.rb', line 7 def initialize(data = {}, headers: nil) @headers = headers if data.is_a?(Hash) # Ensure all hash values are strings @data = data.transform_values(&:to_s) elsif data.is_a?(Array) # Ensure all array elements are strings data_strings = data.map(&:to_s) @data = if headers && !headers.empty? # Convert array to hash using headers headers.each_with_index.each_with_object({}) do |(header, i), hash| hash[header] = i < data_strings.length ? data_strings[i] : '' end else # Keep as array when no headers data_strings end else @data = headers ? {} : [] end end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
5 6 7 |
# File 'lib/marktable/row.rb', line 5 def data @data end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
5 6 7 |
# File 'lib/marktable/row.rb', line 5 def headers @headers end |
Class Method Details
.parse(row_string) ⇒ Object
Parse a markdown row string into an array of values
80 81 82 |
# File 'lib/marktable/row.rb', line 80 def self.parse(row_string) row_string.strip.sub(/^\|/, '').sub(/\|$/, '').split('|').map(&:strip) end |
.separator?(row_string) ⇒ Boolean
Check if a row string represents a separator row
85 86 87 |
# File 'lib/marktable/row.rb', line 85 def self.separator?(row_string) row_string.strip.gsub(/[\|\-\s]/, '').empty? end |
.separator_row(column_widths) ⇒ Object
Generate a separator row for markdown table with specified widths
90 91 92 93 94 95 96 |
# File 'lib/marktable/row.rb', line 90 def self.separator_row(column_widths) separators = column_widths.map do |width| '-' * [3, width].max end ["| #{separators.join(' | ')} |", column_widths] end |
Instance Method Details
#[](key) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/marktable/row.rb', line 31 def [](key) if @data.is_a?(Hash) @data[key] elsif key.is_a?(Integer) && key < @data.length @data[key] else nil end end |
#[]=(key, value) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/marktable/row.rb', line 41 def []=(key, value) if @data.is_a?(Hash) @data[key] = value.to_s elsif key.is_a?(Integer) @data[key] = value.to_s end end |
#keys ⇒ Object
53 54 55 |
# File 'lib/marktable/row.rb', line 53 def keys @data.is_a?(Hash) ? @data.keys : (0...@data.size).to_a end |
#to_a ⇒ Object
66 67 68 |
# File 'lib/marktable/row.rb', line 66 def to_a @data.is_a?(Array) ? @data : @data.values end |
#to_h ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/marktable/row.rb', line 57 def to_h return @data if @data.is_a?(Hash) return {} if @data.empty? || @headers.nil? || @headers.empty? @headers.each_with_index.each_with_object({}) do |(header, i), hash| hash[header] = i < @data.length ? @data[i] : '' end end |
#to_markdown(column_widths) ⇒ Object
Convert a row to markdown format with specified column widths
71 72 73 74 75 76 77 |
# File 'lib/marktable/row.rb', line 71 def to_markdown(column_widths) vals = values formatted_values = vals.each_with_index.map do |val, i| val.to_s.ljust(column_widths[i] || val.to_s.length) end "| #{formatted_values.join(' | ')} |" end |
#values ⇒ Object
49 50 51 |
# File 'lib/marktable/row.rb', line 49 def values @data.is_a?(Hash) ? @data.values : @data end |