camp_recipe_scaler 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e9bfca45b6520367a4a2b05a960246ca479a57d41ea1a95c0773ce1f1e1606a4
4
+ data.tar.gz: 4e97f408f9032f221f4803b591c937f3d3f7309646c0145835ca00134ded02e0
5
+ SHA512:
6
+ metadata.gz: bac3a4949ae49a99eb2ce0fc77313bbd4f31b04928b2ed4c538d30e5f95729e033995c1f7e3dc3b4ebf81d550406118258e488a98cd364186dd0f0287d96e12d
7
+ data.tar.gz: 5cb488a7b02a7e57238dc1b3ff6acdd34c29cb3fce6dd0c63d08d93b95b6a9b43f22eb278c1ea686e1b32f8ad70a001a49caa7ef6d0db46aa007069224baa2a9
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Camp Kitchen Field Notes
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,75 @@
1
+ # camp_recipe_scaler
2
+
3
+ Cooking for four on Friday and nine on Saturday? `camp_recipe_scaler` reads a plain-text recipe, scales it to the number of people at the table, and merges several recipes into one shopping list, so the cooler gets packed once. It is a small, dependency-free Ruby library and command line tool, made for the outdoor cooks behind [Camp Kitchen](https://campkitchen.us/).
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ gem install camp_recipe_scaler
9
+ ```
10
+
11
+ ## Recipe format
12
+
13
+ ```
14
+ # One-pot rice and beans
15
+ servings: 4
16
+ 1 1/2 cups rice
17
+ 2 cans black beans
18
+ 1 onion
19
+ 2 cloves garlic
20
+ 1 tbsp oil
21
+ 1/2 tsp salt
22
+ 3 cups water
23
+ ```
24
+
25
+ Quantities can be whole numbers, decimals, fractions (`1/2`) or mixed numbers (`1 1/2`). Recognised units: tsp, tbsp, cup, oz, lb, g, kg, can, clove, pinch. A line without a quantity (`salt to taste`) is kept as is.
26
+
27
+ ## Command line
28
+
29
+ ```sh
30
+ camp-scale --people 6 examples/rice_and_beans.txt
31
+ camp-scale --people 6 --shopping examples/skillet_potatoes.txt examples/rice_and_beans.txt
32
+ ```
33
+
34
+ ```
35
+ Shopping list for 6 people
36
+ - 3 cans black beans
37
+ - 3 cloves garlic
38
+ - 7 1/2 tbsp oil
39
+ - 5 x onion
40
+ - 1 tbsp paprika
41
+ - 3 lb potatoes
42
+ - 2 1/4 cups rice
43
+ - 2 1/4 tsp salt
44
+ - 1 1/2 lb smoked sausage
45
+ - 4 1/2 cups water
46
+ ```
47
+
48
+ ## Library
49
+
50
+ ```ruby
51
+ require "camp_recipe_scaler"
52
+
53
+ recipe = CampRecipeScaler.parse_recipe(File.read("examples/rice_and_beans.txt"))
54
+ puts recipe.scale(6)
55
+
56
+ list = CampRecipeScaler.shopping_list([recipe.scale(6), other_recipe.scale(6)])
57
+ list.each { |i| puts CampRecipeScaler.format_ingredient(i) }
58
+ ```
59
+
60
+ ## Things to know
61
+
62
+ - Scaling is linear. Salt, spices and cooking time do not always scale that neatly, so season to taste and watch the pot. Doubling a recipe in a camp pan often takes longer than the original.
63
+ - Whole items (cans, cloves, unitless items such as onions) are rounded up on the shopping list.
64
+ - Volume units are merged with volume units, US mass with US mass and metric mass with metric mass. A cup of rice and 100 g of rice stay on separate lines because the gem does not guess densities.
65
+ - Quantities are shown to the nearest 1/4 (1/8 for teaspoons).
66
+
67
+ ## Development
68
+
69
+ ```sh
70
+ ruby -Ilib -Itest test/camp_recipe_scaler_test.rb
71
+ ```
72
+
73
+ ## License
74
+
75
+ MIT
@@ -0,0 +1,9 @@
1
+ # One-pot rice and beans
2
+ servings: 4
3
+ 1 1/2 cups rice
4
+ 2 cans black beans
5
+ 1 onion
6
+ 2 cloves garlic
7
+ 1 tbsp oil
8
+ 1/2 tsp salt
9
+ 3 cups water
@@ -0,0 +1,8 @@
1
+ # Skillet potatoes and sausage
2
+ servings: 2
3
+ 1 lb potatoes
4
+ 8 oz smoked sausage
5
+ 1 onion
6
+ 2 tbsp oil
7
+ 1/2 tsp salt
8
+ 1 tsp paprika
data/exe/camp-scale ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "camp_recipe_scaler"
5
+ require "optparse"
6
+
7
+ people = nil
8
+ shopping = false
9
+ OptionParser.new do |o|
10
+ o.banner = "Usage: camp-scale --people N [--shopping] recipe.txt [more.txt ...]"
11
+ o.on("--people N", Float) { |v| people = v }
12
+ o.on("--shopping", "print one merged shopping list") { shopping = true }
13
+ end.parse!
14
+ abort "need --people N and at least one recipe file" if people.nil? || ARGV.empty?
15
+
16
+ begin
17
+ recipes = ARGV.map { |f| CampRecipeScaler.parse_recipe(File.read(f)).scale(people) }
18
+ rescue CampRecipeScaler::ParseError, Errno::ENOENT => e
19
+ abort "error: #{e.message}"
20
+ end
21
+
22
+ if shopping
23
+ puts "Shopping list for #{CampRecipeScaler.format_number(people)} people"
24
+ CampRecipeScaler.shopping_list(recipes).each { |i| puts " - #{CampRecipeScaler.format_ingredient(i)}" }
25
+ else
26
+ puts recipes.map(&:to_s).join("\n\n")
27
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CampRecipeScaler
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,205 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "camp_recipe_scaler/version"
4
+
5
+ # Scale camp recipes to the number of people at the table and merge several
6
+ # recipes into one shopping list.
7
+ module CampRecipeScaler
8
+ class ParseError < StandardError; end
9
+
10
+ Ingredient = Struct.new(:name, :quantity, :unit, keyword_init: true)
11
+
12
+ # US customary units. Each family has a base unit; factor is "base units per unit".
13
+ VOLUME = { "tsp" => 1.0, "tbsp" => 3.0, "cup" => 48.0 }.freeze # base: teaspoon
14
+ MASS = { "oz" => 1.0, "lb" => 16.0 }.freeze # base: ounce
15
+ METRIC_MASS = { "g" => 1.0, "kg" => 1000.0 }.freeze # base: gram
16
+
17
+ ALIASES = {
18
+ "teaspoon" => "tsp", "teaspoons" => "tsp", "tsp" => "tsp", "t" => "tsp",
19
+ "tablespoon" => "tbsp", "tablespoons" => "tbsp", "tbsp" => "tbsp", "tbs" => "tbsp",
20
+ "cup" => "cup", "cups" => "cup", "c" => "cup",
21
+ "ounce" => "oz", "ounces" => "oz", "oz" => "oz",
22
+ "pound" => "lb", "pounds" => "lb", "lb" => "lb", "lbs" => "lb",
23
+ "gram" => "g", "grams" => "g", "g" => "g",
24
+ "kilogram" => "kg", "kilograms" => "kg", "kg" => "kg",
25
+ "can" => "can", "cans" => "can",
26
+ "clove" => "clove", "cloves" => "clove",
27
+ "pinch" => "pinch"
28
+ }.freeze
29
+
30
+ class Recipe
31
+ attr_reader :name, :servings, :ingredients
32
+
33
+ def initialize(name:, servings:, ingredients:)
34
+ raise ArgumentError, "servings must be positive" unless servings.to_f.positive?
35
+
36
+ @name = name
37
+ @servings = servings
38
+ @ingredients = ingredients
39
+ end
40
+
41
+ # Return a new recipe for +people+ servings.
42
+ def scale(people)
43
+ raise ArgumentError, "people must be positive" unless people.to_f.positive?
44
+
45
+ factor = people.to_f / servings
46
+ scaled = ingredients.map do |i|
47
+ Ingredient.new(name: i.name, quantity: i.quantity && i.quantity * factor, unit: i.unit)
48
+ end
49
+ Recipe.new(name: name, servings: people, ingredients: scaled)
50
+ end
51
+
52
+ def to_s
53
+ lines = ["#{name} (serves #{CampRecipeScaler.format_number(servings)})"]
54
+ ingredients.each { |i| lines << " - #{CampRecipeScaler.format_ingredient(i)}" }
55
+ lines.join("\n")
56
+ end
57
+ end
58
+
59
+ module_function
60
+
61
+ # Parse "1 1/2 lb potatoes", "2 cloves garlic", "salt" or "1/2 tsp salt".
62
+ def parse_ingredient(line)
63
+ text = line.strip.sub(/\A[-*]\s*/, "")
64
+ raise ParseError, "empty ingredient line" if text.empty?
65
+
66
+ m = text.match(%r{\A(\d+\s+\d+/\d+|\d+/\d+|\d+(?:\.\d+)?)\s*(.*)\z})
67
+ return Ingredient.new(name: text, quantity: nil, unit: nil) unless m
68
+
69
+ quantity = parse_number(m[1])
70
+ rest = m[2].strip
71
+ raise ParseError, "missing ingredient name in #{line.inspect}" if rest.empty?
72
+
73
+ word, remainder = rest.split(/\s+/, 2)
74
+ unit = ALIASES[word.downcase]
75
+ if unit && remainder
76
+ Ingredient.new(name: remainder.strip, quantity: quantity, unit: unit)
77
+ else
78
+ Ingredient.new(name: rest, quantity: quantity, unit: nil)
79
+ end
80
+ end
81
+
82
+ def parse_number(str)
83
+ parts = str.split
84
+ parts.sum do |p|
85
+ if p.include?("/")
86
+ n, d = p.split("/").map(&:to_f)
87
+ raise ParseError, "zero denominator in #{str.inspect}" if d.zero?
88
+
89
+ n / d
90
+ else
91
+ p.to_f
92
+ end
93
+ end
94
+ end
95
+
96
+ # Recipe text format:
97
+ # # Title
98
+ # servings: 4
99
+ # 1 lb potatoes
100
+ def parse_recipe(text)
101
+ name = nil
102
+ servings = nil
103
+ ingredients = []
104
+ text.each_line do |raw|
105
+ line = raw.strip
106
+ next if line.empty?
107
+
108
+ if line.start_with?("#")
109
+ name ||= line.sub(/\A#+\s*/, "")
110
+ elsif (m = line.match(/\Aservings:\s*(\d+(?:\.\d+)?)\z/i))
111
+ servings = m[1].to_f
112
+ else
113
+ ingredients << parse_ingredient(line)
114
+ end
115
+ end
116
+ raise ParseError, "recipe needs a '# Title' line" unless name
117
+ raise ParseError, "recipe needs a 'servings: N' line" unless servings
118
+ raise ParseError, "recipe has no ingredients" if ingredients.empty?
119
+
120
+ Recipe.new(name: name, servings: servings, ingredients: ingredients)
121
+ end
122
+
123
+ # Merge ingredients from several recipes. Same-name items are added when
124
+ # their units are compatible (volume, US mass, metric mass, or both unitless/same unit).
125
+ def shopping_list(recipes)
126
+ groups = {}
127
+ recipes.flat_map(&:ingredients).each do |i|
128
+ key = [i.name.downcase, family(i.unit)]
129
+ (groups[key] ||= []) << i
130
+ end
131
+ groups.values.map { |items| merge(items) }.sort_by { |i| i.name.downcase }
132
+ end
133
+
134
+ def family(unit)
135
+ return :volume if VOLUME.key?(unit)
136
+ return :mass if MASS.key?(unit)
137
+ return :metric if METRIC_MASS.key?(unit)
138
+
139
+ unit || :none
140
+ end
141
+
142
+ def merge(items)
143
+ first = items.first
144
+ return Ingredient.new(name: first.name, quantity: nil, unit: first.unit) if items.all? { |i| i.quantity.nil? }
145
+
146
+ table = VOLUME.merge(MASS).merge(METRIC_MASS)
147
+ fam = family(first.unit)
148
+ if %i[volume mass metric].include?(fam)
149
+ base = items.sum { |i| (i.quantity || 0) * table[i.unit] }
150
+ qty, unit = best_unit(base, fam)
151
+ Ingredient.new(name: first.name, quantity: qty, unit: unit)
152
+ else
153
+ Ingredient.new(name: first.name, quantity: items.sum { |i| i.quantity || 0 }, unit: first.unit)
154
+ end
155
+ end
156
+
157
+ def best_unit(base, fam)
158
+ case fam
159
+ when :volume
160
+ return [base / 48.0, "cup"] if base >= 48
161
+ return [base / 3.0, "tbsp"] if base >= 3
162
+
163
+ [base, "tsp"]
164
+ when :mass
165
+ base >= 16 ? [base / 16.0, "lb"] : [base, "oz"]
166
+ else
167
+ base >= 1000 ? [base / 1000.0, "kg"] : [base, "g"]
168
+ end
169
+ end
170
+
171
+ # Friendly quantity: whole numbers or fractions with the given denominator
172
+ # (8 for teaspoons, 4 for everything else).
173
+ def format_number(x, denominator = 4)
174
+ steps = (x * denominator).round
175
+ whole, rest = steps.divmod(denominator)
176
+ return whole.to_s if rest.zero?
177
+
178
+ f = Rational(rest, denominator)
179
+ fs = "#{f.numerator}/#{f.denominator}"
180
+ whole.zero? ? fs : "#{whole} #{fs}"
181
+ end
182
+
183
+ def format_ingredient(i)
184
+ return i.name if i.quantity.nil?
185
+
186
+ qty = if METRIC_MASS.key?(i.unit)
187
+ i.quantity >= 100 ? i.quantity.round.to_s : format("%.1f", i.quantity).sub(/\.0\z/, "")
188
+ elsif i.unit.nil? || i.unit == "can" || i.unit == "clove"
189
+ # whole items: round up so nobody goes hungry
190
+ i.quantity.ceil.to_s
191
+ else
192
+ format_number(i.quantity, i.unit == "tsp" ? 8 : 4)
193
+ end
194
+ return "#{qty} x #{i.name}" if i.unit.nil?
195
+
196
+ [qty, plural(i.unit, i.quantity), i.name].join(" ")
197
+ end
198
+
199
+ def plural(unit, qty)
200
+ return nil if unit.nil?
201
+ return unit unless %w[can clove cup].include?(unit)
202
+
203
+ qty.ceil > 1 || qty > 1 ? "#{unit}s" : unit
204
+ end
205
+ end
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: camp_recipe_scaler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Camp Kitchen Field Notes
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: Dependency-free Ruby library and command line tool that parses simple
13
+ plain-text recipes, scales ingredient quantities to the number of people at the
14
+ campsite, and merges several recipes into one shopping list with compatible units
15
+ combined (teaspoon, tablespoon, cup, ounce, pound, gram, kilogram).
16
+ email:
17
+ - campkitchenus@gmail.com
18
+ executables:
19
+ - camp-scale
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - LICENSE.txt
24
+ - README.md
25
+ - examples/rice_and_beans.txt
26
+ - examples/skillet_potatoes.txt
27
+ - exe/camp-scale
28
+ - lib/camp_recipe_scaler.rb
29
+ - lib/camp_recipe_scaler/version.rb
30
+ homepage: https://campkitchen.us/
31
+ licenses:
32
+ - MIT
33
+ metadata:
34
+ homepage_uri: https://campkitchen.us/
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 3.0.0
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubygems_version: 3.6.7
50
+ specification_version: 4
51
+ summary: Scale camp recipes to the group size and build one shopping list.
52
+ test_files: []