class Array

utilities for ruby hashes and ruby arrays

Copyright © 2021 Stephan Wenzel <stephan.wenzel@drwpatent.de>

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.

utilities for ruby hashes and ruby arrays

Copyright © 2021 Stephan Wenzel <stephan.wenzel@drwpatent.de>

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.

utilities for ruby hashes and ruby arrays

Copyright © 2021 Stephan Wenzel <stephan.wenzel@drwpatent.de>

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.

utilities for ruby hashes and ruby arrays

Copyright © 2021 Stephan Wenzel <stephan.wenzel@drwpatent.de>

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.

Public Instance Methods

group_by_headings(*headings) click to toggle source

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]}}},
 …
} ->
# 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 ) click to toggle source
# 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={}) { |value, column_index, row_index| ... } click to toggle source
# 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) click to toggle source
# 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) click to toggle source

to_html: creates a html table from a rectangular array

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

# 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_list() click to toggle source
# 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: " | " ) click to toggle source

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
# 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

Private Instance Methods

html_tag(type, *content, &block) click to toggle source
# File lib/hash_base/array/to_html.rb, line 85
def html_tag(type, *content, &block)
  if block_given?
    opts = content.first.to_h.map{|k,v| " #{k}='#{v}'"}.join
    "<#{type}#{opts}>#{yield block}</#{type}>"
  else
    opts = content[1].to_h.map{|k,v| " #{k}='#{v}'"}.join
    "<#{type}#{opts}>#{content.first}</#{type}>"
  end
end
style_value(value, options={}, &block) click to toggle source
# File lib/hash_base/array/to_html.rb, line 73
def style_value(value, options={}, &block)
  case value.class
  when Integer
    value.to_s
  when Numeric
    p = options[:precision] || 2
    sprintf("%.#{p}f",value)
  else
    options[:html_safe] ? value.to_s : CGI::escapeHTML(value.to_s)
  end 
end