I Samuel 7:13 apparently precedes a successful occupation of some hotly sought territory by the Philistines. They were subdued in this battle described in ch. 7, after having been victorious during the affair when Israel lost the ark of the covenant over 20 years before. But according to 7:13, this would not be the end of the fighting for, "the hand of the Lord was against the Philistines all the days of Samuel." Did a Philistine army ever cross over one of the borders into Israel again after they were routed in chapter 7? Yes, but not in this place, and not victoriously. Whatever the author of I Samuel 7 meant regarding "the coasts of Israel", it permits the possibility that the "hand of the Lord" might defeat the Philistines at Michmash. Michmash was not traditionally a territory that had ever been occupied by the Philistines, but required a flanking operation to the east which was not the "coast" from whence a frontal assault by the Philistines might be expected to come. If the Philistines had victoriously broken through in the same place described in ch. 7, it might be more problematic. As it is described, they had a ceremony at this location in chapter 7 where Samuel sets up a stone, and names it Ebenezer, between Mizpah and Jeshanah, which had been the site of conflict. Then we immediately read how that the Philistines never again "entered the coast". It seems to me like it might be justified to allow that the author of ch. 7 was not precluding the possibility of what occurred later. Rather, the Philistines never approached by way of the rock of Ebenezer again.
Regarding the grandmother of David, it had been about 500 years since the decree that a Moabite could not enter the congregation. Think of how the ethnic make-up of nations changes over a course of the centuries, especially when there is no religious separatism to keep people from intermarrying. Ruth came from Moab, but who would think that she would still be rejoicing over how the descendants of Lot had refused Israel passage when they emerged from the wilderness of Sinai even if she were full blooded Moabite? She gives little indication that she supported the hiring of Balaam to curse the Israelites. It seems like one is justified in interpreting the law here with the context. The law said that no Moabite could enter even after ten generations, but how was one a Moabite? Not merely by blood, which was probably watered down before the tenth generation anyway, but also by attitude.
No good law can be interpreted as given without any exception if truth, goodness, and common sense suggests an alternative. In my opinion, the Israelites would have been legitimately permitted to allow first generation Moabites to have "entered the congregation" if they exhibited the faith and loyalty of Ruth. When after her Israelite husband died, her mother-in-law encouraged Ruth to go away back to her own people:
See, your sister-in-law has gone back to her people and her gods; return after your sister-in-law. But Ruth said, Entreat me not to leave you or return from following you; for where you go, I will go, and where you lodge, I will lodge; your people shall be my people, and your God my God...
---Ruth 1:15, 16
That law in Deuteronomy was never going to apply, nor was it intended to apply, to anyone who acted like Ruth, no matter who they had for a Mommy or Daddy. Jesus ate some corn that was against the Sabbath laws and so did King David demonstrating the legitimacy of allowing a careful examination not merely of the letter, but also the spirit of the law. Even if we interpreted the law with the most rigorous unwillingness to interpret the spirit of the law, but the letter, we are still probably okay. Who would think that according to the letter of the law, when an Israelite man and a Moabite woman have a child, it would be considered Moabite? Do we think that in that society, the mother's or the father's seed would define his ethnicity? Even if you say they are half and half, would the letter of the law still prohibit someone with David's obvious virtue and religious fervor from being considered a church member? What about the fact that a half and halfie, was one of David's parents and the other was full Israelite. That means David is now one-fourth Moabite. I just don't see how the Israelites could have applied that law to the letter practically speaking in this instance. There is no doubt in my mind that God never intended for that law to apply to anyone except those who continued to approve of the actions taken by the children of Lot at the time of crossing of the frontiers of the Promised Land. I would further suggest that the same law would be legitimately cited to exclude anyone, even Israelites from the congregation who for any reason came to be traitors to their people.
Surely, canon law by the times of David had arrived at the only just conclusion in permitting him and others like him to enter the congregation of the Lord. It is likely that no one even considered citing Deut. 23:3 against someone because one-fourth of his blood came from a grandmother who was courageously loyal and faithful to Israel, but was born in Moab five hundred years after the events that prompted the passage of the law.