Class: Bones::Variablelist

Inherits:
Array
  • Object
show all
Defined in:
lib/bones/variablelist.rb

Overview

This class is based on the standard Array class. It is meant to contain a list of elements of the Variable class. In that sense, using the Array class will suffice. However, this class extends the list with a small number of addi- tional methods. These methods involve selecting a subset of the list or sorting the list.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#representativeObject

Returns the value of attribute representative.



10
11
12
# File 'lib/bones/variablelist.rb', line 10

def representative
  @representative
end

Instance Method Details

#inputsObject

This method is a short-hand version to select a list of input variables. It calls the select method internally.



32
33
34
# File 'lib/bones/variablelist.rb', line 32

def inputs
	select(INPUT)
end

#inputs_onlyObject

This method is a short-hand version to select a list of input only variables. It calls the select method internally.



45
46
47
# File 'lib/bones/variablelist.rb', line 45

def inputs_only
	self-select(OUTPUT)
end

#outputsObject

This method is a short-hand version to select a list of output variables. It calls the select method internally.



38
39
40
# File 'lib/bones/variablelist.rb', line 38

def outputs
	select(OUTPUT)
end

#outputs_onlyObject

This method is a short-hand version to select a list of input only variables. It calls the select method internally.



52
53
54
# File 'lib/bones/variablelist.rb', line 52

def outputs_only
	self-select(INPUT)
end

#select(direction) ⇒ Object

This method returns a subset of the list, based on the argument direction given. It either returns a list of input variables or a list of output variables.



15
16
17
18
19
20
21
# File 'lib/bones/variablelist.rb', line 15

def select(direction)
	array = Variablelist.new()
	self.each do |element|
		array.push(element) if ((direction == INPUT) && (element.input?)) || ((direction == OUTPUT) && (element.output?))
	end
	return array
end

#set_representative(ids) ⇒ Object

Method to set a representative variable for this variable- list. It is set based on the variable’s species-name, e.g. ‘in0’ or ‘out2’.



26
27
28
# File 'lib/bones/variablelist.rb', line 26

def set_representative(ids)
	@representative = select(ids.to_s.scan(/\D+/).join)[ids.to_s.scan(/\d+/).join.to_i]
end

#sort_by(alphabet) ⇒ Object

This method sorts the list of variables based on its species’ pattern (e.g. element or chunk). An alphabet is based as an argument to this method to specify the prefered order. This alphabet must be an array of strings.



60
61
62
63
64
65
66
67
68
# File 'lib/bones/variablelist.rb', line 60

def sort_by(alphabet)
	clone = self.clone
	self.clear
	alphabet.each do |letter|
		clone.each do |array|
			self.push(array) if array.species.pattern == letter
		end
	end
end