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



17
18
19
20
# File 'lib/factis/memory.rb', line 17

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



63
64
65
66
67
68
69
# File 'lib/factis/memory.rb', line 63

def self.forget(fact)
  init_memory!
  unless known_fact?(fact)
    raise %{Trying to forget an unknown fact: '#{fact}'}
  end
  @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



50
51
52
53
# File 'lib/factis/memory.rb', line 50

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.



34
35
36
37
38
39
40
41
42
# File 'lib/factis/memory.rb', line 34

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



79
80
81
82
83
84
85
# File 'lib/factis/memory.rb', line 79

def self.recall(fact)
  init_memory!
  unless known_fact?(fact)
    raise %{Trying to recall an unknown fact: '#{fact}'}
  end
  @facts[fact]
end

.reset!Object

Clear the entire facts hash



89
90
91
92
# File 'lib/factis/memory.rb', line 89

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