Class: CassandraObject::Types::TypeHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/cassandra_object/types/type_helper.rb

Class Method Summary collapse

Class Method Details

.guess_type(object) ⇒ Object



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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/cassandra_object/types/type_helper.rb', line 5

def self.guess_type(object)

  if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.4.0')
    case object
      when ::String then
        Cassandra::Types.varchar
      when ::Fixnum then
        Cassandra::Types.int
      when ::Integer then
        Cassandra::Types.int
      when ::Float then
        Cassandra::Types.float
      when ::Bignum then
        Cassandra::Types.varint
      when ::BigDecimal then
        Cassandra::Types.decimal
      when ::TrueClass then
        Cassandra::Types.boolean
      when ::FalseClass then
        Cassandra::Types.boolean
      when ::NilClass then
        Cassandra::Types.bigint
      # when Uuid          then Cassandra::Types.uuid
      # when TimeUuid      then Cassandra::Types.timeuuid
      when ::IPAddr then
        Cassandra::Types.inet
      when ::Time then
        Cassandra::Types.timestamp
      when ::Hash
        pair = object.first
        Types.map(guess_type(pair[0]), guess_type(pair[1]))
      when ::Array then
        Types.list(guess_type(object.first))
      when ::Set then
        Types.set(guess_type(object.first))
      # when Tuple::Strict then Types.tuple(*object.types)
      # when Tuple         then Types.tuple(*object.map {|v| guess_type(v)})
      # when UDT::Strict
      #   Types.udt(object.keyspace, object.name, object.types)
      # when UDT
      #   Types.udt('unknown', 'unknown', object.map {|k, v| [k, guess_type(v)]})
      when Cassandra::CustomData then
        object.class.type
      else
        raise ::ArgumentError, "Unable to guess the type of the argument: #{object.inspect}"
    end
  else

    case object
      when ::String then
        Cassandra::Types.varchar
      when ::Integer then
        Cassandra::Types.int
      when ::Float then
        Cassandra::Types.float
      when ::BigDecimal then
        Cassandra::Types.decimal
      when ::TrueClass then
        Cassandra::Types.boolean
      when ::FalseClass then
        Cassandra::Types.boolean
      when ::NilClass then
        Cassandra::Types.bigint
      # when Uuid          then Cassandra::Types.uuid
      # when TimeUuid      then Cassandra::Types.timeuuid
      when ::IPAddr then
        Cassandra::Types.inet
      when ::Time then
        Cassandra::Types.timestamp
      when ::Hash
        pair = object.first
        Types.map(guess_type(pair[0]), guess_type(pair[1]))
      when ::Array then
        Types.list(guess_type(object.first))
      when ::Set then
        Types.set(guess_type(object.first))
      # when Tuple::Strict then Types.tuple(*object.types)
      # when Tuple         then Types.tuple(*object.map {|v| guess_type(v)})
      # when UDT::Strict
      #   Types.udt(object.keyspace, object.name, object.types)
      # when UDT
      #   Types.udt('unknown', 'unknown', object.map {|k, v| [k, guess_type(v)]})
      when Cassandra::CustomData then
        object.class.type
      else
        raise ::ArgumentError, "Unable to guess the type of the argument: #{object.inspect}"
    end
  end

end