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



158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/graphkit.rb', line 158

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



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/graphkit.rb', line 171

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