Class: Cooklang::Step

Inherits:
Object
  • Object
show all
Defined in:
lib/cooklang/step.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#segmentsObject (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_usedObject



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

Returns:

  • (Boolean)


67
68
69
# File 'lib/cooklang/step.rb', line 67

def eql?(other)
  self == other
end

#has_cookware?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/cooklang/step.rb', line 91

def has_cookware?
  cookware_used.any?
end

#has_ingredients?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/cooklang/step.rb', line 87

def has_ingredients?
  ingredients_used.any?
end

#has_timers?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/cooklang/step.rb', line 95

def has_timers?
  timers_used.any?
end

#hashObject



71
72
73
# File 'lib/cooklang/step.rb', line 71

def hash
  segments.hash
end

#ingredients_usedObject



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_usedObject



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_hObject



55
56
57
58
59
# File 'lib/cooklang/step.rb', line 55

def to_h
  {
    segments: @segments
  }
end

#to_textObject



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