Class: Cooklang::Ingredient
- Inherits:
-
Object
- Object
- Cooklang::Ingredient
- Defined in:
- lib/cooklang/ingredient.rb
Instance Attribute Summary collapse
-
#fixed ⇒ Object
readonly
Returns the value of attribute fixed.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#notes ⇒ Object
readonly
Returns the value of attribute notes.
-
#quantity ⇒ Object
readonly
Returns the value of attribute quantity.
-
#unit ⇒ Object
readonly
Returns the value of attribute unit.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #eql?(other) ⇒ Boolean
- #fixed? ⇒ Boolean
- #has_notes? ⇒ Boolean
- #has_quantity? ⇒ Boolean
- #has_unit? ⇒ Boolean
- #hash ⇒ Object
-
#initialize(name:, quantity: nil, unit: nil, notes: nil, fixed: false) ⇒ Ingredient
constructor
A new instance of Ingredient.
- #to_h ⇒ Object
- #to_s ⇒ Object
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
#fixed ⇒ Object (readonly)
Returns the value of attribute fixed.
5 6 7 |
# File 'lib/cooklang/ingredient.rb', line 5 def fixed @fixed end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/cooklang/ingredient.rb', line 5 def name @name end |
#notes ⇒ Object (readonly)
Returns the value of attribute notes.
5 6 7 |
# File 'lib/cooklang/ingredient.rb', line 5 def notes @notes end |
#quantity ⇒ Object (readonly)
Returns the value of attribute quantity.
5 6 7 |
# File 'lib/cooklang/ingredient.rb', line 5 def quantity @quantity end |
#unit ⇒ Object (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
43 44 45 |
# File 'lib/cooklang/ingredient.rb', line 43 def eql?(other) self == other end |
#fixed? ⇒ Boolean
63 64 65 |
# File 'lib/cooklang/ingredient.rb', line 63 def fixed? @fixed end |
#has_notes? ⇒ Boolean
59 60 61 |
# File 'lib/cooklang/ingredient.rb', line 59 def has_notes? !@notes.nil? end |
#has_quantity? ⇒ Boolean
51 52 53 |
# File 'lib/cooklang/ingredient.rb', line 51 def has_quantity? !@quantity.nil? end |
#has_unit? ⇒ Boolean
55 56 57 |
# File 'lib/cooklang/ingredient.rb', line 55 def has_unit? !@unit.nil? end |
#hash ⇒ Object
47 48 49 |
# File 'lib/cooklang/ingredient.rb', line 47 def hash [name, quantity, unit, notes, fixed].hash end |
#to_h ⇒ Object
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_s ⇒ Object
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 |