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 supposed to be instantiated only 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({ '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.

It is NOT thread-safe!

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2024-2025 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(map) ⇒ Fact

Ctor.

Parameters:

  • map (Hash)

    A map of key/value pairs



33
34
35
# File 'lib/factbase/fact.rb', line 33

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



45
46
47
# File 'lib/factbase/fact.rb', line 45

def all_properties
  @map.keys
end

#to_sString

Convert it to a string.

Returns:

  • (String)

    String representation of it (in JSON)



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

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