Class: Moory::Filter

Inherits:
Logistic::Unit show all
Defined in:
lib/moory/filter.rb

Constant Summary collapse

IGNORE =
[' ', "\t", "\n"]
DEFAULT_CONSUMER =
$stdout.method(:puts)
IGNORE_UNKNOWN =
proc { |c| pp "Warning! Ignoring unknown character: #{c}" }

Instance Attribute Summary collapse

Attributes included from Efferent

#repertoire

Attributes included from Afferent

#state, #transitions

Instance Method Summary collapse

Methods inherited from Logistic::Unit

#prime

Methods included from Afferent

#awaits, #understand?

Constructor Details

#initialize(rules:, consumer: DEFAULT_CONSUMER, quarantine: IGNORE_UNKNOWN) ⇒ Filter

Returns a new instance of Filter.



11
12
13
14
15
16
# File 'lib/moory/filter.rb', line 11

def initialize(rules:, consumer:DEFAULT_CONSUMER, quarantine:IGNORE_UNKNOWN)
  @buffer = ""
  @consumer   = consumer
  @quarantine = quarantine
  super(rules: rules)
end

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



5
6
7
# File 'lib/moory/filter.rb', line 5

def buffer
  @buffer
end

#consumer=(value) ⇒ Object

Sets the attribute consumer

Parameters:

  • value

    the value to set the attribute consumer to.



3
4
5
# File 'lib/moory/filter.rb', line 3

def consumer=(value)
  @consumer = value
end

#quarantine=(value) ⇒ Object

Sets the attribute quarantine

Parameters:

  • value

    the value to set the attribute quarantine to.



4
5
6
# File 'lib/moory/filter.rb', line 4

def quarantine=(value)
  @quarantine = value
end

Instance Method Details

#configure(rules) ⇒ Object



18
19
20
21
# File 'lib/moory/filter.rb', line 18

def configure(rules)
  super
  repertoire.learn(name: 'produce', item: method(:produce))
end

#issue(stimulus) ⇒ Object



23
24
25
26
27
28
# File 'lib/moory/filter.rb', line 23

def issue(stimulus)
  return if IGNORE.include?(stimulus)
  
  @buffer << stimulus
  super
end

#produce(output) ⇒ Object



30
31
32
33
# File 'lib/moory/filter.rb', line 30

def produce(output)
  consumer.call(@buffer)
  @buffer = ""
end