Class: PriceType
- Inherits:
-
Object
- Object
- PriceType
- Includes:
- EnumField::DefineEnum
- Defined in:
- app/models/enums/price_type.rb
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#range ⇒ Object
readonly
Returns the value of attribute range.
Instance Method Summary collapse
- #calc(price, options = {}) ⇒ Object
- #calc_percentage(price, options) ⇒ Object
- #calc_percentage_min_amount(price, options) ⇒ Object
- #calc_static(price, _options) ⇒ Object
- #format(value) ⇒ Object
-
#initialize(code, range) ⇒ PriceType
constructor
A new instance of PriceType.
- #percentage? ⇒ Boolean
- #title ⇒ Object
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
#code ⇒ Object (readonly)
Returns the value of attribute code.
6 7 8 |
# File 'app/models/enums/price_type.rb', line 6 def code @code end |
#range ⇒ Object (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, = {}) send("calc_#{code}", price, ) end |
#calc_percentage(price, options) ⇒ Object
25 26 27 |
# File 'app/models/enums/price_type.rb', line 25 def calc_percentage(price, ) ([: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, ) checkpoint = [:checkpoint_amount] checkpoint ||= ([:min_amount] * 100.0 / price.to_f) if checkpoint >= [:base].to_f [:min_amount] else calc_percentage(price, ) end end |
#calc_static(price, _options) ⇒ Object
21 22 23 |
# File 'app/models/enums/price_type.rb', line 21 def calc_static(price, ) 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
40 41 42 |
# File 'app/models/enums/price_type.rb', line 40 def percentage? [:percentage, :percentage_min_amount].include?(code) end |
#title ⇒ Object
13 14 15 |
# File 'app/models/enums/price_type.rb', line 13 def title I18n.t(code, scope: [:price_type]) end |