Module: ICU::Accessor

Included in:
Player, Result, Tournament
Defined in:
lib/icu_tournament/util.rb

Instance Method Summary collapse

Instance Method Details

#attr_accessor(name, &block) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/icu_tournament/util.rb', line 49

def attr_accessor(name, &block)
  attr_reader name
  if block
    define_method("#{name}=") do |val|
      val = block.call(val)
      instance_variable_set("@#{name}", val)
    end
  end
end

#attr_date(*names) ⇒ Object



109
110
111
112
113
114
115
116
117
118
# File 'lib/icu_tournament/util.rb', line 109

def attr_date(*names)
  names.each do |name|
    attr_accessor(name) do |val|
      tmp = val.to_s.strip
      tmp = ICU::Util::parsedate(tmp)
      raise "invalid date (#{val}) for #{name}" unless tmp
      tmp
    end
  end
end

#attr_date_or_nil(*names) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/icu_tournament/util.rb', line 120

def attr_date_or_nil(*names)
  names.each do |name|
    attr_accessor(name) do |val|
      tmp = val.to_s.strip
      if tmp == ''
        tmp = nil
      else
        tmp = ICU::Util::parsedate(tmp)
        raise "invalid date or nil (#{val}) for #{name}" unless tmp
      end
      tmp
    end
  end
end

#attr_integer(*names) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/icu_tournament/util.rb', line 59

def attr_integer(*names)
  names.each do |name|
    attr_accessor(name) do |val|
      tmp = val.to_i
      raise "invalid integer (#{val}) for #{name}" unless val.is_a?(Fixnum) || (val.is_a?(String) && val.include?(tmp.to_s))
      tmp
    end
  end
end

#attr_integer_or_nil(*names) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/icu_tournament/util.rb', line 69

def attr_integer_or_nil(*names)
  names.each do |name|
    attr_accessor(name) do |val|
      tmp = case val
        when nil      then nil
        when Fixnum   then val
        when /^\s*$/  then nil
        else val.to_i
      end
      raise "invalid integer (#{val}) for #{name}" if tmp == 0 && val.is_a?(String) && !val.include?('0')
      tmp
    end
  end
end

#attr_positive(*names) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/icu_tournament/util.rb', line 84

def attr_positive(*names)
  names.each do |name|
    attr_accessor(name) do |val|
      tmp = val.to_i
      raise "invalid positive integer (#{val}) for #{name}" unless tmp > 0
      tmp
    end
  end
end

#attr_positive_or_nil(*names) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/icu_tournament/util.rb', line 94

def attr_positive_or_nil(*names)
  names.each do |name|
    attr_accessor(name) do |val|
      tmp = case val
        when nil      then nil
        when Fixnum   then val
        when /^\s*$/  then nil
        else val.to_i
      end
      raise "invalid positive integer or nil (#{val}) for #{name}" unless tmp.nil? || tmp > 0
      tmp
    end
  end
end

#attr_string(regex, *names) ⇒ Object



135
136
137
138
139
140
141
142
143
# File 'lib/icu_tournament/util.rb', line 135

def attr_string(regex, *names)
  names.each do |name|
    attr_accessor(name) do |val|
      tmp = val.to_s.strip
      raise "invalid #{name} (#{val})" unless tmp.match(regex)
      tmp
    end
  end
end

#attr_string_or_nil(regex, *names) ⇒ Object



145
146
147
148
149
150
151
152
153
154
# File 'lib/icu_tournament/util.rb', line 145

def attr_string_or_nil(regex, *names)
  names.each do |name|
    attr_accessor(name) do |val|
      tmp = val.to_s.strip
      tmp = nil if tmp == ''
      raise "invalid #{name} (#{val})" unless tmp.nil? || tmp.match(regex)
      tmp
    end
  end
end