Class: Factbase::Fact

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

Overview

A single fact in a factbase.

This is an internal class, it is not supposed to be instantiated directly, by the Factbase class. However, it is possible to use it for testing directly, for example to make a fact with a single key/value pair inside:

require 'factbase/fact'
f = Factbase::Fact.new(Factbase.new, Mutex.new, { 'foo' => [42, 256, 'Hello, world!'] })
assert_equal(42, f.foo)

A fact is basically a key/value hash map, where values are non-empty sets of values.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2024-2025 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(fb, mutex, map) ⇒ Fact

Ctor.

Parameters:

  • mutex (Mutex)

    A mutex to use for maps synchronization

  • map (Hash)

    A map of key/value pairs



49
50
51
52
53
# File 'lib/factbase/fact.rb', line 49

def initialize(fb, mutex, map)
  @fb = fb
  @mutex = mutex
  @map = map
end

Instance Method Details

#all_propertiesArray<String>

Get a list of all props.

Returns:

  • (Array<String>)

    List of all props in the fact



63
64
65
# File 'lib/factbase/fact.rb', line 63

def all_properties
  @map.keys
end

#to_sString

Convert it to a string.

Returns:

  • (String)

    String representation of it (in JSON)



57
58
59
# File 'lib/factbase/fact.rb', line 57

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