File: 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 (48 lines) | stat: -rw-r--r-- 1,187 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
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
module Neighbor
  class Attribute < ActiveRecord::Type::Value
    delegate :type, :serialize, :deserialize, :cast, to: :new_cast_type

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

    private

    def cast_value(...)
      new_cast_type.send(:cast_value, ...)
    end

    def new_cast_type
      @new_cast_type ||= begin
        if @cast_type.is_a?(ActiveModel::Type::Value)
          case Utils.adapter(@model)
          when :sqlite
            case @type&.to_sym
            when :int8
              Type::SqliteInt8Vector.new
            when :bit
              @cast_type
            when :float32, nil
              Type::SqliteVector.new
            else
              raise ArgumentError, "Unsupported type"
            end
          when :mariadb
            if @model.columns_hash[@attribute_name.to_s]&.type == :integer
              @cast_type
            else
              Type::MysqlVector.new
            end
          else
            @cast_type
          end
        else
          @cast_type
        end
      end
    end
  end
end