cooklang_rb 0.1.0 → 0.2.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
  SHA256:
3
- metadata.gz: b6a4fa9a28ae338fe05601537fee4f49e7b59564655320f16a0823ec0c21724a
4
- data.tar.gz: 87580d9a9822dfb94c3dc227bc5383f403a7100a9197e407f8ccbc703f375dcb
3
+ metadata.gz: 369e119fc8519a25a662143c5101fa77a877c6621faae48829d1933522f2fdb9
4
+ data.tar.gz: 4223c5f8ac7cdfd7c350bcc243aa8c2d4daa2f2c7090d49395a64a852e58bbe6
5
5
  SHA512:
6
- metadata.gz: 7210523498b4db675cf34f1a5afd6d6cb9b7e941e777f13252ebd21d2b62a1a8600ce9e35efcf1aae620047fd61209eb286cda4861a1f15738cd39d298a229e1
7
- data.tar.gz: 73e361bb6bbfb1361697e2b81b176537361c8e4f86e6aedf684db397503d37dfeaee1c9a78a20fb81cb12ec02e2ac5a84d9ccebf492e54b9a5df65bf7ba39550
6
+ metadata.gz: d1f5b4a42b1e84b3aaaa535c51a134f618cf36c9dd65dbfced720775269330d0e3ac25815c35817f7d873d809ef4e0850397e49ff9862d88c0d523d83251c040
7
+ data.tar.gz: 857a2e1fc4681a8b844b596e0716ab0fc5eed0f66557428c4372d58ccfbd0b19f05d080642bd33cb5e2012bdae3aad32f39dc0b99c746e25dba452e3ea6a9823
@@ -1,16 +1,25 @@
1
- name: Ruby
1
+ name: Test
2
2
 
3
- on: [push,pull_request]
3
+ on:
4
+ pull_request:
5
+ branches:
6
+ - '*'
7
+ push:
8
+ branches:
9
+ - master
4
10
 
5
11
  jobs:
6
12
  build:
7
13
  runs-on: ubuntu-latest
14
+ strategy:
15
+ matrix:
16
+ ruby: ['2.5', '2.6', '2.7', '3.0']
8
17
  steps:
9
18
  - uses: actions/checkout@v2
10
19
  - name: Set up Ruby
11
20
  uses: ruby/setup-ruby@v1
12
21
  with:
13
- ruby-version: 3.0.0
22
+ ruby-version: ${{ matrix.ruby }}
14
23
  - name: Run the default task
15
24
  run: |
