Module: Str2Duck::Converter

Extended by:
Converter
Included in:
Converter
Defined in:
lib/str2duck/converter.rb

Instance Method Summary collapse

Instance Method Details

#date(obj) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/str2duck/converter.rb', line 20

def date(obj)
  if Str2Duck::Matcher.date?(obj)
    begin
      return Date.parse(obj)
    rescue NoMethodError
      time_parts= obj.scan(/\d+/).map(&:to_i)
      return Time.new(*time_parts)
    end
  end; nil
end

#datetime(obj) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/str2duck/converter.rb', line 5

def datetime(obj)
  if Str2Duck::Matcher.datetime?(obj)
    if defined?(DateTime) && DateTime.respond_to?(:parse)
      return DateTime.parse(obj)
    # else
    #   if time_parts.count == 8
    #     2.times { time_parts.pop }
    #   elsif time_parts.count == 6
    #     return nil
    #   end
    #   return Time.new(*time_parts)
    end
  end; nil
end

#false(obj) ⇒ Object



49
50
51
52
53
# File 'lib/str2duck/converter.rb', line 49

def false(obj)
  if Str2Duck::Matcher.false?(obj)
    return false
  end; nil
end

#float(obj) ⇒ Object



55
56
57
58
59
# File 'lib/str2duck/converter.rb', line 55

def float(obj)
  if Str2Duck::Matcher.float?(obj)
    return obj.sub(',', '.').to_f
  end; nil
end

#integer(obj) ⇒ Object



61
62
63
64
65
# File 'lib/str2duck/converter.rb', line 61

def integer(obj)
  if Str2Duck::Matcher.integer?(obj)
    return obj.to_i
  end; nil
end

#json(obj) ⇒ Object



67
68
69
70
71
# File 'lib/str2duck/converter.rb', line 67

def json(obj)
  if Str2Duck::Matcher.json?(obj)
    return JSON.parse(obj)
  end; nil
end

#time(obj) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/str2duck/converter.rb', line 31

def time(obj)
  if Str2Duck::Matcher.time?(obj)
    begin
      return Time.parse(obj)
    rescue NoMethodError
      time_parts= obj.scan(/\d+/).map(&:to_i)
      1.times { time_parts.pop }
      return Time.new(*time_parts)
    end
  end; nil
end

#true(obj) ⇒ Object



43
44
45
46
47
# File 'lib/str2duck/converter.rb', line 43

def true(obj)
  if Str2Duck::Matcher.true?(obj)
    return true
  end; nil
end