ruby on rails - Range doesn't conflict with other ranges -
So I have to do a Rail Validation which manages the following situation:
I have a set Is the category, and I need to know if there is a conflict with any of these categories in the category asked. For example, I have the following categories:
- (0 <3000 (.3000)
- (3000..4000)
- (4000 ..5000)
-
(6000 .. 7000)
-
1.10 will be in conflict because this category is already in 0.000 Has been included
-
In the 1.3100 will be the conflict, because this range is included, partially between 0.35000 and 3000.4000
2800..4500 will be in conflict, because this limit is in part, between 0, 3000, 3000..4000 and 4000..5000
-
-
5000 .. 6000 will not be in conflict
What I have achieved is the first and easiest issue from this line:
def is_not_conflictive_range? Categories = serviceprice where (property: self property) .pluck ( : From_value ,: to_value) .map {| Category | Category.first.change.second} Contradictory_range = Categories | Search | Category | range.include? (Self.from_value..self.to_value) end errors.add (: service_price , Ranges from # {self.from_value} to # {self.to_value} ranges from # {conflictive_range.first} to the end of the # {conflictive_range} .last} ") if conflictive_range end But I can not really know how to handle other matters in a simple way.
This is one of those questions that is straightforward when viewed correctly, but otherwise messy and Complex After some false start I addressed this question, "How can one overlap be avoided?"
I have assumed that the boundaries in the array are ordered in this sequence that the end of each range is next The beginning of each and the beginning of each category is not less than the end of the past. This is in your example. If that is not in the situation, then the first step will be to modify the array so that it can happen, which is not difficult
Code def no_overlap? (Arr, range) range.last & lt; = arr.first first || class First & gt; = arr.last.last || arr.each_cons (2). Any? {| R1, r2 | Category> First & gt; = r1.last & amp; grade Last
Example
arr = [1000.3000, 3000..4000, 4000 .. 4000, 4000..5000 , 6000 ... 7000] no_overlap? (Arr, 1..1010) # = & gt; false no_overlap? (Arr, 2800..4500) # = & gt; false no_overlap? (Arr, 2500 .. 5500) # = & Gt; false no_overlap? (Arr, 5000..6000) # = & gt; true no_overlap? (Arr, 0..500) # = & gt; True no_overlap? (Arr, 8000..9000) # = & gt; True
Comments
Post a Comment