module Kit

To be mixed in to a Hash. Basically allows access the elements of a hash via method names rather than brackets.

Public Instance Methods

check(*values) { |instance_eval(expression), test_data| ... } click to toggle source
# 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
method_missing(method, *args) click to toggle source
# 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