Sha256: d15aad81d0ea8d24b91c2b457334378c3d363d762990f2903510a3493675784c

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

# encoding: utf-8
require 'open_classes/string'

# Kernel
module Kernel
  # Ascii Art Ancestors
  #
  # === Examples
  #
  # define class
  #
  #   class BaseHogeForAncestors;end
  #   class HogeForAncestors < BaseHogeForAncestors;end
  #
  # execute aa_ancestors
  #
  #   puts HogeForAncestors.aa_ancestors
  #
  # result is ...
  #
  #   ----------------------
  #   |     BasicObject    |
  #   ----------------------
  #             |
  #   ----------------------
  #   |       Kernel       |
  #   ----------------------
  #             |
  #   ----------------------
  #   |       Object       |
  #   ----------------------
  #             |
  #   ----------------------
  #   |BaseHogeForAncestors|
  #   ----------------------
  #             |
  #   ----------------------
  #   |  HogeForAncestors  |
  #   ----------------------
  def aa_ancestors
    max_class = max_size_class
    max = max_class.to_s.size
    ca = centered_ancestors(max)
    ba = surrounded_ancestors(ca)
    connected_box_ancestors(ba, max)
  end

  private

  def max_size_class
    ancestors.max_by { |v|v.to_s.size }
  end

  def centered_ancestors(max)
    ancestors.reverse.map { |v|v.to_s.center max }
  end

  def surrounded_ancestors(ca)
    ca.reduce([]) { |a, e|a << e.surround }
  end

  def connected_box_ancestors(ba, max)
    ba.join(ancestors_connector(max))
  end

  def ancestors_connector(max)
    connector = '|'.rjust((max + 2) / 2)
    "\n#{connector}\n"
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tbpgr_utils-0.0.151 lib/open_classes/kernel/aa_ancestors.rb