mirror of
https://github.com/jcwimer/wrestlingApp
synced 2026-04-07 14:57:48 +00:00
Fixed matchup object to hash and hash to object
This commit is contained in:
@@ -6,4 +6,19 @@ class Matchup
|
|||||||
return @weight.max
|
return @weight.max
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def to_hash
|
||||||
|
hash = {}
|
||||||
|
instance_variables.each {|var| hash[var.to_s.delete("@")] = instance_variable_get(var) }
|
||||||
|
hash
|
||||||
|
end
|
||||||
|
|
||||||
|
def convert_to_obj(h)
|
||||||
|
h.each do |k,v|
|
||||||
|
self.class.send(:attr_accessor, k)
|
||||||
|
instance_variable_set("@#{k}", v)
|
||||||
|
convert_to_obj(v) if v.is_a? Hash
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
end
|
end
|
||||||
@@ -14,7 +14,9 @@ class Tournament < ActiveRecord::Base
|
|||||||
if self.matchups_array?
|
if self.matchups_array?
|
||||||
return matchupHashesToObjects(self.matchups_array)
|
return matchupHashesToObjects(self.matchups_array)
|
||||||
else
|
else
|
||||||
return generateMatchups
|
@matches = generateMatchups
|
||||||
|
saveMatchups(@matches)
|
||||||
|
return @matches
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -32,8 +34,6 @@ class Tournament < ActiveRecord::Base
|
|||||||
@upcomingMatches = weight.generateMatchups(@matches)
|
@upcomingMatches = weight.generateMatchups(@matches)
|
||||||
end
|
end
|
||||||
@upcomingMatches = assignBouts(@upcomingMatches)
|
@upcomingMatches = assignBouts(@upcomingMatches)
|
||||||
self.matchups_array = matchupObjectsToHash(@upcomingMatches)
|
|
||||||
self.save
|
|
||||||
return @upcomingMatches
|
return @upcomingMatches
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -43,20 +43,16 @@ class Tournament < ActiveRecord::Base
|
|||||||
return @matches
|
return @matches
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def saveMatchups(matches)
|
||||||
|
self.matchups_array = matchupObjectsToHash(matches)
|
||||||
|
self.save
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
def matchupObjectsToHash(matches)
|
def matchupObjectsToHash(matches)
|
||||||
array_of_hashes = []
|
array_of_hashes = []
|
||||||
matches.each do |m|
|
matches.each do |m|
|
||||||
@matchHash = Hash.new
|
@matchHash = m.to_hash
|
||||||
@matchHash["w1"] = m.w1
|
|
||||||
@matchHash["w2"] = m.w2
|
|
||||||
@matchHash["round"] = m.round
|
|
||||||
@matchHash["weight_id"] = m.weight_id
|
|
||||||
@matchHash["boutNumber"] = m.boutNumber
|
|
||||||
@matchHash["w1_name"] = m.w1_name
|
|
||||||
@matchHash["w2_name"] = m.w2_name
|
|
||||||
@matchHash["bracket_position"] = m.bracket_position
|
|
||||||
@matchHash["bracket_position_number"] = m.bracket_position_number
|
|
||||||
array_of_hashes << @matchHash
|
array_of_hashes << @matchHash
|
||||||
end
|
end
|
||||||
return array_of_hashes
|
return array_of_hashes
|
||||||
@@ -66,15 +62,7 @@ class Tournament < ActiveRecord::Base
|
|||||||
array_of_objects = []
|
array_of_objects = []
|
||||||
matches.each do |m|
|
matches.each do |m|
|
||||||
@match = Matchup.new
|
@match = Matchup.new
|
||||||
@match.w1 = m["w1"]
|
@match.convert_to_obj(m)
|
||||||
@match.w2 = m["w2"]
|
|
||||||
@match.round = m["round"]
|
|
||||||
@match.weight_id = m["weight_id"]
|
|
||||||
@match.boutNumber = m["boutNumber"]
|
|
||||||
@match.w1_name = m["w1_name"]
|
|
||||||
@match.w2_name = m["w2_name"]
|
|
||||||
@match.bracket_position = m["bracket_position"]
|
|
||||||
@match.bracket_position_number = m["bracket_position_number"]
|
|
||||||
array_of_objects << @match
|
array_of_objects << @match
|
||||||
end
|
end
|
||||||
return array_of_objects
|
return array_of_objects
|
||||||
|
|||||||
Reference in New Issue
Block a user