16
25
  gem install bundler -v 2.2.3
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cooklang_rb (0.1.0)
4
+ cooklang_rb (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # CooklangRb
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/cooklang_rb.svg)](https://badge.fury.io/rb/cooklang_rb)
4
+ [![Ruby](https://github.com/DRBragg/cooklang_rb/actions/workflows/main.yml/badge.svg)](https://github.com/DRBragg/cooklang_rb/actions/workflows/main.yml)
5
+
3
6
  A Ruby parser for [Cooklang](https://cooklang.org/).
4
7
 
5
8
  If you're unfamiliar with [Cooklang](https://cooklang.org/), please visit [https://cooklang.org/](https://cooklang.org/) to learn more about using Cooklang.
data/cooklang_rb.gemspec CHANGED
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
12
12
  spec.description = "A cooklang parser for Ruby"
13
13
  spec.homepage = "https://github.com/drbragg/cooklang_rb"
14
14
  spec.license = "MIT"
15
- spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
16
16
 
17
17
  spec.metadata["homepage_uri"] = spec.homepage
18
18
  spec.metadata["source_code_uri"] = spec.homepage
@@ -0,0 +1,9 @@
1
+ module BufferUtils
2
+ def end_of_line?
3
+ @buffer.peek(1) == "\n"
4
+ end
5
+
6
+ def end_of_buffer?
7
+ @buffer.eos?
8
+ end
9
+ end
@@ -1,42 +1,16 @@
1
- require_relative "tag_parser"
2
- require_relative "steppable"
1
+ require_relative "tag"
3
2
 
4
3
  module CooklangRb
5
- class Cookware
6
- include TagParser
7
- include Steppable
8
-
4
+ class Cookware < Tag
9
5
  attr_reader :name, :quantity
10
6
 
11
7
  def self.tag
12
8
  COOKWARE_TAG
13
9
  end
14
10
 
15
- def self.multi_word_pattern
16
- /#{tag}#{MULTI_NAME}{#{QUANTITY}}/
17
- end
18
-
19
11
  def initialize(name:, quantity: 1)
20
- @name = name.delete_prefix(tag).chomp
21
- @quantity = normalize quantity
22
- end
23
-
24
- private
25
-
26
- def normalize(value)
27
- return value if value.is_a? Numeric
28
-
29
- value.strip! if value.respond_to?(:strip!)
30
-
31
- if value.include?(".")
32
- value.to_f
33
- elsif value.include?("/") && value[0] != "0"
34
- value.gsub(/\s/, "").to_r.to_f
35
- elsif value.match? /^[0-9]+$/
36
- value.to_i
37
- else
38
- value.empty? ? 1 : value
39
- end
12
+ @name = clean_name(name)
13
+ @quantity = resolve_quantity(quantity, default: 1)
40
14
  end
41
15
  end
42
16
  end
@@ -1,10 +1,9 @@
1
- require_relative "tag_parser"
2
- require_relative "steppable"
1
+ require_relative "tag"
2
+ require_relative "with_units"
3
3
 
4
4
  module CooklangRb
5
- class Ingredient
6
- include TagParser
7
- include Steppable
5
+ class Ingredient < Tag
6
+ include WithUnits
8
7
 
9
8
  attr_reader :name, :quantity, :units
10
9
 
@@ -13,27 +12,9 @@ module CooklangRb
13
12
  end
14
13
 
15
14
  def initialize(name:, quantity: "some", units: "")
16
- @name = name.delete_prefix(tag).chomp
17
- @quantity = normalize quantity
18
- @units = units&.strip || ""
19
- end
20
-
21
- private
22
-
23
- def normalize(value)
24
- return "some" if value.nil? || value.strip.empty?
25
-
26
- value.strip! if value.respond_to?(:strip!)
27
-
28
- if value.include?(".")
29
- value.to_f
30
- elsif value.include?("/") && value[0] != "0"
31
- value.gsub(/\s/, "").to_r.to_f
32
- elsif value.match? /^[0-9]+$/
33
- value.to_i
34
- else
35
- value
36
- end
15
+ @name = clean_name(name)
16
+ @quantity = resolve_quantity(quantity, default: "some")
17
+ @units = clean_units(units)
37
18
  end
38
19
  end
39
20
  end
@@ -21,8 +21,9 @@ module CooklangRb
21
21
  private
22
22
 
23
23
  def add_metadata
24
- data = PATTERN.match(@buffer.scan(PATTERN)).named_captures
25
- @metadata[data["key"].strip] = data["value"]
24
+ @buffer.scan(PATTERN)
25
+ key, value = @buffer.captures.map(&:strip)
26
+ @metadata[key] = value
26
27
  end
27
28
  end
28
29
  end
@@ -0,0 +1,18 @@
1
+ module QuantityResolver
2
+ def resolve_quantity(quantity, default: "")
3
+ return default if quantity.nil?
4
+ return quantity if quantity.is_a? Numeric
5
+
6
+ quantity.strip! if quantity.respond_to?(:strip!)
7
+
8
+ if quantity.include?(".")
9
+ quantity.to_f
10
+ elsif quantity.include?("/") && quantity[0] != "0"
11
+ quantity.gsub(/\s/, "").to_r.to_f
12
+ elsif quantity.match?(/^[0-9]+$/)
13
+ quantity.to_i
14
+ else
15
+ quantity.empty? ? default : quantity
16
+ end
17
+ end
18
+ end
@@ -1,13 +1,13 @@
1
1
  require "strscan"
2
2
 
3
- require_relative "ingredient"
4
- require_relative "cookware"
5
- require_relative "timer"
6
- require_relative "text"
7
3
  require_relative "metadata"
4
+ require_relative "step"
5
+ require_relative "buffer_utils"
8
6
 
9
7
  module CooklangRb
10
8
  class Recipe
9
+ include BufferUtils
10
+
11
11
  attr_reader :metadata, :steps
12
12
 
13
13
  def self.from(source)
@@ -37,7 +37,7 @@ module CooklangRb
37
37
 
38
38
  gather_metadata
39
39
 
40
- until @buffer.eos?
40
+ until end_of_buffer?
41
41
  parse_data
42
42
  end
43
43
  end
@@ -48,7 +48,7 @@ module CooklangRb
48
48
  end
49
49
 
50
50
  def parse_data
51
- if @buffer.peek(1) == "\n"
51
+ if end_of_line?
52
52
  @buffer.getch
53
53
  return
54
54
  end
@@ -57,26 +57,7 @@ module CooklangRb
57
57
  end
58
58
 
59
59
  def build_step
60
- step = []
61
-
62
- until @buffer.peek(1) == "\n" || @buffer.eos?
63
- step << take_step
64
- end
65
-
66
- step
67
- end
68
-
69
- def take_step
70
- case @buffer.peek(1)
71
- when "@"
72
- Ingredient.parse_from(@buffer).to_step
73
- when "#"
74
- Cookware.parse_from(@buffer).to_step
75
- when "~"
76
- Timer.parse_from(@buffer).to_step
77
- else
78
- Text.parse_from(@buffer).to_step
79
- end
60
+ Step.new(@buffer).build
80
61
  end
81
62
 
82
63
  def gather_metadata
@@ -0,0 +1,44 @@
1
+ require_relative "ingredient"
2
+ require_relative "cookware"
3
+ require_relative "timer"
4
+ require_relative "text"
5
+ require_relative "buffer_utils"
6
+
7
+ module CooklangRb
8
+ class Step
9
+ include BufferUtils
10
+
11
+ TAG_MAP = {
12
+ "@" => Ingredient,
13
+ "#" => Cookware,
14
+ "~" => Timer
15
+ }.freeze
16
+
17
+ def initialize(buffer)
18
+ @buffer = buffer
19
+ @content = []
20
+ end
21
+
22
+ def build
23
+ until end_of_step?
24
+ @content << take_step
25
+ end
26
+
27
+ @content
28
+ end
29
+
30
+ private
31
+
32
+ def take_step
33
+ next_step.parse_from(@buffer).to_step
34
+ end
35
+
36
+ def next_step
37
+ TAG_MAP[@buffer.peek(1)] || Text
38
+ end
39
+
40
+ def end_of_step?
41
+ end_of_line? || end_of_buffer?
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,15 @@
1
+ require_relative "tag_parser"
2
+ require_relative "steppable"
3
+ require_relative "quantity_resolver"
4
+
5
+ module CooklangRb
6
+ class Tag
7
+ include TagParser
8
+ include Steppable
9
+ include QuantityResolver
10
+
11
+ def clean_name(name)
12
+ name.delete_prefix(tag).chomp
13
+ end
14
+ end
15
+ end
@@ -21,14 +21,14 @@ module TagParser
21
21
  module ClassMethods
22
22
  def parse_from(buffer)
23
23
  if buffer.match? multi_word_pattern
24
- new **multi_word_pattern.match(buffer.scan(multi_word_pattern)).named_captures.transform_keys(&:to_sym)
24
+ new(**multi_word_pattern.match(buffer.scan(multi_word_pattern)).named_captures.transform_keys(&:to_sym))
25
25
  else
26
- new name: buffer.scan(single_word_pattern)
26
+ new(name: buffer.scan(single_word_pattern))
27
27
  end
28
28
  end
29
29
 
30
30
  def multi_word_pattern
31
- /#{tag}#{MULTI_NAME}?{#{QUANTITY}%?#{UNITS}}/
31
+ /#{tag}#{MULTI_NAME}{#{QUANTITY}}/
32
32
  end
33
33
 
34
34
  def single_word_pattern
@@ -1,10 +1,9 @@
1
- require_relative "tag_parser"
2
- require_relative "steppable"
1
+ require_relative "tag"
2
+ require_relative "with_units"
3
3
 
4
4
  module CooklangRb
5
- class Timer
6
- include TagParser
7
- include Steppable
5
+ class Timer < Tag
6
+ include WithUnits
8
7
 
9
8
  attr_reader :name, :quantity, :units
10
9
 
@@ -12,26 +11,10 @@ module CooklangRb
12
11
  TIMER_TAG
13
12
  end
14
13
 
15
- def initialize(name:, quantity: "some", units: "")
16
- @name = name ? name.delete_prefix(tag).chomp : ""
17
- @quantity = normalize quantity
18
- @units = units&.strip || ""
19
- end
20
-
21
- private
22
-
23
- def normalize(value)
24
- return "" if value.nil?
25
-
26
- if value.include?(".")
27
- value.to_f
28
- elsif value.include?("/") && value[0] != "0"
29
- value.gsub(/\s/, "").to_r.to_f
30
- elsif value.match? /^[0-9]+$/
31
- value.to_i
32
- else
33
- value
34
- end
14
+ def initialize(name:, quantity: "", units: "")
15
+ @name = name ? clean_name(name) : ""
16
+ @quantity = resolve_quantity(quantity)
17
+ @units = clean_units(units)
35
18
  end
36
19
  end
37
20
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CooklangRb
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
@@ -0,0 +1,18 @@
1
+ module WithUnits
2
+ def self.included(base)
3
+ base.include InstanceMethods
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ module InstanceMethods
8
+ def clean_units(units)
9
+ units&.strip || ""
10
+ end
11
+ end
12
+
13
+ module ClassMethods
14
+ def multi_word_pattern
15
+ /#{tag}#{TagParser::MULTI_NAME}?{#{TagParser::QUANTITY}%?#{TagParser::UNITS}}/
16
+ end
17
+ end
18
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cooklang_rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Drew Bragg
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-04-01 00:00:00.000000000 Z
11
+ date: 2022-04-04 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A cooklang parser for Ruby
14
14
  email:
@@ -29,15 +29,20 @@ files:
29
29
  - bin/setup
30
30
  - cooklang_rb.gemspec
31
31
  - lib/cooklang_rb.rb
32
+ - lib/cooklang_rb/buffer_utils.rb
32
33
  - lib/cooklang_rb/cookware.rb
33
34
  - lib/cooklang_rb/ingredient.rb
34
35
  - lib/cooklang_rb/metadata.rb
36
+ - lib/cooklang_rb/quantity_resolver.rb
35
37
  - lib/cooklang_rb/recipe.rb
38
+ - lib/cooklang_rb/step.rb
36
39
  - lib/cooklang_rb/steppable.rb
40
+ - lib/cooklang_rb/tag.rb
37
41
  - lib/cooklang_rb/tag_parser.rb
38
42
  - lib/cooklang_rb/text.rb
39
43
  - lib/cooklang_rb/timer.rb
40
44
  - lib/cooklang_rb/version.rb
45
+ - lib/cooklang_rb/with_units.rb
41
46
  homepage: https://github.com/drbragg/cooklang_rb
42
47
  licenses:
43
48
  - MIT
@@ -53,7 +58,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
53
58
  requirements:
54
59
  - - ">="
55
60
  - !ruby/object:Gem::Version
56
- version: 2.3.0
61
+ version: 2.5.0
57
62
  required_rubygems_version: !ruby/object:Gem::Requirement
58
63
  requirements:
59
64
  - - ">="