Skip to content

Commit

Permalink
Improved type code
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed May 3, 2024
1 parent 5f0720b commit 3f29faf
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
20 changes: 11 additions & 9 deletions lib/neighbor/type/cube.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,24 @@ def type
def cast(value)
if value.is_a?(Array)
if value.first.is_a?(Array)
value.map { |v| cast_point(v) }.join(", ")
value = value.map { |v| cast_point(v) }.join(", ")
else
cast_point(value)
value = cast_point(value)
end
else
super
end
super(value)
end

def deserialize(value)
if value.nil?
super
elsif value.include?("),(")
value[1..-1].split("),(").map { |v| v.split(",").map(&:to_f) }
value = super
unless value.nil?
if value.include?("),(")
value[1..-1].split("),(").map { |v| v.split(",").map(&:to_f) }
else
value[1..-1].split(",").map(&:to_f)
end
else
value[1..-1].split(",").map(&:to_f)
value
end
end

Expand Down
6 changes: 3 additions & 3 deletions lib/neighbor/type/halfvec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ def type

def cast(value)
if value.is_a?(Array)
"[#{value.join(",")}]"
else
super
value = "[#{value.join(",")}]"
end
super(value)
end

def deserialize(value)
value = super
value[1..-1].split(",").map(&:to_f) unless value.nil?
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/neighbor/type/sparsevec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ def type
:sparsevec
end

# TODO improve
def deserialize(value)
# TODO improve
value unless value.nil?
super
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/neighbor/type/vector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ def type

def cast(value)
if value.is_a?(Array)
"[#{value.join(",")}]"
else
super
value = "[#{value.join(",")}]"
end
super(value)
end

def deserialize(value)
value = super
value[1..-1].split(",").map(&:to_f) unless value.nil?
end
end
Expand Down

0 comments on commit 3f29faf

Please sign in to comment.