Class: SQLiteTypes::Array
- Inherits:
-
ActiveRecord::Type::Json
- Object
- ActiveRecord::Type::Json
- SQLiteTypes::Array
- Defined in:
- lib/sqlite_types/array.rb
Constant Summary collapse
- SUPPORTED_SUBTYPES =
%i[integer string text hash json jsonb datetime].freeze
- POSTGRESQL_TIMESTAMP_JSON_FORMAT =
"%Y-%m-%dT%H:%M:%S.%6N"
Instance Method Summary collapse
- #deserialize(value) ⇒ Object
-
#force_equality?(value) ⇒ Boolean
Use “=” instead of “IN” in WHERE clause, to match PostgreSQL arrays.
-
#initialize(subtype, nested: false) ⇒ Array
constructor
A new instance of Array.
- #serialize(value) ⇒ Object
Constructor Details
#initialize(subtype, nested: false) ⇒ Array
Returns a new instance of Array.
10 11 12 13 14 15 16 17 |
# File 'lib/sqlite_types/array.rb', line 10 def initialize(subtype, nested: false) subtype = subtype.to_sym if subtype.respond_to?(:to_sym) raise ArgumentError, "Unsupported subtype: #{subtype}" unless SUPPORTED_SUBTYPES.include?(subtype) @subtype = subtype @nested = nested super() end |
Instance Method Details
#deserialize(value) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/sqlite_types/array.rb', line 19 def deserialize(value) return if value.nil? array = parse_array(value) if @nested array.map do |nested_array| parse_array(nested_array, nested: true).map { |element| cast_element(element) } end else array.map { |element| cast_element(element) } end end |
#force_equality?(value) ⇒ Boolean
Use “=” instead of “IN” in WHERE clause, to match PostgreSQL arrays
44 45 46 |
# File 'lib/sqlite_types/array.rb', line 44 def force_equality?(value) value.is_a?(::Array) end |
#serialize(value) ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/sqlite_types/array.rb', line 33 def serialize(value) return super if value.nil? # Normalize through the same cast path as reads so persisted values and equality queries # use one canonical JSON representation for the declared subtype. array = deserialize(value) array = serialize_datetime_array(array) if @subtype == :datetime super(array) end |