ingreedy 0.0.9 → 0.1.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
  SHA1:
3
- metadata.gz: 452f17ed3a15ae973346ea074ee682bf173d1663
4
- data.tar.gz: 6face6a2f60f6506160d815c69cd0546107fb787
3
+ metadata.gz: 7c24a989bf96f54f8190d1b6fd568a8e8c143954
4
+ data.tar.gz: cf1c0034a35aac3dfa9ee7c9ac4914756349591e
5
5
  SHA512:
6
- metadata.gz: acdc3c1f4f4c135188c9c92e4a83441d569539f4a9b0af31cdfd08e8ca653ce81689dff985d72814d8b71f10f59b4d1eb936cf46f6d35e99aeb1a0b31cc42275
7
- data.tar.gz: b0801f2d7eb26f02eb14cd615102b00dfe5eb695ed03f3b53dd32bf2f62c2df1cc70ae5cebcd552634403de39dc94c3d859d1271b31ba8f3930ad77d03668421
6
+ metadata.gz: 5941276bb95a4068ab44a4cf41923cf7b1f2ad2d243badeea3ffaacd8d3cb0601a10f29a32c33634c9dfb81c3f400db6d3a90ef16369ac7526607d9703356a9d
7
+ data.tar.gz: 94055cb4fa3134feec302d5a741161ac9f365eb03c52c80b5de40d540e4919f9cf8cc0b23adc1e513d3abd30b9945b3315b46628ec6666b36522c85217af76ec
@@ -6,4 +6,10 @@ rvm:
6
6
  - 2.2.3
7
7
  - 2.3.0
8
8
  - ruby-head
9
- - jruby-head
9
+ - jruby-1.7.20
10
+ - jruby-9.0.5.0
11
+ matrix:
12
+ allow_failures:
13
+ - rvm: ruby-head
14
+ - rvm: jruby-1.7.20
15
+ - rvm: jruby-9.0.5.0
data/README.md CHANGED
@@ -30,6 +30,27 @@ print result.ingredient
30
30
  #=> "sucre"
