Class: PriceType

Inherits:
Object
  • Object
show all
Includes:
EnumField::DefineEnum
Defined in:
app/models/enums/price_type.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, range) ⇒ PriceType

Returns a new instance of PriceType.



8
9
10
11
# File 'app/models/enums/price_type.rb', line 8

def initialize(code, range)
  @code = code.to_sym
  @range = range
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



6
7
8
# File 'app/models/enums/price_type.rb', line 6

def code
  @code
end

#rangeObject (readonly)

Returns the value of attribute range.



6
7
8
# File 'app/models/enums/price_type.rb', line 6

def range
  @range
end

Instance Method Details

#calc(price, options = {}) ⇒ Object



17
18
19
# File 'app/models/enums/price_type.rb', line 17

def calc(price, options = {})
  send("calc_#{code}", price, options)
end

#calc_percentage(price, options) ⇒ Object



25
26
27
# File 'app/models/enums/price_type.rb', line 25

def calc_percentage(price, options)
  (options[:base].to_f * price.to_f) / 100.0
end

#calc_percentage_min_amount(price, options) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'app/models/enums/price_type.rb', line 29

def calc_percentage_min_amount(price, options)
  checkpoint = options[:checkpoint_amount]
  checkpoint ||= (options[:min_amount] * 100.0 / price.to_f)

  if checkpoint >= options[:base].to_f
    options[:min_amount]
  else
    calc_percentage(price, options)
  end
end

#calc_static(price, _options) ⇒ Object



21
22
23
# File 'app/models/enums/price_type.rb', line 21

def calc_static(price, _options)
  price
end

#format(value) ⇒ Object



44
45
46
47
48
49
50
51
# File 'app/models/enums/price_type.rb', line 44

def format(value)
  price = value.to_f.round(2)

  case @code
  when :static then "$#{price}"
  when :percentage, :percentage_min_amount then "#{price}%"
  end
end

#percentage?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'app/models/enums/price_type.rb', line 40

def percentage?
  [:percentage, :percentage_min_amount].include?(code)
end

#titleObject



13
14
15
# File 'app/models/enums/price_type.rb', line 13

def title
  I18n.t(code, scope: [:price_type])
end