Class: KBS::Fact

Inherits:
Object
  • Object
show all
Defined in:
lib/kbs/fact.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, attributes = {}) ⇒ Fact

Returns a new instance of Fact.



7
8
9
10
11
# File 'lib/kbs/fact.rb', line 7

def initialize(type, attributes = {})
  @id = object_id
  @type = type
  @attributes = attributes
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



5
6
7
# File 'lib/kbs/fact.rb', line 5

def attributes
  @attributes
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/kbs/fact.rb', line 5

def id
  @id
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/kbs/fact.rb', line 5

def type
  @type
end

Instance Method Details

#[](key) ⇒ Object



13
14
15
# File 'lib/kbs/fact.rb', line 13

def [](key)
  @attributes[key]
end

#[]=(key, value) ⇒ Object



17
18
19
# File 'lib/kbs/fact.rb', line 17

def []=(key, value)
  @attributes[key] = value
end

#matches?(pattern) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/kbs/fact.rb', line 21

def matches?(pattern)
  return false if pattern[:type] && pattern[:type] != @type

  pattern.each do |key, value|
    next if key == :type

    if value.is_a?(Proc)
      return false unless @attributes[key] && value.call(@attributes[key])
    elsif value.is_a?(Symbol) && value.to_s.end_with?('?')
      next
    else
      return false unless @attributes[key] == value
    end
  end

  true
end

#to_sObject



39
40
41
# File 'lib/kbs/fact.rb', line 39

def to_s
  "#{@type}(#{@attributes.map { |k, v| "#{k}: #{v}" }.join(', ')})"
end