Class: Cooklang::Ingredient

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, quantity: nil, unit: nil, notes: nil, fixed: false) ⇒ Ingredient

Returns a new instance of Ingredient.



7
8
9
10
11
12
13
# File 'lib/cooklang/ingredient.rb', line 7

def initialize(name:, quantity: nil, unit: nil, notes: nil, fixed: false)
  @name = name.to_s.freeze
  @quantity = quantity
  @unit = unit&.to_s&.freeze
  @notes = notes&.to_s&.freeze
  @fixed = fixed
end

Instance Attribute Details

#fixedObject (readonly)

Returns the value of attribute fixed.



5
6
7
# File 'lib/cooklang/ingredient.rb', line 5

def fixed
  @fixed
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/cooklang/ingredient.rb', line 5

def name
  @name
end

#notesObject (readonly)

Returns the value of attribute notes.



5
6
7
# File 'lib/cooklang/ingredient.rb', line 5

def notes
  @notes
end

#quantityObject (readonly)

Returns the value of attribute quantity.



5
6
7
# File 'lib/cooklang/ingredient.rb', line 5

def quantity
  @quantity
end

#unitObject (readonly)

Returns the value of attribute unit.



5
6
7
# File 'lib/cooklang/ingredient.rb', line 5

def unit
  @unit
end

Instance Method Details

#==(other) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/cooklang/ingredient.rb', line 33

def ==(other)
  return false unless other.is_a?(Ingredient)

  name == other.name &&
    quantity == other.quantity &&
    unit == other.unit &&
    notes == other.notes &&
    fixed == other.fixed
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/cooklang/ingredient.rb', line 43

def eql?(other)
  self == other
end

#fixed?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/cooklang/ingredient.rb', line 63

def fixed?
  @fixed
end

#has_notes?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/cooklang/ingredient.rb', line 59

def has_notes?
  !@notes.nil?
end

#has_quantity?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/cooklang/ingredient.rb', line 51

def has_quantity?
  !@quantity.nil?
end

#has_unit?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/cooklang/ingredient.rb', line 55

def has_unit?
  !@unit.nil?
end

#hashObject



47
48
49
# File 'lib/cooklang/ingredient.rb', line 47

def hash
  [name, quantity, unit, notes, fixed].hash
end

#to_hObject



23
24
25
26
27
28
29
30
31
# File 'lib/cooklang/ingredient.rb', line 23

def to_h
  {
    name: @name,
    quantity: @quantity,
    unit: @unit,
    notes: @notes,
    fixed: @fixed
  }.compact
end

#to_sObject



15
16
17
18
19
20
21
# File 'lib/cooklang/ingredient.rb', line 15

def to_s
  result = @name
  result += " #{@quantity}" if @quantity
  result += " #{@unit}" if @unit
  result += " (#{@notes})" if @notes
  result
end