Class: Pricing

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

Overview

this prcing class is the oberser class that triggers the observer functionality

Instance Method Summary collapse

Constructor Details

#initialize(order) ⇒ Pricing

this method initializes the class and adds the observer object to observer class



32
33
34
35
# File 'lib/pricing_observer.rb', line 32

def initialize(order)    
order.add_observer(self)
puts "observer initialized"
end

Instance Method Details

#pricing_by_hitObject

this is the logical pricing method that is triggered by the observer when ever the condition is met to be executed.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/pricing_observer.rb', line 37

def pricing_by_hit
    #order_record = OrderItem.last
    #prod = Product.find(order_record.product_id)
  puts("before product assumption")
  products = Product.all
  puts("after product assumption")
    
   products.each do |prod|
    prod.price = 12
    puts(prod.hit.inspect)
       case prod.hit
       when 1
          prod.offerPrice = prod.offerPrice + (prod.offerPrice/50)
       when 2
          prod.offerPrice = prod.offerPrice - (prod.offerPrice/100)
       when 3
          prod.offerPrice = prod.offerPrice - (prod.offerPrice/95)
       when 4
          prod.offerPrice = prod.offerPrice - (prod.offerPrice/90)
       when 5
          prod.offerPrice = prod.offerPrice - (prod.offerPrice/85)
       when 6
          prod.offerPrice = prod.offerPrice - (prod.offerPrice/80)
       when 7
          prod.offerPrice = prod.offerPrice - (prod.offerPrice/75)
       when 8
          prod.offerPrice = prod.offerPrice - (prod.offerPrice/70)
       when 9
          prod.offerPrice = prod.offerPrice - (prod.offerPrice/65)
       when 10
          prod.offerPrice = prod.offerPrice - (prod.offerPrice/60)
       else
          prod.offerPrice = 5
       end
      if(prod.offerPrice < 5)
          prod.offerPrice = 5
      elsif (prod.offerPrice > 12)
          prod.offerPrice = 5
      end
    prod.save
   end
end