Module: ICU::Util::Accessor

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

Overview

Miscellaneous accessor helpers.

Instance Method Summary collapse

Instance Method Details

#attr_accessor(name, &block) ⇒ Object



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

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



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

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

#attr_date_or_nil(*names) ⇒ Object



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

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::Date::parse(tmp)
        raise "invalid date or nil (#{val}) for #{name}" unless tmp
      end
      tmp
    end
  end
end

#attr_integer(*names) ⇒ Object



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

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



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

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



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

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



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

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



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

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



157
158
159
160
161
162
163
164
165
166
# File 'lib/icu_tournament/util.rb', line 157

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