iso8601 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f9dcdc5cbdd5d7ca45c4cdda75be726ba788883b
4
- data.tar.gz: 579e363f9c3969d20987122e9108e07e94acae56
3
+ metadata.gz: f6495a0f183eb9f89a063e0404b9001fd024b90d
4
+ data.tar.gz: c9bedd70b4ecc51ad6c1094a4f714b5d2a17fdcc
5
5
  SHA512:
6
- metadata.gz: c906351a59be4c6173cb08a666396659b2f37b0ba481899bb751434c5ab316f6fcd2d31f9da188c10307d22d85016851038d26351bb6f35d4f97c94aec77da56
7
- data.tar.gz: 666289b7461bdd1d29ed51eed7c810fe3a55ec4403df3163cedce303f31f1888877319fce5efc2294122ffb80dd4b5f64a05ffeeb1b9280dc31b384ab868a009
6
+ metadata.gz: 2dbe7ba46843e37298b2a4766752e74d240546ec6232e9dd1aa0e2449589351d18440ed0bf1b982c39353141d5a91b84b8f031b29697d5f09616bb6eadbfd33d
7
+ data.tar.gz: ced9dcc8204013b265f69e6c8a2407247386202026c83e19296acd99ec44b06a4811b7785e9becc1cae7a49c8a4005c64c4e4a519f7f06decb8bdfd727991b0f
@@ -1,3 +1,8 @@
1
+ ## Changes for 0.7.0
2
+
3
+ * Add decimal fractions for any component in a duration.
4
+ * Add a catch all `ISO8601::Errors::StandardError`.
5
+ * Add support for comma (`,`) as a separator for duration decimal fractions.
1
6
 
2
7
  ## Changes for 0.6.0
3
8
 
@@ -18,16 +18,16 @@ module ISO8601
18
18
  @duration = /^(\+|-)? # Sign
19
19
  P(
20
20
  (
21
- (\d+Y)? # Years
22
- (\d+M)? # Months
23
- (\d+D)? # Days
21
+ (\d+(?:[,.]\d+)?Y)? # Years
22
+ (\d+(?:[.,]\d+)?M)? # Months
23
+ (\d+(?:[.,]\d+)?D)? # Days
24
24
  (T
25
- (\d+H)? # Hours
26
- (\d+M)? # Minutes
27
- (\d+(?:\.\d+)?S)? # Seconds
25
+ (\d+(?:[.,]\d+)?H)? # Hours
26
+ (\d+(?:[.,]\d+)?M)? # Minutes
27
+ (\d+(?:[.,]\d+)?S)? # Seconds
28
28
  )? # Time
29
29
  )
30
- |(\d+W) # Weeks
30
+ |(\d+(?:[.,]\d+)?W) # Weeks
31
31
  ) # Duration
32
32
  $/x.match(pattern) or raise ISO8601::Errors::UnknownPattern.new(pattern)
33
33
 
@@ -43,6 +43,7 @@ module ISO8601
43
43
  :minutes => @duration[9].nil? ? 0 : @duration[9].chop.to_f * sign,
44
44
  :seconds => @duration[10].nil? ? 0 : @duration[10].chop.to_f * sign
45
45
  }
46
+ valid_fractions?
46
47
  end
47
48
  ##
48
49
  # Assigns a new base datetime
@@ -198,5 +199,13 @@ module ISO8601
198
199
  raise ISO8601::Errors::UnknownPattern.new(@duration)
199
200
  end
200
201
  end
202
+
203
+ def valid_fractions?
204
+ values = @atoms.values.reject(&:zero?)
205
+ fractions = values.select { |a| (a % 1) != 0 }
206
+ if fractions.size > 1 || (fractions.size == 1 && fractions.last != values.last)
207
+ raise ISO8601::Errors::InvalidFractions.new(@duration)
208
+ end
209
+ end
201
210
  end
202
211
  end
@@ -4,23 +4,33 @@ module ISO8601
4
4
  ##
5
5
  # Contains all ISO8601-specific errors.
6
6
  module Errors
7
+
8
+ class StandardError < ::StandardError
9
+ end
7
10
  ##
8
11
  # Raised when the given pattern doesn't fit as ISO 8601 parser.
9
- class UnknownPattern < ::StandardError
12
+ class UnknownPattern < StandardError
10
13
  def initialize(pattern)
