Class: Plato

Inherits:
Object
  • Object
show all
Defined in:
lib/P11/plato.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ Plato

Returns a new instance of Plato.



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/P11/plato.rb', line 3

def initialize(name, &block)
    @name = name
    @alimentos = []
    @total = 0
    
    if block_given?  
      if block.arity == 1
        yield self
      else
       instance_eval(&block) 
      end
    end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



2
3
4
# File 'lib/P11/plato.rb', line 2

def name
  @name
end

#totalObject (readonly)

Returns the value of attribute total.



2
3
4
# File 'lib/P11/plato.rb', line 2

def total
  @total
end

Instance Method Details

#aceite(name, amount) ⇒ Object



88
89
90
# File 'lib/P11/plato.rb', line 88

def aceite(name, amount)
    insert_food(name, amount)
end

#cereal(name, amount) ⇒ Object



80
81
82
# File 'lib/P11/plato.rb', line 80

def cereal(name, amount)
    insert_food(name, amount)
end

#convert_quantities(amount) ⇒ Object



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
54
55
56
57
# File 'lib/P11/plato.rb', line 17

def convert_quantities(amount)
  grams = 0
  prop = 1
  
    if amount[:porcion] =~ /taza/i
       amount[:porcion] = amount[:porcion].gsub(/taz(a\s|as\s|a\z|as\z)/, '')
        grams = 3
        else if amount[:porcion] =~ /cucharada/i
                amount[:porcion] = amount[:porcion].gsub(/cucharad(a\s|as\s|a\z|as\z)/, '')
                grams = 0.09
    
            else if amount[:porcion] =~ /cucharon/i
                    amount[:porcion] = amount[:porcion].gsub(/cucharo(n\s|nes\s|n\z|nes\z)/, '')
                    grams = 10
        
                else if amount[:porcion] =~ /pieza/i
                        amount[:porcion] = amount[:porcion].gsub(/piez(a\s|as\s|a\z|as\z)/, '')
                        grams = 1
                end
            end
        end
    end
    
    if amount[:porcion] =~ /pequeñ(a\s|as\s|a\z|as\z|o\s|os\s|o\z|os\z)/i 
       amount[:porcion] = amount[:porcion].gsub(/pequeñ(a\s|as\s|a\z|as\z|o\s|os\s|o\z|os\z)/, '')
       grams = grams.to_f / 2
    end
    # reconoce numero y fraccion amount[:porcion] = amount[:porcion].match(/(\d\/\d)|\d/)
    
    # Si el número es una fracción
    if amount[:porcion].match(/(\d\/\d)/)
        num = amount[:porcion].match(/(\d\/)/).to_s.gsub(/(\/)/, '').to_f
        den = amount[:porcion].match(/(\/\d)/).to_s.gsub(/(\/)/, '').to_f
        prop = num/den
    else
        # Si el numero es entero
        prop = amount[:porcion].match(/\d/).to_s.to_i
    end
    
    grams = grams * prop
end

#fruta(name, amount) ⇒ Object



76
77
78
# File 'lib/P11/plato.rb', line 76

def fruta(name, amount)
    insert_food(name, amount)
end

#insert_food(name, amount) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/P11/plato.rb', line 59

def insert_food(name, amount)
    food = Food[name]

    if amount[:porcion]
        quantity = Food[name].cal_index * convert_quantities(amount)
    else
        quantity = Food[name].cal_index * amount[:gramos]/10
    end

    @alimentos << [food, quantity.round(2)]  
    @total = @alimentos.map{|x| x[1]}.reduce(:+)
end

#proteina(name, amount) ⇒ Object



84
85
86
# File 'lib/P11/plato.rb', line 84

def proteina(name, amount)
    insert_food(name, amount)
end

#to_sObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/P11/plato.rb', line 92

def to_s
    puts @name
    
    i = 0
    while(i < @name.size)
        print "="
        i = i + 1
    end
    puts
    puts "Composición nutricional:"
    puts "                 Glúcidos Proteínas Lípidos Valor energético Valor porcion"
    
    @alimentos.map{|x| print x[0], "%8s" % x[1], "\n"}
    print "Valor energético total: ", "%44s" % @total
     
end

#vegetal(name, amount) ⇒ Object



72
73
74
# File 'lib/P11/plato.rb', line 72

def vegetal(name, amount)
    insert_food(name, amount)
end