Class: Marktable::Row
- Inherits:
-
Object
- Object
- Marktable::Row
- Defined in:
- lib/marktable/row.rb
Instance Attribute Summary collapse
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#values ⇒ Object
readonly
Returns the value of attribute values.
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 is a separator row.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Access a value by index or header.
-
#[]=(key, value) ⇒ Object
Set a value by index or header.
-
#headers? ⇒ Boolean
Check if this row uses headers.
-
#initialize(data, headers: nil) ⇒ Row
constructor
A new instance of Row.
-
#to_hash ⇒ Object
Convert row data to a hash using headers as keys.
-
#to_markdown(column_widths) ⇒ Object
Format the row as a markdown table row.
Constructor Details
#initialize(data, headers: nil) ⇒ Row
Returns a new instance of Row.
7 8 9 10 |
# File 'lib/marktable/row.rb', line 7 def initialize(data, headers: nil) @headers = headers @values = extract_values(data) end |
Instance Attribute Details
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
5 6 7 |
# File 'lib/marktable/row.rb', line 5 def headers @headers end |
#values ⇒ Object (readonly)
Returns the value of attribute values.
5 6 7 |
# File 'lib/marktable/row.rb', line 5 def values @values end |
Class Method Details
.parse(row_string) ⇒ Object
Parse a markdown row string into an array of values
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/marktable/row.rb', line 71 def self.parse(row_string) # Skip if nil or empty return [] if row_string.nil? || row_string.strip.empty? # Remove leading/trailing pipes and split by pipe cells = row_string.strip.sub(/^\|/, '').sub(/\|$/, '').split('|') # Trim whitespace from each cell cells.map(&:strip) end |
.separator?(row_string) ⇒ Boolean
Check if a row is a separator row
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/marktable/row.rb', line 83 def self.separator?(row_string) # Skip if nil or empty return false if row_string.nil? || row_string.strip.empty? # Remove pipes and strip whitespace content = row_string.gsub('|', '').strip # Check if it contains only dashes and colons (separator characters) content.match?(/^[\s:,-]+$/) && content.include?('-') end |
Instance Method Details
#[](key) ⇒ Object
Access a value by index or header
31 32 33 34 35 36 37 38 |
# File 'lib/marktable/row.rb', line 31 def [](key) if key.is_a?(Integer) @values[key] elsif @headers idx = @headers.index(key) idx ? @values[idx] : nil end end |
#[]=(key, value) ⇒ Object
Set a value by index or header
41 42 43 44 45 46 47 48 |
# File 'lib/marktable/row.rb', line 41 def []=(key, value) if key.is_a?(Integer) @values[key] = value if key >= 0 && key < @values.size elsif @headers idx = @headers.index(key) @values[idx] = value if idx && idx < @values.size end end |
#headers? ⇒ Boolean
Check if this row uses headers
51 52 53 |
# File 'lib/marktable/row.rb', line 51 def headers? !@headers.nil? end |
#to_hash ⇒ Object
Convert row data to a hash using headers as keys
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/marktable/row.rb', line 56 def to_hash return {} unless @headers result = {} @values.each_with_index do |value, i| # Only include values that have a corresponding header if i < @headers.size header = @headers[i] result[header] = value if header end end result end |
#to_markdown(column_widths) ⇒ Object
Format the row as a markdown table row
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/marktable/row.rb', line 13 def to_markdown(column_widths) vals = @values # Limit values to either headers count or column_widths size, whichever is appropriate max_cols = @headers ? @headers.size : column_widths.size formatted_values = column_widths.take(max_cols).map.with_index do |width, i| if i < vals.size vals[i].to_s.ljust(width) else ''.ljust(width) end end "| #{formatted_values.join(' | ')} |" end |