Module: Aska::InstanceMethods
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args) ⇒ Object
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/aska.rb', line 67
def method_missing(m, *args)
if self.class.defined_rules.has_key?("#{m}")
self.class.send(:define_method, m) do
self.class.look_up_rules(m)
end
self.send m
else
super
end
end
|
Instance Method Details
#attr_accessor?(name) ⇒ Boolean
61
62
63
|
# File 'lib/aska.rb', line 61
def attr_accessor?(name)
self.class.attr_accessors.include?(name.to_sym)
end
|
#get_var(name) ⇒ Object
Get the variable from the class If it’s defined as an attr_accessor, we know it has been defined as a rule Otherwise, if we are passing it as a
58
59
60
|
# File 'lib/aska.rb', line 58
def get_var(name)
attr_accessor?(name) ? self.send(name.to_sym) : (supported_method?(name) ? name.to_sym : name.to_f)
end
|
#rules ⇒ Object
35
36
37
|
# File 'lib/aska.rb', line 35
def rules
@rules ||= self.class.defined_rules
end
|
#supported_method?(meth) ⇒ Boolean
64
65
66
|
# File 'lib/aska.rb', line 64
def supported_method?(meth)
%w(< > == => =<).include?("#{meth}")
end
|
#valid_rule?(rule) ⇒ Boolean
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/aska.rb', line 44
def valid_rule?(rule)
return false unless rule rule.each do |key,value|
begin
return self.send(key).send(value[0].to_sym, get_var(value[1]))
rescue Exception => e
return false
end
end
end
|
#valid_rules?(name = :rules) ⇒ Boolean
38
39
40
41
42
43
|
# File 'lib/aska.rb', line 38
def valid_rules?(name=:rules)
arr = self.class.look_up_rules(name).collect do |rule|
valid_rule?(rule)
end
arr.reject {|a| a == true }.empty?
end
|