Class: Percent
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
- .demongoize(object) ⇒ Object
- .evolve(object) ⇒ Object
- .mongoize(object) ⇒ Object
- .new_from_mongo(object) ⇒ Object
- .new_from_string(object) ⇒ Object
Instance Method Summary collapse
- #f_percent(options = {}) ⇒ Object
-
#initialize(object) ⇒ Percent
constructor
A new instance of Percent.
- #mongoize ⇒ Object
- #percent ⇒ Object
Constructor Details
#initialize(object) ⇒ Percent
Returns a new instance of Percent.
4 5 6 |
# File 'lib/caisson/implants/mongoid.rb', line 4 def initialize(object) @value = object end |
Instance Attribute Details
#value ⇒ Object (readonly)
Returns the value of attribute value.
2 3 4 |
# File 'lib/caisson/implants/mongoid.rb', line 2 def value @value end |
Class Method Details
.demongoize(object) ⇒ Object
32 33 34 |
# File 'lib/caisson/implants/mongoid.rb', line 32 def demongoize(object) self.new(object.to_f / 1000) end |
.evolve(object) ⇒ Object
36 37 38 |
# File 'lib/caisson/implants/mongoid.rb', line 36 def evolve(object) object.mongoize end |
.mongoize(object) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/caisson/implants/mongoid.rb', line 40 def mongoize(object) case object when Percent then object.mongoize else Percent.new_from_string(object).mongoize end end |
.new_from_mongo(object) ⇒ Object
47 48 49 |
# File 'lib/caisson/implants/mongoid.rb', line 47 def new_from_mongo(object) Numeric(object.to_f / 1000) end |
.new_from_string(object) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/caisson/implants/mongoid.rb', line 51 def new_from_string(object) begin val = (object.to_s.gsub(',', '.').gsub('%', '').to_f * 1000).to_i rescue val = 0 end self.new val end |
Instance Method Details
#f_percent(options = {}) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/caisson/implants/mongoid.rb', line 8 def f_percent(={}) .reverse_merge! decimal_format: ',', symbol: true val = sprintf("%0.03f", @value).gsub('.', [:decimal_format]) val = case when val[-3,3] == '000' then val[0..-5] when val[-1,1] == '0' then val[0..-2] else val end val += '%' if [:symbol] return val end |
#mongoize ⇒ Object
23 24 25 |
# File 'lib/caisson/implants/mongoid.rb', line 23 def mongoize @value.to_s.rjust(10, '0') end |
#percent ⇒ Object
27 28 29 |
# File 'lib/caisson/implants/mongoid.rb', line 27 def percent @value.to_f end |