Module: Hbtrack::Util

Defined in:
lib/hbtrack/util.rb

Overview

This class contains the methods that are used to format the progress of a Habit into string

Constant Summary collapse

FONT_COLOR =
{
  green: "\e[32m",
  red: "\e[31m",
  blue: "\e[34m"
}.freeze

Class Method Summary collapse

Class Method Details

.convert_key_to_date(key, no_of_space) ⇒ String

Convert key into date in string form.

Example

Util.convert_key_to_date(:"2017,7", 0)
#=> "July 2016 : "

Parameters:

  • key (Symbol)

    The key of the progress in the form of :‘year, month’. Example: :“2017,7”

  • no_of_space (Numeric)

    number of space to be added in front

Returns:

  • (String)

    a string in date form.



51
52
53
54
55
# File 'lib/hbtrack/util.rb', line 51

def convert_key_to_date(key, no_of_space)
  year = key.to_s.split(',')[0]
  ' ' * no_of_space + get_month_from(key) +
    " #{year}" + ' : '
end

.get_date_from(key:) ⇒ Object



57
58
59
60
# File 'lib/hbtrack/util.rb', line 57

def get_date_from(key:)
  date_component = key.to_s.split(',').map(&:to_i)
  Date.new(date_component[0], date_component[1], 1)
end

.get_month_from(key) ⇒ String

Get the month in string form from given key

Example

Util.get_month_from(:"2017,7")
#=> "July"

Parameters:

  • key (Symbol)

    The key of the progress

Returns:

  • (String)

    month



71
72
73
74
# File 'lib/hbtrack/util.rb', line 71

def get_month_from(key)
  key = key.to_s.split(',')
  Date::MONTHNAMES[key[1].to_i]
end

.title(string) ⇒ Nil

Format the string with title style.

Example

puts Util.title("Title")
# Title
# -----
#=> nil

Parameters:

  • string (String)

    the string to be styled as title

Returns:

  • (Nil)


34
35
36
37
# File 'lib/hbtrack/util.rb', line 34

def title(string)
  string + "\n" +
    '-' * string.length + "\n"
end