lib/rlottery/lottery.rb in jlindley-rlottery-0.1.2 vs lib/rlottery/lottery.rb in jlindley-rlottery-0.1.3

- old
+ new

@@ -32,10 +32,33 @@ def rule_set @rule_set end + def persist(filename) + File.open(filename, 'w+') do |file| + YAML.dump( self, file ) + end + end + + def self.load(filename) + YAML.load_file filename + end + + def ==(other) + my_slots = self.slots.values + other_slots = other.slots.values + slots_are_equal = slots_equality?(my_slots, other_slots) + return false unless slots_are_equal + + my_participants = self.participants.values + other_participants = self.slots.values + participants_are_equal = participants_equality?(my_slots, other_slots) + return false unless participants_are_equal + return true + end + protected def participants=(participants_list) @participants_list ||= {} list_init(Participant, participants_list, @participants_list) @@ -56,9 +79,44 @@ else raise ArgumentError, "Unknown kind of #{item_class}: #{i}." end store_in[item.identifier] = item end + end + + private + + def slots_equality?(mine, theirs) + generic_entity_equality?(mine, theirs) + end + + def participants_equality?(mine, theirs) + generic_entity_equality?(mine, theirs) + end + + def generic_entity_equality?(mine, theirs) + return false unless sizes_equal?(mine, theirs) + return false unless keys_equal?(mine, theirs) + mine.each do |i| + my_item = i + their_item = theirs.detect{ |t| t.identifier == i.identifier } + return false unless their_item + my_assignment_ids = i.assignments.map{ |a| a.identifier } + their_assignment_ids = their_item.assignments.map{ |a| a.identifier } + return false unless (my_assignment_ids - their_assignment_ids == []) + end + true + end + + def sizes_equal?(mine, theirs) + mine.size == theirs.size ? true : false + end + + def keys_equal?(mine, theirs) + my_entity_keys = mine.map{ |e| e.identifier } + their_entity_keys = theirs.map{ |e| e.identifier } + return false unless (my_entity_keys - their_entity_keys == []) + return true end end end \ No newline at end of file