Module: Str2Duck::Format

Defined in:
lib/str2duck/format.rb

Class Method Summary collapse

Class Method Details

.date(obj) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/str2duck/format.rb', line 23

def date obj
  if Str2Duck::Regexp.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



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/str2duck/format.rb', line 6

def datetime obj
  if Str2Duck::Regexp.datetime?(obj)
    if defined? DateTime
      return DateTime.parse obj
    else
      time_parts= obj.scan(/\d+/).map(&:to_i)
      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



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

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

.float(obj) ⇒ Object



62
63
64
65
66
67
# File 'lib/str2duck/format.rb', line 62

def float obj
  if Str2Duck::Regexp.float?(obj)
    return obj.to_f
  end
  nil
end

.integer(obj) ⇒ Object



69
70
71
72
73
74
# File 'lib/str2duck/format.rb', line 69

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

.json(obj) ⇒ Object



76
77
78
79
80
81
# File 'lib/str2duck/format.rb', line 76

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

.time(obj) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/str2duck/format.rb', line 35

def time obj
  if Str2Duck::Regexp.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



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

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

.yaml(obj) ⇒ Object

damn, this thing eats almost everything…



84
85
86
87
88
89
# File 'lib/str2duck/format.rb', line 84

def yaml obj
  if Str2Duck::Regexp.yaml?(obj)
    return YAML.load(obj)
  end
  nil
end