Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/edtf/parser.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion lib/edtf/set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ class Set
include Comparable

def_delegators :@dates, :size, :length, :empty?
def_delegators :to_a, :include?

attr_accessor :choice, :later, :earlier

Expand All @@ -29,6 +28,21 @@ def initialize_copy(other)
end
end

def include?(v)
return false unless v.is_a?(Date)
return false if empty?

dates_array = to_a
if earlier?
(min = dates_array.first) >= v && min.precision == v.precision
elsif later?
(max = dates_array.last) <= v && max.precision == v.precision
else
dates_array.member?(v)
end
end
alias member? include?

def <<(date)
dates << date
self
Expand Down
164 changes: 163 additions & 1 deletion spec/edtf/set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module EDTF
end

it 'does not include the date 1671-01-01' do
expect(set.include?(Date.new(1671))).to be false
expect(set.include?(Date.new(1671).day_precision!)).to be false
end

it 'does not include the year 1669' do
Expand Down Expand Up @@ -103,5 +103,167 @@ module EDTF
expect(set.to_a).to eq(expected_months)
end
end

context "open sets" do
describe 'the set {..1984}' do
let(:set) { Date.edtf('{..1984}') }

it "is not a choice" do
expect(set).not_to be_choice
end

it 'includes the date 1984' do
expect(set.include?(Date.new(1984).year_precision!)).to be true
end

it 'includes the date 1984 BCE' do
expect(set.include?(Date.new(-1984).year_precision!)).to be true
end

it 'does not include the date 1995' do
expect(set.include?(Date.new(1995).year_precision!)).to be false
end
end

describe 'the set {..1984-05-11}' do
let(:set) { Date.edtf('{..1984-05-11}') }

it "is not a choice" do
expect(set).not_to be_choice
end

it 'includes the date 1984-05-11' do
expect(set.include?(Date.new(1984, 5, 11))).to be true
end

it 'includes the date 11 May 1984 BCE' do
expect(set.include?(Date.new(-1984, 5, 11))).to be true
end

it 'does not include the date 1995-01-01' do
expect(set.include?(Date.new(1995))).to be false
end
end

describe 'the set {1984..}' do
let(:set) { Date.edtf('{1984..}') }

it "is not a choice" do
expect(set).not_to be_choice
end

it 'includes the date 1984' do
expect(set.include?(Date.new(1984).year_precision!)).to be true
end

it 'does not include the date 1984 BCE' do
expect(set.include?(Date.new(-1984).year_precision!)).to be false
end

it 'includes the date 1995' do
expect(set.include?(Date.new(1995).year_precision!)).to be true
end
end

describe 'the set {1984-05-11..}' do
let(:set) { Date.edtf('{1984-05-11..}') }

it "is not a choice" do
expect(set).not_to be_choice
end

it 'includes the date 1984-05-11' do
expect(set.include?(Date.new(1984, 5, 11))).to be true
end

it 'does not include the date 11 May 1984 BCE' do
expect(set.include?(Date.new(-1984, 5, 11))).to be false
end

it 'includes the date 1995-01-01' do
expect(set.include?(Date.new(1995))).to be true
end
end

describe 'the set [..1984]' do
let(:set) { Date.edtf('[..1984]') }

it "is a choice" do
expect(set).to be_choice
end

it 'includes the date 1984' do
expect(set.include?(Date.new(1984).year_precision!)).to be true
end

it 'includes the date 1984 BCE' do
expect(set.include?(Date.new(-1984).year_precision!)).to be true
end

it 'does not include the date 1995' do
expect(set.include?(Date.new(1995).year_precision!)).to be false
end
end

describe 'the set [..1984-05-11]' do
let(:set) { Date.edtf('[..1984-05-11]') }

it "is a choice" do
expect(set).to be_choice
end

it 'includes the date 1984-05-11' do
expect(set.include?(Date.new(1984, 5, 11))).to be true
end

it 'includes the date 11 May 1984 BCE' do
expect(set.include?(Date.new(-1984, 5, 11))).to be true
end

it 'does not include the date 1995-01-01' do
expect(set.include?(Date.new(1995))).to be false
end
end

describe 'the set [1984..]' do
let(:set) { Date.edtf('[1984..]') }

it "is a choice" do
expect(set).to be_choice
end

it 'includes the date 1984' do
expect(set.include?(Date.new(1984).year_precision!)).to be true
end

it 'does not include the date 1984 BCE' do
expect(set.include?(Date.new(-1984).year_precision!)).to be false
end

it 'includes the date 1995' do
expect(set.include?(Date.new(1995).year_precision!)).to be true
end
end

describe 'the set [1984-05-11..]' do
let(:set) { Date.edtf('[1984-05-11..]') }

it "is a choice" do
expect(set).to be_choice
end

it 'includes the date 1984-05-11' do
expect(set.include?(Date.new(1984, 5, 11))).to be true
end

it 'does not include the date 11 May 1984 BCE' do
expect(set.include?(Date.new(-1984, 5, 11))).to be false
end

it 'includes the date 1995-01-01' do
expect(set.include?(Date.new(1995))).to be true
end
end
end
end
end