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.



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

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



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

def self.all
  @@all
end

.max(element) ⇒ Object



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

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

.min(element) ⇒ Object



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

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

.new_from_api(fruits) ⇒ Object



64
65
66
# File 'lib/fruit.rb', line 64

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

Instance Method Details

#add_colorObject



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

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