Class: MealSelector::Meal

Inherits:
Object
  • Object
show all
Defined in:
lib/meal_selector/meal.rb

Overview

Note:

check #whole_meal? before looking beyond meal name and id.

Data container for a partial/whole Meal

Author:

  • Anthony Tilelli

Constant Summary collapse

EMPTY_MEALS =

Empty meals are invalid

{ meals: nil }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(meal_hash) ⇒ Meal

Creates a frozen Meal from a single meal_hash.

Parameters:

  • meal_hash (Hash)

    must contain one meal in meal format `{ meals: [meal] }`



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/meal_selector/meal.rb', line 16

def initialize(meal_hash)
  raise 'meal_hash must be a hash' unless meal_hash.is_a?(Hash)
  raise 'Empty Meal provided' if meal_hash == EMPTY_MEALS
  raise 'Incorrect hash for meal object' unless meal_hash[:meals].is_a?(Array)
  raise 'More then one meal provided' unless meal_hash[:meals].count == 1

  pre_meal = meal_hash[:meals][0].dup
  raise 'meal lacks an id' unless pre_meal[:idMeal].is_a?(String)
  raise 'meal_hash lacks a name' unless pre_meal[:strMeal].is_a?(String)

  # minimum meal_data
  @id = pre_meal.delete(:idMeal)
  @name = pre_meal.delete(:strMeal)
  @whole_meal = pre_meal.keys.include?(:strInstructions)
  # Rest of meal data
  whole_meal_init(pre_meal) if whole_meal?
  freeze
end

Instance Attribute Details

#categoryObject (readonly)

Returns the value of attribute category



11
12
13
# File 'lib/meal_selector/meal.rb', line 11

def category
  @category
end

#idObject (readonly)

Returns the value of attribute id



11
12
13
# File 'lib/meal_selector/meal.rb', line 11

def id
  @id
end

#ingredientObject (readonly)

Returns the value of attribute ingredient



11
12
13
# File 'lib/meal_selector/meal.rb', line 11

def ingredient
  @ingredient
end

#instructionsObject (readonly)

Returns the value of attribute instructions



11
12
13
# File 'lib/meal_selector/meal.rb', line 11

def instructions
  @instructions
end

#nameObject (readonly)

Returns the value of attribute name



11
12
13
# File 'lib/meal_selector/meal.rb', line 11

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type



11
12
13
# File 'lib/meal_selector/meal.rb', line 11

def type
  @type
end

#youtubeObject (readonly)

Returns the value of attribute youtube



11
12
13
# File 'lib/meal_selector/meal.rb', line 11

def youtube
  @youtube
end

Class Method Details

.create_from_array(meals_hash) ⇒ Hash

Create new meals from multiple meals and returns hash of Meal objects

Parameters:

  • meals_hash (Hash)

    contain multiple meals in meals format `{ meals: [meal] }`

Returns:

  • (Hash)

    Meals hashed by id `=> Meal` or `{}` for zero meals



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/meal_selector/meal.rb', line 74

def self.create_from_array(meals_hash)
  raise 'meals_hash must me a hash' unless meals_hash.is_a?(Hash)
  return {} if meals_hash == EMPTY_MEALS
  raise 'meals_hash must be an array of meals' unless meals_hash[:meals].is_a?(Array)

  processed = {}
  meals_hash[:meals].each do |meal|
    next if meal[:strMeal].match?(/test/)

    meal_obj = Meal.new(meals: [meal])
    processed[meal_obj.id.to_sym] = meal_obj
  end
  processed
end

Instance Method Details

#==(other) ⇒ Boolean

Compares if two meal objects are equal

Parameters:

Returns:

  • (Boolean)


62
63
64
65
66
67
68
69
# File 'lib/meal_selector/meal.rb', line 62

def ==(other)
  return false unless other.is_a?(Meal)
  return false if other.whole_meal? != whole_meal?

  a = to_meal_hash
  b = other.to_meal_hash
  a == b
end

#to_meal_hashHash

Turns object back into a meal found in the `meals: array`. Usefull for saving to a file.

Returns:

  • (Hash)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/meal_selector/meal.rb', line 43

def to_meal_hash
  if whole_meal?
    {
      idMeal: @id,
      strMeal: @name,
      strCategory: @category,
      strInstructions: @instructions,
      strTags: @type,
      strYoutube: @youtube,
      sync_ingredients: @ingredient
    }
  else
    { idMeal: @id, strMeal: @name }
  end
end

#whole_meal?Boolean

Does meal object contain enough infor for show meal?

Returns:

  • (Boolean)

    if meal contains more then :id and :name.



37
38
39
# File 'lib/meal_selector/meal.rb', line 37

def whole_meal?
  @whole_meal
end