Class: MadderLib::Sequencer

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/madderlib/sequencer.rb

Overview

Sequencer

A utility class used by a Builder for the actual building process.

A Sequencer provides no useful functionality to users of MadderLib. All of its value is inherent in the Builder and Phrase objects that it leverages.

Defined Under Namespace

Classes: RESULT_NODE

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(builder, steps, ids, attrs = {}) ⇒ Sequencer

Returns a new instance of Sequencer.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/madderlib/sequencer.rb', line 13

def initialize(builder, steps, ids, attrs={})
	@builder, @steps, @ids = builder, steps, ids

	#	arbitrary attributes for convenience
	(attrs || {}).each do |k, v|
		self.instance_variable_set "@#{k}".to_sym, v
	end

	#	prime
	@anytime ||= []
	@before ||= {}
	@after ||= {}
end

Instance Attribute Details

#builderObject (readonly)

Returns the value of attribute builder.



11
12
13
# File 'lib/madderlib/sequencer.rb', line 11

def builder
  @builder
end

#idsObject (readonly)

Returns the value of attribute ids.



11
12
13
# File 'lib/madderlib/sequencer.rb', line 11

def ids
  @ids
end

#stepsObject (readonly)

Returns the value of attribute steps.



11
12
13
# File 'lib/madderlib/sequencer.rb', line 11

def steps
  @steps
end

Instance Method Details

#after(ref) ⇒ Object



31
32
33
# File 'lib/madderlib/sequencer.rb', line 31

def after(ref)
	@after[ref]
end

#before(ref) ⇒ Object



27
28
29
# File 'lib/madderlib/sequencer.rb', line 27

def before(ref)
	@before[ref]
end

#each_phrase(data = nil, &block) ⇒ Object

iterates over each phrase in the sequence



66
67
68
# File 'lib/madderlib/sequencer.rb', line 66

def each_phrase(data=nil, &block)
	self.phrases(data).each {|phrase| yield phrase }
end

#each_word(data = nil) ⇒ Object Also known as: each

iterates over each word in the sequence



49
50
51
# File 'lib/madderlib/sequencer.rb', line 49

def each_word(data=nil)
	self.words(data).each {|word| yield word }
end

#phrases(data = nil, &block) ⇒ Object

returns each phrase in the sequence



55
56
57
58
59
60
61
62
63
# File 'lib/madderlib/sequencer.rb', line 55

def phrases(data=nil, &block)
	#	sequence the phrases
	#		*sigh* verb and a noun
	nodes = sequence(data, &block)

	#	only care about the phrases
	#		these will all have been 'spoken'
	nodes.collect {|node| node.phrase }
end

#words(data = nil, &block) ⇒ Object

returns each word in the sequence



38
39
40
41
42
43
44
45
46
# File 'lib/madderlib/sequencer.rb', line 38

def words(data=nil, &block)
	#	sequence the phrases
	#		*sigh* verb and a noun
	nodes = sequence(data, &block)

	#	composite the words
	#		each node contains an array of words
	nodes.collect {|node| node.words }.flatten
end