Class: PlatoDSL

Inherits:
Object
  • Object
show all
Defined in:
lib/tddAlimentos/platoDSL.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ PlatoDSL

Returns a new instance of PlatoDSL.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/tddAlimentos/platoDSL.rb', line 5

def initialize (name, &block)

    @name = name
    @description = ""
    @food = []
    @gr = []

    if block_given?
        if block.arity == 1
            yield self
        else
            instance_eval(&block)
        end
    end

    @vct = calc_vct
    @gei = calc_gei
    @terrain = calc_terrain

end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



3
4
5
# File 'lib/tddAlimentos/platoDSL.rb', line 3

def description
  @description
end

#foodObject

Returns the value of attribute food.



3
4
5
# File 'lib/tddAlimentos/platoDSL.rb', line 3

def food
  @food
end

#geiObject

Returns the value of attribute gei.



3
4
5
# File 'lib/tddAlimentos/platoDSL.rb', line 3

def gei
  @gei
end

#grObject

Returns the value of attribute gr.



3
4
5
# File 'lib/tddAlimentos/platoDSL.rb', line 3

def gr
  @gr
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/tddAlimentos/platoDSL.rb', line 3

def name
  @name
end

#terrainObject

Returns the value of attribute terrain.



3
4
5
# File 'lib/tddAlimentos/platoDSL.rb', line 3

def terrain
  @terrain
end

#vctObject

Returns the value of attribute vct.



3
4
5
# File 'lib/tddAlimentos/platoDSL.rb', line 3

def vct
  @vct
end

Instance Method Details

#alimento(options = {}) ⇒ Object



30
31
32
33
# File 'lib/tddAlimentos/platoDSL.rb', line 30

def alimento (options = {})
    @food << options[:valor]
    @gr << options[:gramos]
end

#calc_geiObject

Calcula el gas de efecto invernadero que se produce al crear el plato

Devuelve

GEI del plato



69
70
71
72
73
74
75
76
77
78
# File 'lib/tddAlimentos/platoDSL.rb', line 69

def calc_gei
    datos = @food.collect { |x| x.gei }
    gramos = @gr.collect { |x| x/100.0 }
    result = 0.0

    for i in 0...datos.size
        result += datos[i] * gramos[i]
    end
    result.round(2)
end

#calc_terrainObject

Calcula el terreno consumido por la producción del plato

Devuelve

Valor del terreno



83
84
85
86
87
88
89
90
91
92
# File 'lib/tddAlimentos/platoDSL.rb', line 83

def calc_terrain
    datos = @food.collect { |x| x.terrain }
    gramos = @gr.collect { |x| x/100.0 }
    result = 0.0

    for i in 0...datos.size
        result += datos[i] * gramos[i]
    end
    result
end

#calc_vctObject

Calcula el Valor Calórico Total del plato

Devuelve

Valor Calórico Total del plato



55
56
57
58
59
60
61
62
63
64
# File 'lib/tddAlimentos/platoDSL.rb', line 55

def calc_vct
    alimentos = @food.collect { |x| x.energetic_value }
    gramos = @gr.collect { |x| x/100.0 }
    result = 0.0

    for i in 0...alimentos.size
        result += (gramos[i] * alimentos[i])
    end
    result.round(2)
end

#descripcion(description) ⇒ Object



26
27
28
# File 'lib/tddAlimentos/platoDSL.rb', line 26

def descripcion (description)
    @description = description
end

#to_sObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/tddAlimentos/platoDSL.rb', line 35

def to_s
    str = ""

    str += @name + "\n"
    str += @description + "\n"
    str += "Ingredientes: "
    @food.each_with_index do |ingredient,index|
        str += "#{ingredient.name} (#{@gr[index]} gr.)"
        if index < (@food.size - 1)
            str += ", "
        end
    end
    str += "\nValor calorico total: " + @vct.to_s + " kcal.\n"
    str += "Uso de terreno: " + @terrain.to_s + "\n"
    str += "GEI: " + @gei.to_s
end