Module: Doodle::DataTypes

Defined in:
lib/doodle/datatypes.rb

Instance Method Summary collapse

Instance Method Details

#boolean(name, params = { }, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/doodle/datatypes.rb', line 26

def boolean(name, params = { }, &block)
  define name, params, block, { } do
    must "be true or false" do |v|
      [true, false].include?(v)
    end
    from String, Symbol do |v|
      case v.to_s
      when /^(yes|true|on)$/
        true
      when /^(no|false|off)$/
        false
      else
        v
      end
    end
  end
end

#date(name, params = { }, &block) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/doodle/datatypes.rb', line 104

def date(name, params = { }, &block)
  define name, params, block, { :kind => Date } do
    from String do |s|
      Date.parse(s)
    end
    from Array do |y,m,d|
      Date.new(y, m, d)
    end
    from Integer do |jd|
      Date.new(*Date.jd_to_civil(jd))
    end
  end
end

#email(name, params = { }, &block) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/doodle/datatypes.rb', line 94

def email(name, params = { }, &block)
  # for max length, see http://www.imc.org/ietf-fax/archive2/msg01578.html
  # 384 = 128+1+255
  string(name, { :max => 384 }.merge(params), &block).instance_eval do
    must "be valid email address" do |s|
      s =~ RFC822::EmailAddress
    end
  end
end

#integer(name, params = { }, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/doodle/datatypes.rb', line 14

def integer(name, params = { }, &block)
  define name, params, block, { :kind => Integer } do
    from Float do |n|
      n.to_i
    end
    from String do |n|
      n =~ /[0-9]+(.[0-9]+)?/ or raise ArgumentError, "#{name} must be numeric"
      n.to_i
    end
  end
end

#string(name, params = { }, &block) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/doodle/datatypes.rb', line 52

def string(name, params = { }, &block)
  # must extract non-standard attributes before processing with
  # define otherwise causes UnknownAttribute error in Attribute definition
  if params.key?(:max)
    max = params.delete(:max)
  end
  if params.key?(:size)
    size = params.delete(:size)
    # size should be a Range
    size.kind_of?(Range) or raise ArgumentError, "#{name}: size should be a Range", [caller[-1]]
  end
  define name, params, block, { :kind => String } do
    from String do |s|
      s
    end
    from Integer do |i|
      i.to_s
    end
    from Symbol do |s|
      s.to_s
    end
    if max
      must "be <= #{max} characters" do |s|
        s.size <= max
      end
    end
    if size
      must "have size from #{size} characters" do |s|
        size.include?(s.size)
      end
    end
  end
end

#symbol(name, params = { }, &block) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/doodle/datatypes.rb', line 44

def symbol(name, params = { }, &block)
  define name, params, block, { :kind => Symbol } do
    from String do |s|
      s.to_sym
    end
  end
end

#uri(name, params = { }, &block) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/doodle/datatypes.rb', line 86

def uri(name, params = { }, &block)
  define name, params, block, { :kind => URI } do
    from String do |s|
      URI.parse(s)
    end
  end
end

#version(name, params = { }, &block) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/doodle/datatypes.rb', line 118

def version(name, params = { }, &block)
  define name, params, block, { :kind => String } do
    must "be of form n.n.n" do |str|
      str =~ /\d+\.\d+.\d+/
    end
    from Array do |a|
      a.size == 3 or raise ArgumentError, "#{name}: version array argument must contain exactly 3 elements", [caller[-1]]
      a.join('.')
    end
  end
end