Module: AMarmita::Helpers

Extended by:
Helpers
Included in:
Helpers
Defined in:
lib/a_marmita/helpers.rb

Constant Summary collapse

MONTH_NAMES_PT =
["", "janeiro", "fevereiro", "março", "abril", "maio", "junho", "julho", "agosto", "setembro", "outubro", "novembro", "dezembro"]

Instance Method Summary collapse

Instance Method Details

#blank?(object) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
# File 'lib/a_marmita/helpers.rb', line 60

def blank?(object)
  case object
  when String
    object == ''
  else
    object.nil?
  end
end

#cart_date_parser(string) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/a_marmita/helpers.rb', line 32

def cart_date_parser(string)
  return nil if string.nil?

  day, month = *string.scan(/.* - ([0-9]*) (.*)/).flatten

  format_date "#{Date.today.year}-#{get_month_number(month)}-#{day}"
end

#date_parser(date) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/a_marmita/helpers.rb', line 40

def date_parser(date)
  if date.is_a?(String)
    date = Date.parse(date) rescue nil
  end

  date
end

#format_date(date) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/a_marmita/helpers.rb', line 48

def format_date(date)
  date = date_parser(date)

  date = (Date.today + 1) unless date.respond_to?(:strftime)

  date.strftime('%Y-%m-%d')
end

#get_month_number(month) ⇒ Object



56
57
58
# File 'lib/a_marmita/helpers.rb', line 56

def get_month_number(month)
  MONTH_NAMES_PT.index(month.downcase)
end

#to_float(string) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/a_marmita/helpers.rb', line 24

def to_float(string)
  return 0 if string.nil?
  
  values = string.scan(/\d+[,.]*\d*/).flatten.map(&:to_f)

  values.length > 1 ? values : values.first
end

#to_int(string) ⇒ Object



20
21
22
# File 'lib/a_marmita/helpers.rb', line 20

def to_int(string)
  string.nil? ? 0 : string.gsub(/[^0-9]/, '').to_i
end

#try(object, *a, &b) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/a_marmita/helpers.rb', line 12

def try(object, *a, &b)
  if a.empty? && block_given?
    yield object
  else
    object.respond_to?(a.first) ? object.public_send(*a, &b) : nil
  end
end