Module: Kit

Included in:
GraphKit, KitHash
Defined in:
lib/graphkit.rb

Defined Under Namespace

Classes: IntegrityError

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/graphkit.rb', line 184

def method_missing(method, *args)
#     p method, args
  m = method.to_s
  if m =~ /=$/
    name = m.chop.to_s
    self.class.send(:define_method, method){|arg| self[name.to_sym] = arg}
    return send(method, args[0])
  else
    self.class.send(:define_method, method){self[method]}
    return send(method)
  end
end

Instance Method Details

#check(*values, &block) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/graphkit.rb', line 197

def check(*values, &block)
  values.each do |arr|
    case arr.size 
    when 2
      expression, test_data = arr
      if block
        raise IntegrityError.new("#{expression} failed argument correctness test (value given was '#{self[expression].inspect}')") unless yield(instance_eval(expression), test_data)
      else
        is_array = test_data.class == Array
        raise IntegrityError.new("#{expression} was  #{instance_eval(expression)} instead of #{test_data.inspect}") unless (is_array ? test_data.include?(instance_eval(expression)) : instance_eval(expression) == test_data)
      end
    when 3
      string, value, test_data = arr
      if block
        raise IntegrityError.new("#{string} failed argument correctness test (value given was '#{value.inspect}')") unless yield(value, test_data)
      else
        is_array = test_data.class == Array
        raise IntegrityError.new("#{string} was #{value.inspect} instead of #{test_data.inspect}") unless (is_array ? test_data.include?(value) : value == test_data)
      end
    else
      raise "Bad value checking data: #{arr.inspect}"
    end
  end
end