diff --git a/app/models/matchup.rb b/app/models/matchup.rb index 3db1948..8180646 100644 --- a/app/models/matchup.rb +++ b/app/models/matchup.rb @@ -5,5 +5,20 @@ class Matchup @weight = Weight.find(self.weight_id) return @weight.max 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 \ No newline at end of file diff --git a/app/models/tournament.rb b/app/models/tournament.rb index 9ac94f0..ad10ac8 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -14,7 +14,9 @@ class Tournament < ActiveRecord::Base if self.matchups_array? return matchupHashesToObjects(self.matchups_array) else - return generateMatchups + @matches = generateMatchups + saveMatchups(@matches) + return @matches end end @@ -32,8 +34,6 @@ class Tournament < ActiveRecord::Base @upcomingMatches = weight.generateMatchups(@matches) end @upcomingMatches = assignBouts(@upcomingMatches) - self.matchups_array = matchupObjectsToHash(@upcomingMatches) - self.save return @upcomingMatches end @@ -43,20 +43,16 @@ class Tournament < ActiveRecord::Base return @matches end + def saveMatchups(matches) + self.matchups_array = matchupObjectsToHash(matches) + self.save + end + def matchupObjectsToHash(matches) array_of_hashes = [] matches.each do |m| - @matchHash = Hash.new - @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 + @matchHash = m.to_hash array_of_hashes << @matchHash end return array_of_hashes @@ -66,15 +62,7 @@ class Tournament < ActiveRecord::Base array_of_objects = [] matches.each do |m| @match = Matchup.new - @match.w1 = m["w1"] - @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"] + @match.convert_to_obj(m) array_of_objects << @match end return array_of_objects