datemath 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 233e2bd36ca6201e4626ce77408e011d810b345ae0258145aa280060fe65d539
4
- data.tar.gz: 380afab2e63824722d60846b08f933d62c39ab3ce25d78140a4db435e9c6cc19
3
+ metadata.gz: 23cef9f2a120a8bdb84d14e43802d3fb6c8d73f02426159bfc8f774a85c2f48e
4
+ data.tar.gz: 4a9952a60d8c4fc80d5f9e181e5ce6ac5dc8ed3666b41b4afde2593608c3712b
5
5
  SHA512:
6
- metadata.gz: 6dc61018e16ba75a21461da7c57ee4eb2c83839b1b392efdf63591958a76bf7b3fa09e8b367e7d48c717a02b5131a69dc0f187bb857f0ece2657f325a00ef523
7
- data.tar.gz: 50c216362f841623437f7f5f07620bab4f4d0cf5980115e9f9cc07e6dbfd300f7c0f8d40aab2eaca612e6d360640de681e3c306c197c1fb4c530abc5563f374b
6
+ metadata.gz: b9babe3938ad79aacc5a241d11668f694b58abe033d56b8660282640df4e4ce36497259f5008a9963fc9c4ce6e343fbd9cd40259ef75e47ec844857100ff729f
7
+ data.tar.gz: '04488afd1884c1dccebd1ec914b3b92e2a55c8c35c6e01e020a8e96fe7116ecf53af2715d21290722d6ea07788897e9555441c8c82d2f38f70632f95e2cb63c6'
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- datemath (0.1.0)
4
+ datemath (0.2.0)
5
5
  activesupport
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -27,37 +27,37 @@ Here are a few examples:
27
27
  Current date time:
28
28
 
29
29
  ```ruby
30
- Datemath::Parser.new.parse("now")
30
+ Datemath::Parser.new("now").parse
31
31
  ```
32
32
 
33
33
  Specific date time:
34
34
 
35
35
  ```ruby
36
- Datemath::Parser.new.parse("2015-05-05T00:00:00")
36
+ Datemath::Parser.new("2015-05-05T00:00:00").parse
37
37
  ```
38
38
 
39
39
  Complex expression:
40
40
 
41
41
  ```ruby
42
- Datemath::Parser.new.parse("now+1d")
42
+ Datemath::Parser.new("now+1d").parse
43
43
  ```
44
44
 
45
45
  Multiple operations:
46
46
 
47
47
  ```ruby
48
- Datemath::Parser.new.parse("now+1d-1m")
48
+ Datemath::Parser.new("now+1d-1m").parse
49
49
  ```
50
50
 
51
51
  Rounding:
52
52
 
53
53
  ```ruby
54
- Datemath::Parser.new.parse("now+1d-1m/d")
54
+ Datemath::Parser.new("now+1d-1m/d").parse
55
55
  ```
56
56
 
57
57
  Anchoring dates:
58
58
 
59
59
  ```ruby
60
- Datemath::Parser.new.parse("2015-05-05T00:00:00||+1d-1m")
60
+ Datemath::Parser.new("2015-05-05T00:00:00||+1d-1m").parse
61
61
  ```
62
62
 
63
63
  You can see more cases here: [here](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/date-math-expressions.html)
@@ -3,7 +3,8 @@ module Datemath
3
3
 
4
4
  # Initialize
5
5
  #
6
- def initialize
6
+ def initialize(text)
7
+ @text = text
7
8
  @units = ['y', 'M', 'w', 'd', 'h', 'm', 's', 'ms']
8
9
  end
9
10
 
@@ -12,25 +13,25 @@ module Datemath
12
13
  # @param [String] text
13
14
  # @param [Boolean] round_up
14
15
  # @return [DateTime]
15
- def parse(text, round_up = false)
16
- return nil unless text
16
+ def parse(round_up = false)
17
+ return nil unless @text
17
18
 
18
19
  time = nil
19
20
  math_string = ''
20
21
  index = nil
21
22
  parse_string = nil
22
23
 
23
- if (text[0, 3] == 'now')
24
+ if (@text[0, 3] == 'now')
24
25
  time = DateTime.now
25
- math_string = text['now'.length..text.length]
26
+ math_string = @text['now'.length..@text.length]
26
27
  else
27
- index = text.index('||')
28
+ index = @text.index('||')
28
29
  if index.nil?
29
- parse_string = text
30
+ parse_string = @text
30
31
  math_string = ''
31
32
  else
32
- parse_string = text[0, index]
33
- math_string = text[(index + 2)..text.length]
33
+ parse_string = @text[0, index]
34
+ math_string = @text[(index + 2)..@text.length]
34
35
  end
35
36
  time = DateTime.parse(parse_string) rescue nil
36
37
  end
@@ -39,6 +40,8 @@ module Datemath
39
40
  parse_date_math(math_string, time, round_up)
40
41
  end
41
42
 
43
+ private
44
+
42
45
  # Handles math_string to manipulate a given datetime
43
46
  #
44
47
  # @param [String] math_string
@@ -1,3 +1,3 @@
1
1
  module Datemath
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: datemath
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - victor-aguilars