11
14
  super("The pattern “#{pattern}” is not allowed in this implementation of ISO8601.")
12
15
  end
13
16
  end
14
17
  ##
18
+ # Raised when the given pattern contains an invalid fraction.
19
+ class InvalidFractions < StandardError
20
+ def initialize(pattern)
21
+ super("Fractions are only allowed in the last component")
22
+ end
23
+ end
24
+ ##
15
25
  # Raised when the given date is valid but out of range.
16
- class RangeError < ::StandardError
26
+ class RangeError < StandardError
17
27
  def initialize(pattern)
18
28
  super("“#{pattern}” is out of range")
19
29
  end
20
30
  end
21
31
  ##
22
32
  # Raise when the base is not suitable.
23
- class DurationBaseError < ::StandardError
33
+ class DurationBaseError < StandardError
24
34
  def initialize(duration)
25
35
  super("Wrong base for #{duration} duration.")
26
36
  end
@@ -1,5 +1,5 @@
1
1
  module ISO8601
2
2
  ##
3
3
  # The gem version
4
- VERSION = '0.6.0'
4
+ VERSION = '0.7.0'
5
5
  end
@@ -13,14 +13,30 @@ describe ISO8601::Duration do
13
13
  expect { ISO8601::Duration.new('~P1Y') }.to raise_error(ISO8601::Errors::UnknownPattern)
14
14
  expect { ISO8601::Duration.new('.P1Y') }.to raise_error(ISO8601::Errors::UnknownPattern)
15
15
  end
16
+ it "should raise a ISO8601::Errors::InvalidFraction for any invalid patterns" do
17
+ expect { ISO8601::Duration.new('P1.5Y0.5M') }.to raise_error(ISO8601::Errors::InvalidFractions)
18
+ expect { ISO8601::Duration.new('P1.5Y1M') }.to raise_error(ISO8601::Errors::InvalidFractions)
19
+ expect { ISO8601::Duration.new('P1.5MT10.5S') }.to raise_error(ISO8601::Errors::InvalidFractions)
20
+ end
16
21
  it "should parse any allowed pattern" do
17
22
  expect { ISO8601::Duration.new('P1Y') }.to_not raise_error
23
+ expect { ISO8601::Duration.new('P0.5Y') }.to_not raise_error
24
+ expect { ISO8601::Duration.new('P0,5Y') }.to_not raise_error
18
25
  expect { ISO8601::Duration.new('P1Y1M') }.to_not raise_error
26
+ expect { ISO8601::Duration.new('P1Y0.5M') }.to_not raise_error
27
+ expect { ISO8601::Duration.new('P1Y0,5M') }.to_not raise_error
19
28
  expect { ISO8601::Duration.new('P1Y1M1D') }.to_not raise_error
29
+ expect { ISO8601::Duration.new('P1Y1M0.5D') }.to_not raise_error
30
+ expect { ISO8601::Duration.new('P1Y1M0,5D') }.to_not raise_error
20
31
  expect { ISO8601::Duration.new('P1Y1M1DT1H') }.to_not raise_error
32
+ expect { ISO8601::Duration.new('P1Y1M1DT0.5H') }.to_not raise_error
33
+ expect { ISO8601::Duration.new('P1Y1M1DT0,5H') }.to_not raise_error
21
34
  expect { ISO8601::Duration.new('P1Y1M1DT1H1M') }.to_not raise_error
35
+ expect { ISO8601::Duration.new('P1Y1M1DT1H0.5M') }.to_not raise_error
36
+ expect { ISO8601::Duration.new('P1Y1M1DT1H0,5M') }.to_not raise_error
22
37
  expect { ISO8601::Duration.new('P1Y1M1DT1H1M1S') }.to_not raise_error
23
38
  expect { ISO8601::Duration.new('P1Y1M1DT1H1M1.0S') }.to_not raise_error
39
+ expect { ISO8601::Duration.new('P1Y1M1DT1H1M1,0S') }.to_not raise_error
24
40
  expect { ISO8601::Duration.new('P1W') }.to_not raise_error
25
41
  expect { ISO8601::Duration.new('+P1Y') }.to_not raise_error
26
42
  expect { ISO8601::Duration.new('-P1Y') }.to_not raise_error
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iso8601
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arnau Siches