class HarvardDishDSL

Attributes

dish_title[R]
ingredient_quantity_array[R]

Public Class Methods

new(dish_title) { |self| ... } click to toggle source
# File lib/dsl/harvard_dish.rb, line 19
def initialize(dish_title, &block)
  @dish_title = dish_title
  @ingredient_quantity_array = [] # Arrays of pairs ingredient name and quantity
  @number_of_people = 0
  
  if block_given?
    if block.arity == 1
      yield self
    else
      instance_eval(&block)
    end
  end
  
end

Public Instance Methods

aceite(ingredient_name, options = {}) click to toggle source
# File lib/dsl/harvard_dish.rb, line 72
def aceite (ingredient_name, options = {})
  ingredient_quantity = analyze_options(options)
  @ingredient_quantity_array.push([ingredient_name, ingredient_quantity])
end
cereal(ingredient_name, options = {}) click to toggle source
# File lib/dsl/harvard_dish.rb, line 62
def cereal (ingredient_name, options = {})
  ingredient_quantity = analyze_options(options)
  @ingredient_quantity_array.push([ingredient_name, ingredient_quantity])
end
fruta(ingredient_name, options = {}) click to toggle source
# File lib/dsl/harvard_dish.rb, line 57
def fruta (ingredient_name, options = {})
  ingredient_quantity = analyze_options(options)
  @ingredient_quantity_array.push([ingredient_name, ingredient_quantity])
end
personas(number_of_people) click to toggle source
# File lib/dsl/harvard_dish.rb, line 77
def personas (number_of_people)
  @number_of_people = number_of_people
end
proteina(ingredient_name, options = {}) click to toggle source
# File lib/dsl/harvard_dish.rb, line 67
def proteina (ingredient_name, options = {})
  ingredient_quantity = analyze_options(options)
  @ingredient_quantity_array.push([ingredient_name, ingredient_quantity])
end
to_s() click to toggle source
# File lib/dsl/harvard_dish.rb, line 34
def to_s
  rows = []
  total_energetic_content = 0
  
  @ingredient_quantity_array.each { |ingredient_name, ingredient_quantity|
    @food = @@ingredient_database[ingredient_name] * ingredient_quantity
    rows << [@food.name, ingredient_quantity*10, @food.glucid_quantity, @food.protein_quantity, @food.lipid_quantity, @food.energetic_content]
    total_energetic_content += @food.energetic_content
  }
  
  rows << ['Valor Energético Total', '', '', '', '', total_energetic_content]
  
  # Gema para tabla usada -> https://github.com/tj/terminal-table
  return Terminal::Table.new(:title => @dish_title, 
                             :headings => [' ', 'Gramos', 'Glúcidos', 'Proteínas', 'Lípidos', 'Valor Energético'], 
                             :rows => rows)
end
vegetal(ingredient_name, options = {}) click to toggle source
# File lib/dsl/harvard_dish.rb, line 52
def vegetal (ingredient_name, options = {})
  ingredient_quantity = analyze_options(options)
  @ingredient_quantity_array.push([ingredient_name, ingredient_quantity])
end

Private Instance Methods

analyze_options(options) click to toggle source
# File lib/dsl/harvard_dish.rb, line 83
def analyze_options(options)
  quantity = 0
  options.each { |option_name, option_value|
    case option_name
      when :porcion
        quantity = transform_to_grams(option_value)
      else :gramos
        quantity = option_value/10
    end
  }
  return quantity
end
transform_to_grams(portion) click to toggle source

pieza, pieza pequeña, taza, taza pequeña, cucharon, cucharada

# File lib/dsl/harvard_dish.rb, line 97
def transform_to_grams(portion)
  quantity = 0
  
  if (portion =~ /piez/)
    if (portion =~ /pequeña/)
      quantity = SMALL_PIECE_QUANTITY
    else
      quantity = PIECE_QUANTITY
    end
  elsif (portion =~ /taz/)
    if (portion =~ /pequeña/)
      quantity = SMALL_MUG_QUANTITY
    else
      quantity = MUG_QUANTITY
    end
  elsif (portion =~ /cucharon/)
    quantity = SPOON_QUANTITY
  elsif (portion =~ /cuchar/)
    quantity = SMALL_SPOON_QUANTITY
  end
  
  fixed_quantity = (quantity * portion.to_r).to_f / INGREDIENT_DATABASE_GRAMS
        
  return fixed_quantity
end