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(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 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(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
# File 'lib/factbase/fact.rb', line 49

def initialize(mutex, map)
  @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



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

def all_properties
  @map.keys
end

#to_sString

Convert it to a string.

Returns:

  • (String)

    String representation of it (in JSON)



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

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