Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/hash_base/array/to_html.rb,
lib/hash_base/array/to_list.rb,
lib/hash_base/array/to_text.rb,
lib/hash_base/array/grouping.rb

Overview

utilities for ruby hashes and ruby arrays

Copyright © 2021 Stephan Wenzel <[email protected]>

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Defined Under Namespace

Classes: ArrayNotRectangular, ArrayRowIsNotAHash

Instance Method Summary collapse

Instance Method Details

#group_by_headings(*headings) ⇒ Object

uses arrays first line with headings to group

group_by_headings: creates a nested hash from an array, like group_by

but with many headings at a time
REMOVES: first line of array

-> arbitray number of headings, example ‘myarray.group_by_headings(“name”, “place”)’

-> array

nested hash ->

{key0=>{key1=>{key2=>{key3=>[ array remaining array elemnts]}}},
 key0=>{key1=>{key2=>{key3=>[ array remaining array elemnts]}}},
 …
} ->


90
91
92
93
94
95
# File 'lib/hash_base/array/grouping.rb', line 90

def group_by_headings(*headings)
  copy       = dup
  header_row = copy.shift
  indices    = headings.map {|h| header_row.find_index(h) }
  copy.group_by_positions(*indices)
end

#group_by_positions(*positions) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/hash_base/array/grouping.rb', line 42

def group_by_positions( *positions )

  ohash = ActiveSupport::OrderedHash.new
  
  each do |subarray|
    
    case subarray
    when Array
      key = subarray.delete_at(positions[0])
    else
      raise ArrayNotRectangular
    end
    
    if ohash.has_key?(key)
      ohash[key] << subarray
    else
      ohash[key]  = [subarray]
    end
  end #each
  
  if positions.length > 1
    positions.map!{|p| p > positions[0] ? p - 1 : p }
    ohash.group_by_positions( *positions.drop(1) ) 
  else
    ohash
  end
  
end

#table_row(row_index, options = {}, &block) ⇒ Object

def



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/hash_base/array/to_html.rb', line 55

def table_row(row_index, options={}, &block)
  html_tag(:tr, options[:row].to_h.compact) do
    row = collect.with_index do |value, column_index| 
      html_tag(options.dig(:cell, :type) || :td, options[:cell].to_h.except(:type).compact) do
        cell_value = if block_given?
          yield value, column_index, row_index
        else
          style_value(value, options)
        end
      end #html_tag #cell_type
    end.join("") #collect columns
    row
  end #html_tag #tr
end

#table_row_group(options = {}, &block) ⇒ Object



48
49
50
51
52
53
# File 'lib/hash_base/array/to_html.rb', line 48

def table_row_group(options={}, &block)
  rows =  collect.with_index do |row, row_index|
    row.table_row(row_index, options, &block)
  end.join("")
  rows.present? ? html_tag(options.dig(:row_group, :type) || :tbody, rows) : ""
end

#to_html(options = {}, &block) ⇒ Object

to_html: creates a html table from a rectangular array

options: headings: true/false, footer: true/false



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/hash_base/array/to_html.rb', line 31

def to_html(options={}, &block)

  options[:table] = options[:table].to_h.merge({:class => options[:table_class]}.compact) # support legacy
  
  t          = dup
        body = t
  head, body = [t.shift, t] if options[:headings]
  foot, body = [t.pop  , t] if options[:footer]
  
  html_tag(:table, options[:table].to_h.compact) do
    [head].compact.table_row_group(options.merge(:row_group => {:type => :thead}, :cell => {:type => :th}), &block) +
      body.compact.table_row_group(options.merge(:row_group => {:type => :tbody}, :cell => {:type => :td}), &block) +
    [foot].compact.table_row_group(options.merge(:row_group => {:type => :tfoot}, :cell => {:type => :td}), &block) 
  end
end

#to_listObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/hash_base/array/to_list.rb', line 31

def to_list
  # test, if each element is a hash
  each do |hash|
    raise ArrayRowIsNotAHash unless hash.is_a?(Hash)
  end
  # extract all keys from all hashes
  keys = map do |hash|
    hash.keys
  end.flatten.uniq
  # now extract value for each key in each hash
  map do |hash|
    keys.map do |key|
      hash[key]
    end
  end.unshift(keys)
end

#to_text(indent: 15, precision: 2, padding: " | ") ⇒ Object

creates a table from a rectangular array, balanced with indent

indent:              (Integer) number of spaces for padding each table element
precision            (Integer) number of precision digits to print for floats
padding              (String)  string between adjacing colums


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/hash_base/array/to_text.rb', line 32

def to_text(indent: 15, precision: 2, padding: " | " )
  map do |line| 
    ln = line.is_a?(Array) ? line : [line]
    ln.map do |e| 
      case e 
      when Integer
        e.to_s.rjust(indent)
      when Complex, Rational
        e.to_s.rjust(indent)
      when Numeric
        sprintf("%.#{precision}f",e).rjust(indent)
      else
        e.to_s.ljust(indent)
      end
    end.join(padding)
  end.join("\n")
end