class Factis::Memory

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

Public Class Methods

all_facts() click to toggle source

Get the entire facts hash

@return [Hash] all facts that have been stored

# File lib/factis/memory.rb, line 24
def self.all_facts
  init_memory!
  @facts
end
forget(fact) click to toggle source

Removes the given fact (key) from internal storage

@param fact the key of the fact to remove

@return [Object] the content of the removed fact

@raise [RuntimeError] if the key is not in the fact hash

# 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) click to toggle source

Determines if the given fact key is already known

@param fact the fact key to check

@return [Boolean] true if the key is known, false otherwise

# 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}) click to toggle source

Stores content as fact, optionally overwriting said content

@param fact the key for the fact hash @param content [Object] the content for the fact @param options [Hash] The only key checked is :overwrite

a boolean defaulting to false.

@return [Object] the fact content

@raise [RuntimeError] if the fact key is already known and :overwrite

is not active.
# 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) click to toggle source

Get fact content by key

@param fact the key of the fact to retrieve

@return [Object] the content of the recalled fact

@raise [RuntimeError] if the key is not in the fact hash

# 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!() click to toggle source

Clear the entire facts hash

# File lib/factis/memory.rb, line 92
def self.reset!
  init_memory!
  @facts.clear
end

Private Class Methods

detect_unknown_fact(fact, error_message) click to toggle source
# File lib/factis/memory.rb, line 13
def detect_unknown_fact(fact, error_message)
  unless known_fact?(fact)
    raise "#{error_message}: #{fact}"
  end
end
init_memory!() click to toggle source
# File lib/factis/memory.rb, line 9
def init_memory!
  @facts ||= Hash.new
end