Class: FruitInfo::Fruit

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

Constant Summary collapse

@@all =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Fruit

Returns a new instance of Fruit.



8
9
10
11
12
# File 'lib/fruit.rb', line 8

def initialize(attributes)
  attributes.each { |k, v| send("#{k}=", v) }
  add_color
  @@all << self
end

Instance Attribute Details

#colorObject

Returns the value of attribute color.



6
7
8
# File 'lib/fruit.rb', line 6

def color
  @color
end

#familyObject

Returns the value of attribute family.



6
7
8
# File 'lib/fruit.rb', line 6

def family
  @family
end

#genusObject

Returns the value of attribute genus.



6
7
8
# File 'lib/fruit.rb', line 6

def genus
  @genus
end

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/fruit.rb', line 6

def id
  @id
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/fruit.rb', line 6

def name
  @name
end

#nutritionsObject

Returns the value of attribute nutritions.



6
7
8
# File 'lib/fruit.rb', line 6

def nutritions
  @nutritions
end

#orderObject

Returns the value of attribute order.



6
7
8
# File 'lib/fruit.rb', line 6

def order
  @order
end

Class Method Details

.allObject



57
58
59
# File 'lib/fruit.rb', line 57

def self.all
  @@all
end

.max(element) ⇒ Object



14
15
16
# File 'lib/fruit.rb', line 14

def self.max(element)
  all.max { |a, b| a.nutritions[element] <=> b.nutritions[element] }
end

.min(element) ⇒ Object



18
19
20
# File 'lib/fruit.rb', line 18

def self.min(element)
  all.min { |a, b| a.nutritions[element] <=> b.nutritions[element] }
end

.new_from_api(fruits) ⇒ Object



61
62
63
# File 'lib/fruit.rb', line 61

def self.new_from_api(fruits)
  fruits.each { |fruit| new(fruit) }
end

Instance Method Details

#add_colorObject



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
# File 'lib/fruit.rb', line 22

def add_color
  case name
  when 'Apricot'
    self.color = :yellow
  when 'Banana'
    self.color = :light_yellow
  when 'Blueberry'
    self.color = :blue
  when 'Cherry'
    self.color = :red
  when 'Apple'
    self.color = :green
  when 'Lemon'
    self.color = :light_yellow
  when 'Mango'
    self.color = :yellow
  when 'Orange'
    self.color = :yellow
  when 'Pear'
    self.color = :green
  when 'Pineapple'
    self.color = :light_yellow
  when 'Raspberry'
    self.color = :light_red
  when 'Strawberry'
    self.color = :red
  when 'Tomato'
    self.color = :red
  when 'Watermelon'
    self.color = :light_green
  else
    self.color = :white
  end
end