Class: TableTransformer::Data
- Inherits:
-
Object
- Object
- TableTransformer::Data
- Includes:
- Helper
- Defined in:
- lib/table_transformer.rb
Instance Attribute Summary collapse
-
#keys ⇒ Object
readonly
Returns the value of attribute keys.
-
#original ⇒ Object
readonly
Returns the value of attribute original.
Instance Method Summary collapse
- #column_width ⇒ Object
-
#initialize(original:, keys: []) ⇒ Data
constructor
A new instance of Data.
- #line_generator ⇒ Object
- #max_size ⇒ Object
- #output ⇒ Object
- #output_data ⇒ Object
- #output_data_given_data ⇒ Object
- #string_data ⇒ Object
Methods included from Helper
Constructor Details
#initialize(original:, keys: []) ⇒ Data
Returns a new instance of Data.
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/table_transformer.rb', line 10 def initialize(original:, keys: []) @original = if original.class.method_defined?(:keys) [] << original else [] + original end @keys = if keys.blank? @original[0].keys else keys end end |
Instance Attribute Details
#keys ⇒ Object (readonly)
Returns the value of attribute keys.
9 10 11 |
# File 'lib/table_transformer.rb', line 9 def keys @keys end |
#original ⇒ Object (readonly)
Returns the value of attribute original.
9 10 11 |
# File 'lib/table_transformer.rb', line 9 def original @original end |
Instance Method Details
#column_width ⇒ Object
69 70 71 72 73 74 75 76 77 |
# File 'lib/table_transformer.rb', line 69 def column_width @column_width ||= {}.tap do |width| keys.each do |k| width[k] = max_size[k] < width(k) ? width(k) : max_size[k] width[k] += 2 end break width end end |
#line_generator ⇒ Object
63 64 65 66 67 |
# File 'lib/table_transformer.rb', line 63 def line_generator @line_generator ||= LineGenerator.new( column_width: column_width ) end |
#max_size ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'lib/table_transformer.rb', line 79 def max_size @max_size ||= {}.tap do |size| keys.each do |k| max_data = string_data.max_by { |data| width(data[k]) } size[k] = width(max_data[k]) end break size end end |
#output ⇒ Object
23 24 25 |
# File 'lib/table_transformer.rb', line 23 def output puts output_data end |
#output_data ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/table_transformer.rb', line 27 def output_data @output_data ||= if string_data.present? output_data_given_data else "Empty set\n\n" end end |
#output_data_given_data ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/table_transformer.rb', line 36 def output_data_given_data [].tap do |arr| arr << line_generator.delimiter_line arr << line_generator.header_line arr << line_generator.delimiter_line string_data.each do |data| arr << line_generator.data_line(data) end arr << line_generator.delimiter_line arr << "#{string_data.size} rows in set\n\n" end end |
#string_data ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/table_transformer.rb', line 49 def string_data @string_data ||= [].tap do |arr| original.each do |data| transformed_data = {}.tap do |t_data| keys.each do |k| t_data[k] = data[k].to_s end break t_data end arr << transformed_data if transformed_data.present? end end end |