pract 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,112 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>
7
+ Top Level Namespace
8
+
9
+ &mdash; Documentation by YARD 0.9.9
10
+
11
+ </title>
12
+
13
+ <link rel="stylesheet" href="css/style.css" type="text/css" charset="utf-8" />
14
+
15
+ <link rel="stylesheet" href="css/common.css" type="text/css" charset="utf-8" />
16
+
17
+ <script type="text/javascript" charset="utf-8">
18
+ pathId = "";
19
+ relpath = '';
20
+ </script>
21
+
22
+
23
+ <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
24
+
25
+ <script type="text/javascript" charset="utf-8" src="js/app.js"></script>
26
+
27
+
28
+ </head>
29
+ <body>
30
+ <div class="nav_wrap">
31
+ <iframe id="nav" src="class_list.html?1"></iframe>
32
+ <div id="resizer"></div>
33
+ </div>
34
+
35
+ <div id="main" tabindex="-1">
36
+ <div id="header">
37
+ <div id="menu">
38
+
39
+ <a href="_index.html">Index</a> &raquo;
40
+
41
+
42
+ <span class="title">Top Level Namespace</span>
43
+
44
+ </div>
45
+
46
+ <div id="search">
47
+
48
+ <a class="full_list_link" id="class_list_link"
49
+ href="class_list.html">
50
+
51
+ <svg width="24" height="24">
52
+ <rect x="0" y="4" width="24" height="4" rx="1" ry="1"></rect>
53
+ <rect x="0" y="12" width="24" height="4" rx="1" ry="1"></rect>
54
+ <rect x="0" y="20" width="24" height="4" rx="1" ry="1"></rect>
55
+ </svg>
56
+ </a>
57
+
58
+ </div>
59
+ <div class="clear"></div>
60
+ </div>
61
+
62
+ <div id="content"><h1>Top Level Namespace
63
+
64
+
65
+
66
+ </h1>
67
+ <div class="box_info">
68
+
69
+
70
+
71
+
72
+
73
+
74
+
75
+
76
+
77
+
78
+
79
+ </div>
80
+
81
+ <h2>Defined Under Namespace</h2>
82
+ <p class="children">
83
+
84
+
85
+ <strong class="modules">Modules:</strong> <span class='object_link'><a href="Pract.html" title="Pract (module)">Pract</a></span>
86
+
87
+
88
+
89
+ <strong class="classes">Classes:</strong> <span class='object_link'><a href="Alimento.html" title="Alimento (class)">Alimento</a></span>, <span class='object_link'><a href="Grupo.html" title="Grupo (class)">Grupo</a></span>, <span class='object_link'><a href="Lista.html" title="Lista (class)">Lista</a></span>, <span class='object_link'><a href="Nodo.html" title="Nodo (class)">Nodo</a></span>
90
+
91
+
92
+ </p>
93
+
94
+
95
+
96
+
97
+
98
+
99
+
100
+
101
+
102
+ </div>
103
+
104
+ <div id="footer">
105
+ Generated on Wed Nov 15 15:04:50 2017 by
106
+ <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
107
+ 0.9.9 (ruby-2.3.5).
108
+ </div>
109
+
110
+ </div>
111
+ </body>
112
+ </html>
data/lib/pract.rb ADDED
@@ -0,0 +1,62 @@
1
+ require "./lib/pract/sort.rb"
2
+
3
+ class Alimento
4
+ include Comparable
5
+ attr_accessor :name, :protein, :glucid, :lipid, :glucose
6
+
7
+ #Constructor
8
+ #@ param name of food [String] and the attributes: protein, glucid, lipid
9
+ def initialize (name, protein, glucid, lipid, glucose)
10
+ @name = name
11
+ @protein = protein
12
+ @glucid = glucid
13
+ @lipid = lipid
14
+ @glucose = glucose
15
+ end
16
+ #@return the energy provided by proteins
17
+ def protein_energy
18
+ return @protein * 4
19
+ end
20
+ #@return the energy provided by glucids
21
+ def glucid_energy
22
+ return @glucid * 4
23
+ end
24
+ #@return the energy provided by lipids
25
+ def lipid_energy
26
+ return @lipid * 9
27
+ end
28
+ #@return the total energy of food
29
+ def energy
30
+ return glucid_energy + protein_energy + lipid_energy
31
+ end
32
+ #@return the format showed by puts
33
+ def to_s
34
+ return "#{@name}: #{@protein}g proteinas, #{@glucid}g glucidos, #{@lipid}g grasas"
35
+ end
36
+ #@return only name and total energy
37
+ def show_energy
38
+ return "#{@name}: #{self.energy.round(2)} Kcal."
39
+ end
40
+ #Make it comparable
41
+ def <=> (other)
42
+ return self.energy <=> other.energy
43
+ end
44
+ #Define also the function == in comparable
45
+ def == (other)
46
+ return self.energy == other.energy
47
+ end
48
+
49
+ def aibc
50
+ aux = @glucose.each_with_index.map do # i es el value , m es el indice
51
+ |i, m| @glucose[m].each_with_index.map do
52
+ |i, n|
53
+ if(n>0)
54
+ i-@glucose[m][0] + (@glucose[m][n-1]-@glucose[m][0])
55
+ else
56
+ 0
57
+ end
58
+ end.each.collect{|i| i.round(2)/2*5}.reduce('+')
59
+ end
60
+ return aux
61
+ end
62
+ end
@@ -0,0 +1,72 @@
1
+ require "./lib/pract.rb"
2
+ require "./lib/pract/list.rb"
3
+
4
+ class Grupo < Alimento
5
+ #Constructor
6
+ #@param group_name as the name of the list
7
+ #@val as the first element of the list
8
+ def initialize (group_name)
9
+ super(group_name, 0, 0, 0, [])
10
+ @lista = Lista.new
11
+ end
12
+ #String format
13
+ def to_s
14
+ elements = @name + ":"
15
+ elements << @lista.to_s
16
+ #point = @lista.head
17
+ #while(@lista.tail != point)
18
+ # elements = elements + point.val.name + ", "
19
+ # point = point.next
20
+ #end
21
+ #elements = elements + @lista.tail.val.name + " ]"
22
+ end
23
+ #Insert val to the last
24
+ def insert (val)
25
+ @lista.add_last(val)
26
+ end
27
+ #Remove an element called val
28
+ def delete (val)
29
+ @lista.remove(val)
30
+ end
31
+ #@return whether val is in the list or not
32
+ def has_a(val)
33
+ if(@lista.search(val) != 0)
34
+ return true
35
+ else
36
+ return false
37
+ end
38
+ end
39
+ #@return the energy provided by proteins of all foods in the list
40
+ def protein_energy
41
+ @protein = 0
42
+ point = @lista.head
43
+ while(@lista.tail != point)
44
+ @protein = @protein + point.val.protein_energy
45
+ point = point.next
46
+ end
47
+ @protein = @protein + @lista.tail.val.protein_energy
48
+ return @protein
49
+ end
50
+ #@return the energy provided by glucids of all foods in the list
51
+ def glucid_energy
52
+ @glucid = 0
53
+ point = @lista.head
54
+ while(@lista.tail != point)
55
+ @glucid = @glucid + point.val.glucid_energy
56
+ point = point.next
57
+ end
58
+ @glucid = @glucid + @lista.tail.val.glucid_energy
59
+ return @glucid
60
+ end
61
+ #@return the energy provided by lipids of all foods in the list
62
+ def lipid_energy
63
+ @lipid = 0
64
+ point = @lista.head
65
+ while(@lista.tail != point)
66
+ @lipid = @lipid + point.val.lipid_energy
67
+ point = point.next
68
+ end
69
+ @lipid = @lipid + @lista.tail.val.lipid_energy
70
+ return @lipid
71
+ end
72
+ end
data/lib/pract/list.rb ADDED
@@ -0,0 +1,122 @@
1
+ require "./lib/pract/nodo.rb"
2
+
3
+ class Lista
4
+ include Enumerable
5
+ attr_accessor :head, :tail
6
+
7
+ #Constructor
8
+ #@param val as first element
9
+ def initialize
10
+ @head = @tail = nil
11
+ end
12
+ #@param point as auxiliar pointer
13
+ #@param elements as showing variable
14
+ #@return string format
15
+ def to_s
16
+ elements = "["
17
+ point = @head
18
+ while point != @tail
19
+ elements = elements + point.val.to_s
20
+ elements = elements + "], ["
21
+ point = point.next
22
+ end
23
+ if (@head != nil)
24
+ elements = elements + point.val.to_s
25
+ end
26
+ elements = elements + "]"
27
+ end
28
+ #Add val as last element
29
+ def add_last (val)
30
+ if (@head!= nil)
31
+ point = @tail
32
+ point.next = Nodo.new(point, val, nil)
33
+ @tail = point.next
34
+ else
35
+ @head = @tail = Nodo.new(nil, val, nil)
36
+ end
37
+ end
38
+ #Add val as first element
39
+ def add_first (val)
40
+ if (@head!= nil)
41
+ point = @head
42
+ point.prev = Nodo.new(nil, val, point)
43
+ @head = point.prev
44
+ else
45
+ @head = @tail = Nodo.new(nil, val, nil)
46
+ end
47
+ end
48
+ #Remove the first element
49
+ def remove_first
50
+ point = @head
51
+ @head = point.next
52
+ end
53
+ #Remove the last element
54
+ def remove_last
55
+ point = @tail
56
+ @tail = point.prev
57
+ end
58
+ #Remove an element
59
+ #@param val [value] as the value we want to remove
60
+ def remove (val)
61
+ if (@head != nil)
62
+ if(@head == @tail) && (@head.val == val)
63
+ @head = nil
64
+ @tail = nil
65
+ elsif (@head.val == val)
66
+ remove_first
67
+ elsif (@tail.val == val)
68
+ remove_last
69
+ else
70
+ point = @head
71
+ while (point.next != nil) && (point.val != val)
72
+ point = point.next
73
+ end
74
+ if point != @tail
75
+ before = point.prev
76
+ after = point.next
77
+ before.next = after
78
+ after.prev = before
79
+ point.prev = nil
80
+ point.next = nil
81
+ end
82
+ end
83
+ end
84
+ end
85
+ #@return size of element
86
+ def sz
87
+ size_list = 0
88
+ point = @head
89
+ while(point != tail)
90
+ size_list = size_list + 1
91
+ point = point.next
92
+ end
93
+ size_list = size_list+1
94
+ return size_list
95
+ end
96
+ #@return position of val[from 1] or nil is not found
97
+ def search(val)
98
+ num = 1
99
+ point = @head
100
+ while(point != @tail)
101
+ if(point.val == val)
102
+ return num
103
+ else
104
+ num = num+1
105
+ point = point.next
106
+ end
107
+ end
108
+ if @tail.val == val
109
+ return num
110
+ else return 0
111
+ end
112
+ end
113
+ #Make list enumerable
114
+ def each
115
+ point = @head
116
+ while(point != @tail) do
117
+ yield point.val
118
+ point = point.next
119
+ end
120
+ yield(point.val)
121
+ end
122
+ end
data/lib/pract/nodo.rb ADDED
@@ -0,0 +1,2 @@
1
+ #Create nodo as auxiliar class
2
+ Nodo = Struct.new(:prev, :val, :next)
@@ -0,0 +1,102 @@
1
+ require "./lib/pract/grupo.rb"
2
+ require "./lib/pract.rb"
3
+
4
+ class Plate < Grupo
5
+ attr_accessor :name, :plates
6
+
7
+ def initialize(name, &block)
8
+ super(name)
9
+ @@plates = []
10
+ @@amount = []
11
+ @@type = []
12
+
13
+ if block_given?
14
+ if block.arity == 1
15
+ yield self
16
+ else
17
+ instance_eval(&block)
18
+ end
19
+ end
20
+ end
21
+
22
+ def new_plate (val, options = {})
23
+
24
+ ingredient = val.name
25
+ ingredient << " (#{options[:amount]})" if options[:amount]
26
+
27
+
28
+ @@plates << ingredient
29
+
30
+ @lista.add_last(val)
31
+ self.calculate
32
+ return val
33
+ end
34
+
35
+ def vegetal (val, options = {})
36
+ @@type << 0
37
+ new_plate(val, options)
38
+ end
39
+
40
+ def fruta (val, options = {})
41
+ @@type << 1
42
+ new_plate(val, options)
43
+ end
44
+
45
+ def cereal (val, options = {})
46
+ @@type << 2
47
+ new_plate(val, options)
48
+ end
49
+
50
+ def proteina (val, options = {})
51
+ @@type << 3
52
+ new_plate(val, options)
53
+ end
54
+
55
+ def aceite (val, options = {})
56
+ @@type << 4
57
+ new_plate(val, options)
58
+ end
59
+
60
+ def to_s_other
61
+ elements = ""
62
+ elements << @name + ": "
63
+ @@plates.each do
64
+ |i| elements << i.to_s + " "#+ @plates[j][0]
65
+ end
66
+ return elements
67
+ end
68
+
69
+ def formatted_to_s
70
+ total = 0
71
+ string = @name
72
+ string << "\n#{'=' * @name.size}\n"
73
+ string << "Composicion nutricional:\n\n"
74
+ @lista.each_with_index do
75
+ |i, j|
76
+ string << "#{j + 1}) " + i.name + ": " + ((i.protein_energy* @@amount[j]).round(2)).to_s + "g proteinas, " + ((i.glucid_energy * @@amount[j]).round(2)).to_s + "g glucidos, " + ((i.lipid_energy * @@amount[j]).round(2)).to_s + "g grasas. (" + ((i.energy* @@amount[j]).round(2)).to_s + " g totales)\n"
77
+ total = (total + i.energy* @@amount[j]).round(2)
78
+ end
79
+ string << "\n TOTAL: #{total} g "
80
+ #@instructions.each_with_index do |instruction, index|
81
+ # string << "#{index + 1}) #{instruction}\n"
82
+ # end
83
+
84
+ return string
85
+ end
86
+
87
+ def calculate
88
+ @@plates.each_with_index do
89
+ |i, j|
90
+ elements = []
91
+ elements = i.to_s.tr('()','').split(" ")
92
+
93
+ amount = 0
94
+ amount = elements[1].to_f * 90 / 20 if (elements[2] == 'lata')
95
+ amount = elements[1].to_f * 30 / 20 if (elements[2] == 'pieza')
96
+ amount = elements[1].to_f * 10 / 20 if (elements[2] == 'cucharada')
97
+ amount = elements[1].to_f / 20 if (elements[2] == 'g')
98
+ @@amount[j] = amount;
99
+ end
100
+ return @@amount
101
+ end
102
+ end