Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
uoy-faculty-helpers / lib / faculty_helpers.rb
Size: Mime:
# frozen_string_literal: true

require_relative 'faculty_helpers/version'

# Top-level module documentation comment goes here...
module FacultyHelpers
  module_function

  # Output Yes or No for True or False
  def human_boolean(boolean)
    boolean ? 'Yes' : 'No'
  end

  def human_readable_bytes(bytes)
    return '0B' if bytes.zero?

    index = Math.log(bytes, 1024).floor
    formatted_bytes = (bytes.to_f / (1024**index)).round([0, 2, 2, 2, 3][index])
    unit = %w[B KiB MiB GiB TiB][index]
    "#{formatted_bytes} #{unit}"
  end

  # Convert a number into the corresponding day of the week 1 = Monday, 2 = Tuesday etc,
  def number_to_day(day)
    Date::DAYNAMES[day.to_i % 7]
  end

  # Convert a number into the corresponding short day of the week 1 = Mon, 2 = Tue etc,
  def number_to_short_day(day)
    Date::ABBR_DAYNAMES[day.to_i % 7]
  end

  # Shamelessly lifted from rails
  # (https://github.com/rails/rails/blob/master/activesupport/lib/active_support/locale/en.rb)
  def ordinal(number) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength
    case number
    when 1 then 'st'
    when 2 then 'nd'
    when 3 then 'rd'
    when 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 then 'th'
    else
      num_modulo = number.to_i.abs % 100
      num_modulo %= 10 if num_modulo > 13
      case num_modulo
      when 1 then 'st'
      when 2 then 'nd'
      when 3 then 'rd'
      else 'th'
      end
    end
  end

  # Shamelessly lifted from rails
  # (https://github.com/rails/rails/blob/master/activesupport/lib/active_support/locale/en.rb)
  def ordinalize(number)
    "#{number}#{ordinal(number)}"
  end

  # NB: \m is a Postgresish for "match the start of a word"
  #  Ruby doesn't know about this at all, but Sequel passes \b without adjustment,
  #  which doesn't work.  It'll also pass \m without adjustment.
  def search_regex(string)
    /\m#{Regexp.escape(string).gsub('\ ', '.*\m')}/i
  end
end