Module: Rbprolog::ClassMethods

Defined in:
lib/rbprolog.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/rbprolog.rb', line 36

def method_missing(sym, *args)
  if self.syms.include? sym
    Hash === args.last ? rule(sym, *args) : rule(sym, *args, :if => [])
  elsif self.syms.include? sym.to_s.chomp('?').to_sym
    Deduction.new(self, sym.to_s.chomp('?').to_sym, *args)
  else
    super
  end
end

Instance Method Details

#const_missing(sym) ⇒ Object



32
33
34
# File 'lib/rbprolog.rb', line 32

def const_missing(sym)
  Var.new(sym)
end

#keywords(*syms) ⇒ Object



25
26
27
28
29
30
# File 'lib/rbprolog.rb', line 25

def keywords(*syms)
  raise if syms.any? {|sym| sym.to_s.end_with? '?'}

  self.syms ||= []
  self.syms.concat(syms)
end

#rule(sym, *args, options) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rbprolog.rb', line 46

def rule(sym, *args, options)
  self.rules ||= []
  self.rules << Rule.new(sym, *args, options[:if])

  unless method_defined?(sym)
    #FIXME only allow fact for now
    define_method(sym) do |*args|
      @rules ||= []
      @rules << Rule.new(sym, *args, [])
    end

    define_method("#{sym}!") do |*args|
      Deduction.new(self, sym, *args)
    end

    define_method("#{sym}?") do |*args|
      self.send("#{sym}!", *args).any? {|hash| true}
    end
  end
end