Class: FReCon::Position
Instance Attribute Summary collapse
-
#alliance ⇒ Object
readonly
Returns the value of attribute alliance.
-
#number ⇒ Object
readonly
Returns the value of attribute number.
Class Method Summary collapse
-
.demongoize(object) ⇒ Object
MongoDB compatibility methods.
-
.evolve(object) ⇒ Object
Used for queries.
-
.mongoize(object) ⇒ Object
Allows passing a String or Hash instead of a Position.
Instance Method Summary collapse
-
#initialize(*args) ⇒ Position
constructor
A new instance of Position.
- #is_blue? ⇒ Boolean (also: #was_blue?)
- #is_red? ⇒ Boolean (also: #was_red?)
- #mongoize ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(*args) ⇒ Position
Returns a new instance of Position.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/frecon/position.rb', line 62 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
#alliance ⇒ Object (readonly)
Returns the value of attribute alliance.
14 15 16 |
# File 'lib/frecon/position.rb', line 14 def alliance @alliance end |
#number ⇒ Object (readonly)
Returns the value of attribute number.
14 15 16 |
# File 'lib/frecon/position.rb', line 14 def number @number end |
Class Method Details
.demongoize(object) ⇒ Object
MongoDB compatibility methods.
17 18 19 20 21 22 23 |
# File 'lib/frecon/position.rb', line 17 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
Used for queries.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/frecon/position.rb', line 43 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
Allows passing a String or Hash instead of a Position. i.e. record.position = “r3”
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/frecon/position.rb', line 27 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?
119 120 121 |
# File 'lib/frecon/position.rb', line 119 def is_blue? @alliance == :blue end |
#is_red? ⇒ Boolean Also known as: was_red?
123 124 125 |
# File 'lib/frecon/position.rb', line 123 def is_red? @alliance == :red end |
#mongoize ⇒ Object
58 59 60 |
# File 'lib/frecon/position.rb', line 58 def mongoize to_s end |
#to_s ⇒ Object
115 116 117 |
# File 'lib/frecon/position.rb', line 115 def to_s "#{@alliance[0]}#{@number}" end |