Class: ActionMCP::Types::FloatArrayType

Inherits:
ActiveModel::Type::Value
  • Object
show all
Defined in:
lib/action_mcp/types/float_array_type.rb

Overview

Custom ActiveModel type for handling arrays of floating point numbers

Instance Method Summary collapse

Instance Method Details

#cast(value) ⇒ Object



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
# File 'lib/action_mcp/types/float_array_type.rb', line 11

def cast(value)
  return [] if value.nil?
  return value if value.is_a?(Array) && value.all? { |v| v.is_a?(Float) }

  Array(value).map do |v|
    case v
    when Float then v
    when Numeric then v.to_f
    when String
      case v.downcase
      when "infinity", "+infinity"
        Float::INFINITY
      when "-infinity"
        -Float::INFINITY
      when "nan"
        Float::NAN
      else
        begin
          Float(v)
        rescue StandardError
          nil
        end
      end
    end
  end.compact
end

#deserialize(value) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/action_mcp/types/float_array_type.rb', line 42

def deserialize(value)
  return value if value.is_a?(Array)
  return [] if value.nil?

  # Handle JSON deserialization
  if value.is_a?(String)
    begin
      parsed = JSON.parse(value)
      cast(parsed)
    rescue JSON::ParserError
      []
    end
  else
    cast(value)
  end
end

#serialize(value) ⇒ Object



38
39
40
# File 'lib/action_mcp/types/float_array_type.rb', line 38

def serialize(value)
  cast(value)
end

#typeObject



7
8
9
# File 'lib/action_mcp/types/float_array_type.rb', line 7

def type
  :float_array
end