class Moory::Repertoire

Constants

SKIP

Attributes

always[R]
fallback[R]
knowledge[R]

Public Class Methods

from_hash(hash={}) click to toggle source
# File lib/moory/repertoire.rb, line 9
def Repertoire.from_hash(hash={})
  new.tap { |r| r.cram(hash) }
end
new() click to toggle source
# File lib/moory/repertoire.rb, line 13
def initialize
  @fallback  = SKIP
  @always    = SKIP
  @knowledge = {}
end

Public Instance Methods

always=(obj) click to toggle source
# File lib/moory/repertoire.rb, line 39
def always=(obj)
  @always = censor(obj)
end
cram(hash={}) click to toggle source
# File lib/moory/repertoire.rb, line 19
def cram(hash={})
  hash.each { |k,v| learn(name: k, item: v) }
end
fallback=(obj) click to toggle source
# File lib/moory/repertoire.rb, line 35
def fallback=(obj)
  @fallback = censor(obj)
end
learn(item:,name:) click to toggle source
# File lib/moory/repertoire.rb, line 23
def learn(item:,name:)
  @knowledge.store(name, item) if (
    appropriate?(item) && name
  )
end
recall(name) click to toggle source
# File lib/moory/repertoire.rb, line 29
def recall(name)
  name ? 
    knowledge.fetch(name, fallback) : 
    fallback
end

Private Instance Methods

appropriate?(obj) click to toggle source
# File lib/moory/repertoire.rb, line 45
def appropriate?(obj)
  obj.respond_to?(:call)
end
censor(obj) click to toggle source
# File lib/moory/repertoire.rb, line 49
def censor(obj)
  appropriate?(obj) ? obj : SKIP
end