Class: FReCon::Position

Inherits:
Object show all
Defined in:
lib/frecon/position.rb

Overview

Public: A wrapper to handle converting team positions and storing them.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Position

Returns a new instance of Position.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/frecon/position.rb', line 104

def initialize(*args)
	if args.length == 1
		# Match `string' against the regular expression, described below.
		#
		# This regular expression matches all values for `string' where
		# the first letter is either 'r' or 'b' (case-insensitive due to /i
		# at the end of the regular expression) and the last one-or-more
		# characters in the string are digits 0-9. Anything between those two
		# that is either a letter or an underscore is not retained, but
		# if other characters exist (e.g. spaces as of right now) `string'
		# will not match.
		#
		# You can use any words you like if you have more than just
		# 'r<n>' or 'b<n>', for example 'red_2' matches just the same
		# as 'r2', or, just for fun, just the same as 'royal______2'.
		#
		# This behavior may change in the future.
		match_data = args[0].match(/^([rb])[a-z\_]*([0-9]+)/i)

		# Note: if matched at all, match_data[0] is the entire
		# string that was matched, hence the indices that start
		# at one.

		raise ArgumentError, 'string is improperly formatted' unless match_data

		@alliance = case match_data[1].downcase
		            when 'b'
			            :blue
		            when 'r'
			            :red
		            else
			            raise ArgumentError, "alliance character must be in ['b', 'r']"
		            end

		position_number = match_data[2].to_i
		raise ArgumentError, 'position number must be in [1, 2, 3]' unless [1, 2, 3].include?(position_number)

		@number = position_number
	elsif args.length == 2
		raise TypeError, 'alliance must be a Symbol or String' unless args[0].is_a?(Symbol) || args[0].is_a?(String)
		raise ArgumentError, 'alliance must be in [:blue, :red]' unless [:blue, :red].include?(args[0].to_sym)

		@alliance = args[0].to_sym

		raise TypeError, 'second argument must be an Integer' unless args[1].is_an?(Integer)
		raise ArgumentError, 'second argument must be in [1, 2, 3]' unless [1, 2, 3].include?(args[1])

		@number = args[1]
	else
		raise ArgumentError, "wrong number of arguments (#{args.length} for [1, 2])"
	end
end

Instance Attribute Details

#allianceObject (readonly)

Public: The alliance part of the position

Examples

position = Position.new('r2')
position.alliance
# => :red


26
27
28
# File 'lib/frecon/position.rb', line 26

def alliance
  @alliance
end

#numberObject (readonly)

Public: The slot part of the position.

Examples

position = Position.new('r2')
position.number
# => 2


35
36
37
# File 'lib/frecon/position.rb', line 35

def number
  @number
end

Class Method Details

.demongoize(object) ⇒ Object

Public: Convert a stored position to a Position object.

object - String representation of a position (mongoized)

Returns Position parsed from object.

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
# File 'lib/frecon/position.rb', line 42

def self.demongoize(object)
	# `object' should *always* be a String (since MatchNumber#mongoize returns a
	# String which is what is stored in the database)
	raise ArgumentError, "`object' must be a String" unless object.is_a?(String)

	Position.new(object)
end

.evolve(object) ⇒ Object

Public: Convert a Position object to a storable string representation for queries.

object - A Position, String, or Hash. If Position, run #mongoize on it.

If String, create a new Position object for it, then run
#mongoize on it. If Hash, convert its keys to symbols, then
pull out the :alliance and :number keys to generate a Position.

Returns String containing the mongo-ready value for the representation.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/frecon/position.rb', line 82

def self.evolve(object)
	case object
	when Position
		object.mongoize
	when String
		Position.new(object).mongoize
	when Hash
		# Convert keys to symbols if necessary.
		object = Hash[object.map { |key, value| [key.to_sym, value] }]
		Position.new(object[:alliance], object[:number]).mongoize
	else
		object
	end
end

.mongoize(object) ⇒ Object

Public: Convert a Position object to a storable string representation.

object - A Position, String, or Hash. If Position, run #mongoize on it.

If String, create a new Position object for it, then run
#mongoize on it. If Hash, convert its keys to symbols, then
pull out the :alliance and :number keys to generate a Position.

Returns String containing the mongo-ready value for the representation.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/frecon/position.rb', line 58

def self.mongoize(object)
	case object
	when Position
		object.mongoize
	when String
		Position.new(object).mongoize
	when Hash
		# Convert keys to symbols if necessary.
		object = Hash[object.map { |key, value| [key.to_sym, value] }]
		Position.new(object[:alliance], object[:number]).mongoize
	else
		object
	end
end

Instance Method Details

#is_blue?Boolean Also known as: was_blue?

Public: Determine if Position is on blue alliance.

Returns:

  • (Boolean)


165
166
167
# File 'lib/frecon/position.rb', line 165

def is_blue?
	@alliance == :blue
end

#is_red?Boolean Also known as: was_red?

Public: Determine if Position is on red alliance.

Returns:

  • (Boolean)


170
171
172
# File 'lib/frecon/position.rb', line 170

def is_red?
	@alliance == :red
end

#mongoizeObject

Public: Convert to a storable string representation.

Returns String representing the Position’s data.



100
101
102
# File 'lib/frecon/position.rb', line 100

def mongoize
	to_s
end

#to_sObject

Public: Convert to a String.

Returns String representing the position data.



160
161
162
# File 'lib/frecon/position.rb', line 160

def to_s
	"#{@alliance[0]}#{@number}"
end