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



41
42
43
44
45
46
47
48
49
# File 'lib/rbprolog.rb', line 41

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(sym.to_s.chomp('?').to_sym, *args)
  else
    super
  end
end

Instance Method Details

#const_missing(sym) ⇒ Object



37
38
39
# File 'lib/rbprolog.rb', line 37

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

#keywords(*syms) ⇒ Object



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

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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rbprolog.rb', line 51

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

  unless method_defined?(sym)
    define_method("#{sym}!") do |*args|
      deduction = Deduction.new(sym, *args)

      deduction.extend(Enumerable)

      rules = self.rules
      deduction.define_singleton_method(:each) do |&block|
        each_deduce(Context.new, rules, []) do |hash|
          block.call hash
        end
      end

      deduction
    end

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