Class: Factis::Memory

Inherits:
Object
  • Object
show all
Defined in:
lib/factis/memory.rb

Overview

This is the workhorse of Factis. It’s what does the actual tracking of facts.

Class Method Summary collapse

Class Method Details

.all_factsHash

Get the entire facts hash

Returns:

  • (Hash)

    all facts that have been stored



24
25
26
27
# File 'lib/factis/memory.rb', line 24

def self.all_facts
  init_memory!
  @facts
end

.forget(fact) ⇒ Object

Removes the given fact (key) from internal storage

Parameters:

  • fact

    the key of the fact to remove

Returns:

  • (Object)

    the content of the removed fact

Raises:

  • (RuntimeError)

    if the key is not in the fact hash



70
71
72
73
74
# File 'lib/factis/memory.rb', line 70

def self.forget(fact)
  init_memory!
  detect_unknown_fact(fact, 'Trying to forget an unknown fact')
  @facts.delete(fact)
end

.known_fact?(fact) ⇒ Boolean

Determines if the given fact key is already known

Parameters:

  • fact

    the fact key to check

Returns:

  • (Boolean)

    true if the key is known, false otherwise



57
58
59
60
# File 'lib/factis/memory.rb', line 57

def self.known_fact?(fact)
  init_memory!
  @facts.keys.include?(fact)
end

.memorize(fact, content, options = {:overwrite => false}) ⇒ Object

Stores content as fact, optionally overwriting said content

Parameters:

  • fact

    the key for the fact hash

  • content (Object)

    the content for the fact

  • options (Hash) (defaults to: {:overwrite => false})

    The only key checked is :overwrite a boolean defaulting to false.

Returns:

  • (Object)

    the fact content

Raises:

  • (RuntimeError)

    if the fact key is already known and :overwrite is not active.



41
42
43
44
45
46
47
48
49
# File 'lib/factis/memory.rb', line 41

def self.memorize(fact, content, options = {:overwrite => false})
  init_memory!
  if known_fact?(fact)
    unless options[:overwrite] == true
      raise %{Cannot memorize a fact more than once: '#{fact}'}
    end
  end
  @facts[fact] = content
end

.recall(fact) ⇒ Object

Get fact content by key

Parameters:

  • fact

    the key of the fact to retrieve

Returns:

  • (Object)

    the content of the recalled fact

Raises:

  • (RuntimeError)

    if the key is not in the fact hash



84
85
86
87
88
# File 'lib/factis/memory.rb', line 84

def self.recall(fact)
  init_memory!
  detect_unknown_fact(fact, 'Trying to recall an unknown fact')
  @facts[fact]
end

.reset!Object

Clear the entire facts hash



92
93
94
95
# File 'lib/factis/memory.rb', line 92

def self.reset!
  init_memory!
  @facts.clear
end