31
31
  ```
32
32
 
33
+ ### Handling amounts
34
+
35
+ By default, Ingreedy will convert all amounts to a rational number:
36
+
37
+ ```ruby
38
+ result = Ingreedy.parse("1 1/2 cups flour")
39
+ print result.amount
40
+ #=> 3/2
41
+ ```
42
+
43
+ However, setting `Ingreedy.preverse_amounts = true`, will allow amounts
44
+ to be detected and returned as originally input:
45
+
46
+ ```ruby
47
+ Ingreedy.preserve_amounts = true
48
+
49
+ result = Ingreedy.parse("1 1/2 cups flour")
50
+ print result.amount
51
+ #=> 1 1/2
52
+ ```
53
+
33
54
  [Live demo](http://hangryingreedytest.herokuapp.com/)
34
55
 
35
56
  # Pieces of Flair
@@ -7,12 +7,8 @@ require File.join(path, "dictionary_collection")
7
7
  module Ingreedy
8
8
  ParseFailed = Class.new(StandardError)
9
9
 
10
- def self.locale
11
- @locale ||= nil
12
- end
13
-
14
- def self.locale=(locale)
15
- @locale = locale
10
+ class << self
11
+ attr_accessor :locale, :preserve_amounts
16
12
  end
17
13
 
18
14
  def self.parse(query)
@@ -59,26 +59,36 @@
59
59
  - "gr."
60
60
  - "gram"
61
61
  - "grams"
62
+ - "gramme"
63
+ - "grammes"
62
64
  :kilogram:
63
65
  - "kg"
64
66
  - "kg."
65
67
  - "kilogram"
66
68
  - "kilograms"
69
+ - "kilogramme"
70
+ - "kilogrammes"
67
71
  :liter:
68
72
  - "l"
69
73
  - "l."
70
74
  - "liter"
71
75
  - "liters"
76
+ - "litre"
77
+ - "litres"
72
78
  :milligram:
73
79
  - "mg"
74
80
  - "mg."
75
81
  - "milligram"
76
82
  - "milligrams"
83
+ - "milligramme"
84
+ - "milligrammes"
77
85
  :milliliter:
78
86
  - "ml"
79
87
  - "ml."
80
88
  - "milliliter"
81
89
  - "milliliters"
90
+ - "millilitre"
91
+ - "millilitres"
82
92
  :pinch:
83
93
  - "pinch"
84
94
  - "pinches"
@@ -132,6 +142,6 @@
132
142
  sixty: 60
133
143
  seventy: 70
134
144
  eighty: 80
135
- ninety: 9
145
+ ninety: 90
136
146
  :prepositions:
137
147
  - "of"
@@ -12,37 +12,43 @@ module Ingreedy
12
12
  end
13
13
 
14
14
  def rationalize
15
- if @word
16
- result = rationalize_word
17
- elsif @fraction
18
- result = rationalize_fraction
19
- if @integer
20
- result += @integer.to_i
21
- end
22
- elsif @integer
23
- result = @integer.to_r
24
- elsif @float
25
- result = @float.tr(",", ".").to_r
15
+ if Ingreedy.preserve_amounts
16
+ (normalized_word || compound_fraction || @float || @integer)
17
+ else
18
+ (normalized_word || rationalized_fraction || rationalized_float || @integer).to_r
26
19
  end
27
-
28
- result
29
20
  end
30
21
 
31
22
  private
32
23
 
33
- def rationalize_fraction
34
- vulgar_fractions.each do |char, amount|
35
- @fraction.gsub!(char, amount.to_s)
24
+ def normalized_word
25
+ return unless @word
26
+ Ingreedy.dictionaries.current.numbers[@word.downcase]
27
+ end
28
+
29
+ def normalized_fraction
30
+ @fraction.tap do |fraction|
31
+ Ingreedy.dictionaries.current.vulgar_fractions.each do |char, amount|
32
+ fraction.gsub!(char, amount.to_s)
33
+ end
36
34
  end
37
- @fraction.to_r
38
35
  end
39
36
 
40
- def vulgar_fractions
41
- Ingreedy.dictionaries.current.vulgar_fractions
37
+ def rationalized_fraction
38
+ return unless @fraction
39
+ result = normalized_fraction
40
+ result = result.to_r + @integer.to_i
41
+ result
42
42
  end
43
43
 
44
- def rationalize_word
45
- Ingreedy.dictionaries.current.numbers[@word.downcase]
44
+ def compound_fraction
45
+ return unless @fraction
46
+ "#{@integer} #{normalized_fraction}".strip
47
+ end
48
+
49
+ def rationalized_float
50
+ return unless @float
51
+ @float.tr(",", ".")
46
52
  end
47
53
  end
48
54
  end
@@ -1,3 +1,3 @@
1
1
  module Ingreedy
2
- VERSION = "0.0.9"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -41,6 +41,32 @@ describe Ingreedy, "amount parsing" do
41
41
  end
42
42
  end
43
43
 
44
+ describe Ingreedy, "amount parsing preserving original value" do
45
+ around do |example|
46
+ Ingreedy.preserve_amounts = true
47
+ example.run
48
+ Ingreedy.preserve_amounts = false
49
+ end
50
+
51
+ {
52
+ "1 cup flour" => "1",
53
+ "2 cups flour" => "2",
54
+ "1 1/2 cups flour" => "1 1/2",
55
+ "¼ cups flour" => "1/4",
56
+ "1 ½ cups flour" => "1 1/2",
57
+ "1½ cups flour" => "1 1/2",
58
+ "1.0 cup flour" => "1.0",
59
+ "1.5 cups flour" => "1.5",
60
+ "1,5 cups flour" => "1,5",
61
+ "1/2 cups flour" => "1/2",
62
+ ".25 cups flour" => ".25",
63
+ }.each do |query, expected|
64
+ it "parses the correct amount" do
65
+ expect(Ingreedy.parse(query).amount).to eq(expected)
66
+ end
67
+ end
68
+ end
69
+
44
70
  describe Ingreedy, "english units" do
45
71
  context "abbreviated" do
46
72
  {
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ingreedy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ian C. Anderson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-26 00:00:00.000000000 Z
11
+ date: 2016-08-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parslet
@@ -157,7 +157,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
157
157
  version: '0'
158
158
  requirements: []
159
159
  rubyforge_project:
160
- rubygems_version: 2.4.8
160
+ rubygems_version: 2.4.5.1
161
161
  signing_key:
162
162
  specification_version: 4
163
163
  summary: Recipe parser