date_interval 0.0.2 → 0.0.3
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 +4 -4
- data/README.md +15 -1
- data/lib/date_interval.rb +16 -3
- data/lib/date_interval/version.rb +1 -1
- data/spec/date_interval_spec.rb +8 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d152db4e7865cac25d7a2b1b08d2cc80bbe8eb20
|
4
|
+
data.tar.gz: b5e555298cd3a97e4fe67896d0d2453e2f7f7829
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1a9d2410b4bf99186e5c19537f21e28fce8e30628a9e748cba412af0fb37c3de5a863bb49b3453f9051f9d1b984c05c8a123627e6ac6e9727e8091b8ea801890
|
7
|
+
data.tar.gz: bea237b30f2c170ece7b345e74b2bb5eacdc7bb5a70a0194b6ed422f829a8173b27b99c6fc81f3c2c0ef5905d71d822bae5ee50a3048bdc1b6baabffb81dbde4
|
data/README.md
CHANGED
@@ -97,9 +97,23 @@ DateInterval.parse("2014-01-01 - 2014-12-31, none, +holidays")
|
|
97
97
|
# ]
|
98
98
|
```
|
99
99
|
|
100
|
+
### Validating expressions
|
101
|
+
|
102
|
+
To validation if an expression is valid use the `DateInterval.valid?` method.
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
require "date_interval"
|
106
|
+
|
107
|
+
DateInterval.valid?("2014-01-01 - 2014-01-05")
|
108
|
+
#=> true
|
109
|
+
|
110
|
+
DateInterval.valid?("invalid")
|
111
|
+
#=> false
|
112
|
+
```
|
113
|
+
|
100
114
|
## Contributing
|
101
115
|
|
102
|
-
1. Fork it ( http://github.com/
|
116
|
+
1. Fork it ( http://github.com/fnando/date_interval/fork )
|
103
117
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
104
118
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
105
119
|
4. Push to the branch (`git push origin my-new-feature`)
|
data/lib/date_interval.rb
CHANGED
@@ -19,9 +19,6 @@ module DateInterval
|
|
19
19
|
InvalidRuleError = Class.new(StandardError)
|
20
20
|
|
21
21
|
def self.parse(expression)
|
22
|
-
parser = Parser.new
|
23
|
-
transformer = Transformer.new
|
24
|
-
|
25
22
|
tree = parser.parse(expression)
|
26
23
|
ast = transformer.apply(tree)
|
27
24
|
|
@@ -30,4 +27,20 @@ module DateInterval
|
|
30
27
|
ast[:filters].kind_of?(Array) ? ast[:filters] : []
|
31
28
|
)
|
32
29
|
end
|
30
|
+
|
31
|
+
def self.valid?(expression)
|
32
|
+
parser.parse(expression)
|
33
|
+
return true
|
34
|
+
rescue Parslet::ParseFailed
|
35
|
+
return false
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
def self.parser
|
40
|
+
Parser.new
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.transformer
|
44
|
+
Transformer.new
|
45
|
+
end
|
33
46
|
end
|
data/spec/date_interval_spec.rb
CHANGED
@@ -191,4 +191,12 @@ describe DateInterval do
|
|
191
191
|
|
192
192
|
expect(interval).to eql(expected_interval)
|
193
193
|
end
|
194
|
+
|
195
|
+
it "detects expression as valid" do
|
196
|
+
expect(DateInterval).to be_valid("2014-01-01 - 2014-01-02")
|
197
|
+
end
|
198
|
+
|
199
|
+
it "detects expression as invalid" do
|
200
|
+
expect(DateInterval).not_to be_valid("invalid")
|
201
|
+
end
|
194
202
|
end
|