diff options
| author | Alban Peignier | 2017-12-17 15:31:47 +0100 | 
|---|---|---|
| committer | Alban Peignier | 2018-01-05 10:23:29 +0100 | 
| commit | f90488de1f657abf24026231485c87d3e42ee11d (patch) | |
| tree | 5ee9ebe90dbd709dc2fb89aa38559b123453ccf0 | |
| parent | e7e27a0d80938220664f7cae60d2e6452b74ec69 (diff) | |
| download | chouette-core-f90488de1f657abf24026231485c87d3e42ee11d.tar.bz2 | |
Move (clean) period split logic into Range#remove. Refs #5299
| -rw-r--r-- | app/models/merge.rb | 17 | ||||
| -rw-r--r-- | lib/range_ext.rb | 12 | ||||
| -rw-r--r-- | spec/lib/range_ext_spec.rb | 23 | 
3 files changed, 39 insertions, 13 deletions
| diff --git a/app/models/merge.rb b/app/models/merge.rb index 0733ae8bf..7c5a5d68c 100644 --- a/app/models/merge.rb +++ b/app/models/merge.rb @@ -118,26 +118,17 @@ class Merge < ActiveRecord::Base            line_metadatas.each do |m|              m.periodes = m.periodes.map do |existing_period| -              if period.begin <= existing_period.begin and -                existing_period.end <= period.end -                # between -                nil -              elsif existing_period.include? period.begin -                # before -                Range.new existing_period.begin, period.begin - 1 -              elsif existing_period.include? period.end -                # after -                Range.new period.end + 1, existing_period.end -              end -            end.compact +              existing_period.remove period +            end.flatten            end            attributes = {              line_ids: [line_id],              periodes: [period],              referential_source_id: referential.id, -            created_at: metadata.created_at +            created_at: metadata.created_at # TODO check required dates            } +            # line_metadatas should not contain conflicted metadatas            merge_metadatas << ReferentialMetadata.new(attributes)          end diff --git a/lib/range_ext.rb b/lib/range_ext.rb index f1df5e70d..a6a3bfc5d 100644 --- a/lib/range_ext.rb +++ b/lib/range_ext.rb @@ -5,4 +5,16 @@ class Range      [self.min, other.min].max..[self.max, other.max].min    end    alias_method :&, :intersection + +  def remove(other) +    return self if (self.max < other.min or other.max < self.min) + +    [].tap do |remaining| +      remaining << (self.min..other.min-1) if self.min < other.min +      remaining << (other.max+1..self.max) if other.max < self.max +      remaining.compact! +    end +  end +  alias_method :-, :remove +  end diff --git a/spec/lib/range_ext_spec.rb b/spec/lib/range_ext_spec.rb index 9c44608b9..cae637e47 100644 --- a/spec/lib/range_ext_spec.rb +++ b/spec/lib/range_ext_spec.rb @@ -15,4 +15,27 @@ RSpec.describe Range do        expect( (2..4) & (1..3) ).to eq 2..3      end    end + +  context "remove" do +    it "is unchanged when the given range has no intersection" do +      expect( (1..2).remove(3..4) ).to eq 1..2 +      expect( (3..4).remove(1..2) ).to eq 3..4 +    end + +    it "is nil for two equal ranges" do +      expect( (1..2).remove(1..2) ).to be_empty +    end + +    it "is the begin of the range when given range intersect the end" do +      expect( (5..10).remove(8..15) ).to eq [5..7] +    end + +    it "is the end of the range when given range intersect the begin" do +      expect( (5..10).remove(1..6) ).to eq [7..10] +    end + +    it "is the two remaing ranges when given range is the middle" do +      expect( (1..10).remove(4..6) ).to eq [1..3, 7..10] +    end +  end  end | 
