Class: Auctify::MinimalBidsLadderType
- Inherits:
-
ActiveRecord::Type::Value
- Object
- ActiveRecord::Type::Value
- Auctify::MinimalBidsLadderType
- Defined in:
- app/types/auctify/minimal_bids_ladder_type.rb
Overview
in ruby/rails we expecting default format as hash with ranges { (0…1_000) => 200, (1_000..) => 500 } but acceppting (in setter) also hash with numbers or strings in DB we store JSON with minimal prices casted to nice strings : { “0” => 200, “1_000” => 500 }
Instance Method Summary collapse
-
#cast(value) ⇒ Object
setter.
-
#deserialize(db_value) ⇒ Object
getter, from db data to ruby raw object.
-
#serialize(value) ⇒ Object
modifier to store in db.
- #type ⇒ Object
Instance Method Details
#cast(value) ⇒ Object
setter
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'app/types/auctify/minimal_bids_ladder_type.rb', line 12 def cast(value) # setter # hash is expected as value value = JSON.parse(value) if value.is_a?(String) if value.blank? {} else case value.keys.first when Range # { (0...1_000) => 200, (1_000..) => 500 } cast_from_ranges(value) when String # { "0" => 200, "1_000" => 500 } cast_from_min_strings(value) when Numeric # { 0 => 200, 1000 => 500 } cast_from_min_numbers(value) else raise "Uncovered ladder key type `#{value.keys.first}`" end end end |
#deserialize(db_value) ⇒ Object
getter, from db data to ruby raw object
31 32 33 34 35 |
# File 'app/types/auctify/minimal_bids_ladder_type.rb', line 31 def deserialize(db_value) # getter, from db data to ruby raw object return {} if db_value.blank? cast(ActiveSupport::JSON.decode(db_value)) end |
#serialize(value) ⇒ Object
modifier to store in db
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'app/types/auctify/minimal_bids_ladder_type.rb', line 37 def serialize(value) # modifier to store in db return nil if value.nil? # so You are able to do `KLASS.where(bid_steps_ladder: nil)` # value should be Hash with ranges as keys result = {} value.each_pair do |k_range, v| min_price_string = ActiveSupport::NumberHelper.number_to_delimited(k_range.first, delimiter: "_") result[min_price_string] = v end ActiveSupport::JSON.encode(result) end |
#type ⇒ Object
8 9 10 |
# File 'app/types/auctify/minimal_bids_ladder_type.rb', line 8 def type :jsonb end |