File: normalized_attribute.rb

package info (click to toggle)
ruby-neighbor 0.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 204 kB
  • sloc: ruby: 840; makefile: 4
file content (21 lines) | stat: -rw-r--r-- 521 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
module Neighbor
  class NormalizedAttribute < ActiveRecord::Type::Value
    delegate :type, :serialize, :deserialize, to: :@cast_type

    def initialize(cast_type:, model:, attribute_name:)
      @cast_type = cast_type
      @model = model
      @attribute_name = attribute_name.to_s
    end

    def cast(...)
      Neighbor::Utils.normalize(@cast_type.cast(...), column_info: @model.columns_hash[@attribute_name])
    end

    private

    def cast_value(...)
      @cast_type.send(:cast_value, ...)
    end
  end
end