Class: KBS::Fact
- Inherits:
-
Object
- Object
- KBS::Fact
- Defined in:
- lib/kbs/fact.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
-
#initialize(type, attributes = {}) ⇒ Fact
constructor
A new instance of Fact.
- #matches?(pattern) ⇒ Boolean
- #to_s ⇒ Object
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
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
5 6 7 |
# File 'lib/kbs/fact.rb', line 5 def attributes @attributes end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
5 6 7 |
# File 'lib/kbs/fact.rb', line 5 def id @id end |
#type ⇒ Object (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
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_s ⇒ Object
39 40 41 |
# File 'lib/kbs/fact.rb', line 39 def to_s "#{@type}(#{@attributes.map { |k, v| "#{k}: #{v}" }.join(', ')})" end |