Module: Aska::InstanceMethods

Defined in:
lib/aska.rb

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

Returns:

  • (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

#rulesObject



35
36
37
# File 'lib/aska.rb', line 35

def rules
  @rules ||= self.class.defined_rules
end

#supported_method?(meth) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/aska.rb', line 64

def supported_method?(meth)
  %w(< > == => =<).include?("#{meth}")
end

#valid_rule?(rule) ⇒ Boolean

Returns:

  • (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 # Can't apply a rule that is nil, can we?
  rule.each do |key,value|
    begin
      # puts "testing if #{key} (#{self.send(key)}) is #{value[0]} #{get_var(value[1])} [#{value[1]}]"
      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

Returns:

  • (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