Module: DateHumanize

Defined in:
lib/date_humanize.rb,
lib/date_humanize/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.aprox_value(months, years) ⇒ Object



42
43
44
# File 'lib/date_humanize.rb', line 42

def self.aprox_value months, years
  "#{prefix_years years}#{pluralize_months months}"
end

.humanize_days(days) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/date_humanize.rb', line 7

def self.humanize_days days
  years = days/365

  if years > 0
    days -= years*365
  end

  months = days/30

  "#{print_days days, years}#{print_aprox days, months, years}"
end

.limit_days_to_show_aprox(months) ⇒ Object



46
47
48
# File 'lib/date_humanize.rb', line 46

def self.limit_days_to_show_aprox months
  (months*30) + (months.to_f/2).round
end

.pluralize_days(days) ⇒ Object



50
51
52
# File 'lib/date_humanize.rb', line 50

def self.pluralize_days days
  "#{pluralize(days, 'dia', 'dias')}"
end

.pluralize_months(months) ⇒ Object



54
55
56
# File 'lib/date_humanize.rb', line 54

def self.pluralize_months months
  "#{pluralize(months, 'mês', 'meses')}"
end

.pluralize_years(years) ⇒ Object



58
59
60
# File 'lib/date_humanize.rb', line 58

def self.pluralize_years years
  "#{pluralize(years, 'ano', 'anos')}"
end

.prefix_years(years) ⇒ Object



38
39
40
# File 'lib/date_humanize.rb', line 38

def self.prefix_years years
  "#{pluralize_years years} e " if years > 0
end


25
26
27
28
# File 'lib/date_humanize.rb', line 25

def self.print_aprox days, months, years
  return "" unless show_aprox? days, months
  return " (aprox. #{aprox_value months, years})"
end


19
20
21
22
23
# File 'lib/date_humanize.rb', line 19

def self.print_days days, years
  return "#{prefix_years(years)}1 semana" if days == 7
  return pluralize_years years if days%365 == 0
  return "#{prefix_years(years)}#{pluralize_days days}"
end

.show_aprox?(days, months) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
# File 'lib/date_humanize.rb', line 30

def self.show_aprox? days, months
  return false if days < 30
  return false if days%365 == 0
  return false if days > limit_days_to_show_aprox(months)

  return true
end