Class: Cooklang::Step
- Inherits:
-
Object
- Object
- Cooklang::Step
- Defined in:
- lib/cooklang/step.rb
Instance Attribute Summary collapse
-
#segments ⇒ Object
readonly
Returns the value of attribute segments.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #cookware_used ⇒ Object
- #eql?(other) ⇒ Boolean
- #has_cookware? ⇒ Boolean
- #has_ingredients? ⇒ Boolean
- #has_timers? ⇒ Boolean
- #hash ⇒ Object
- #ingredients_used ⇒ Object
-
#initialize(segments:) ⇒ Step
constructor
A new instance of Step.
- #timers_used ⇒ Object
- #to_h ⇒ Object
- #to_text ⇒ Object
Constructor Details
#initialize(segments:) ⇒ Step
Returns a new instance of Step.
7 8 9 |
# File 'lib/cooklang/step.rb', line 7 def initialize(segments:) @segments = segments.freeze end |
Instance Attribute Details
#segments ⇒ Object (readonly)
Returns the value of attribute segments.
5 6 7 |
# File 'lib/cooklang/step.rb', line 5 def segments @segments end |
Instance Method Details
#==(other) ⇒ Object
61 62 63 64 65 |
# File 'lib/cooklang/step.rb', line 61 def ==(other) return false unless other.is_a?(Step) segments == other.segments end |
#cookware_used ⇒ Object
79 80 81 |
# File 'lib/cooklang/step.rb', line 79 def cookware_used @segments.filter_map { |segment| segment[:name] if segment.is_a?(Hash) && segment[:type] == :cookware } end |
#eql?(other) ⇒ Boolean
67 68 69 |
# File 'lib/cooklang/step.rb', line 67 def eql?(other) self == other end |
#has_cookware? ⇒ Boolean
91 92 93 |
# File 'lib/cooklang/step.rb', line 91 def has_cookware? cookware_used.any? end |
#has_ingredients? ⇒ Boolean
87 88 89 |
# File 'lib/cooklang/step.rb', line 87 def has_ingredients? ingredients_used.any? end |
#has_timers? ⇒ Boolean
95 96 97 |
# File 'lib/cooklang/step.rb', line 95 def has_timers? timers_used.any? end |
#hash ⇒ Object
71 72 73 |
# File 'lib/cooklang/step.rb', line 71 def hash segments.hash end |
#ingredients_used ⇒ Object
75 76 77 |
# File 'lib/cooklang/step.rb', line 75 def ingredients_used @segments.filter_map { |segment| segment[:name] if segment.is_a?(Hash) && segment[:type] == :ingredient } end |
#timers_used ⇒ Object
83 84 85 |
# File 'lib/cooklang/step.rb', line 83 def timers_used @segments.select { |segment| segment.is_a?(Hash) && segment[:type] == :timer } end |
#to_h ⇒ Object
55 56 57 58 59 |
# File 'lib/cooklang/step.rb', line 55 def to_h { segments: @segments } end |
#to_text ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/cooklang/step.rb', line 11 def to_text text_parts = @segments.map do |segment| case segment when Hash case segment[:type] when :ingredient segment[:name] when :cookware segment[:name] when :timer segment[:name] || "timer" else segment[:value] || "" end when String segment when Ingredient segment.name when Cookware segment.name when Timer if segment.name "#{segment.name} for #{segment.duration} #{segment.unit}" elsif segment.duration && segment.unit "for #{segment.duration} #{segment.unit}" else "timer" end else segment.to_s end end result = text_parts.join # Special normalization for metadata break patterns: # Convert sequences like "--- \n text \n ---" to "--- text ---" if result.include?("---") result = result.gsub(/---\s*\n\s*/, "--- ").gsub(/\s*\n\s*---/, " ---") end result.rstrip end |