Class: Cassandra::Types::UserDefined
- Inherits:
-
Cassandra::Type
- Object
- Cassandra::Type
- Cassandra::Types::UserDefined
- Defined in:
- lib/cassandra/types.rb
Defined Under Namespace
Classes: Field
Instance Attribute Summary collapse
-
#keyspace ⇒ String
readonly
Keyspace where this type is defined.
-
#name ⇒ String
readonly
Name of this type.
Instance Method Summary collapse
-
#assert(value, message = nil, &block) ⇒ void
Asserts that a given value is an Cassandra::UDT.
-
#each_field(&block) ⇒ Object
(also: #fields)
Yield or enumerate each field defined in this type.
- #eql?(other) ⇒ Boolean (also: #==)
-
#field(name) ⇒ Cassandra::UserDefined::Field?
A field with this name or nil.
-
#has_field?(name) ⇒ Boolean
Whether this type has a given field.
-
#kind ⇒ Symbol
:udt
. -
#new(*value) ⇒ Cassandra::UDT
Coerces the value to Cassandra::UDT.
-
#to_cql ⇒ Object
Output this type in CQL.
-
#to_s ⇒ String
"keyspace.name"
.
Instance Attribute Details
#keyspace ⇒ String (readonly)
Returns keyspace where this type is defined.
1021 1022 1023 |
# File 'lib/cassandra/types.rb', line 1021 def keyspace @keyspace end |
#name ⇒ String (readonly)
Returns name of this type.
1024 1025 1026 |
# File 'lib/cassandra/types.rb', line 1024 def name @name end |
Instance Method Details
#assert(value, message = nil, &block) ⇒ void
This method returns an undefined value.
Asserts that a given value is an Cassandra::UDT
1109 1110 1111 1112 1113 1114 1115 1116 |
# File 'lib/cassandra/types.rb', line 1109 def assert(value, = nil, &block) Util.assert_instance_of(Cassandra::UDT, value, , &block) Util.assert(value.size <= @fields.size, , &block) @fields.each do |field| Util.assert_type(field.type, value[field.name], , &block) end nil end |
#each_field {|field| ... } ⇒ Cassandra::Types::UserDefined #each_field ⇒ Array<Array<String, Cassandra::Type>> Also known as: fields
Yield or enumerate each field defined in this type
1048 1049 1050 1051 1052 1053 1054 1055 |
# File 'lib/cassandra/types.rb', line 1048 def each_field(&block) if block_given? @fields.each(&block) self else @fields.dup end end |
#eql?(other) ⇒ Boolean Also known as: ==
1124 1125 1126 1127 1128 1129 |
# File 'lib/cassandra/types.rb', line 1124 def eql?(other) other.is_a?(UserDefined) && @keyspace == other.keyspace && @name == other.name && @fields == other.fields end |
#field(name) ⇒ Cassandra::UserDefined::Field?
Returns a field with this name or nil.
1061 1062 1063 |
# File 'lib/cassandra/types.rb', line 1061 def field(name) @fields.find {|f| f.name == name} end |
#has_field?(name) ⇒ Boolean
Returns whether this type has a given field.
1038 1039 1040 |
# File 'lib/cassandra/types.rb', line 1038 def has_field?(name) @fields.any? {|f| f.name == name} end |
#kind ⇒ Symbol
Returns :udt
.
1067 1068 1069 |
# File 'lib/cassandra/types.rb', line 1067 def kind :udt end |
#new(*value) ⇒ Cassandra::UDT
Coerces the value to Cassandra::UDT
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 |
# File 'lib/cassandra/types.rb', line 1075 def new(*value) value = value.first if value.one? value = Array(value) unless value.is_a?(::Hash) Util.assert(value.size <= @fields.size) { "too many values: #{value.size} out of #{@fields.size}" } case value when ::Array result = ::Hash.new value.each_with_index do |v, i| f = @fields[i] Util.assert_type(f.type, v) result[f.name] = v end when ::Hash result = ::Hash.new @fields.each do |f| n = f.name v = value[n] Util.assert_type(f.type, v) result[n] = v end end Cassandra::UDT::Strict.new(@keyspace, @name, @fields, result) end |
#to_cql ⇒ Object
Output this type in CQL
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 |
# File 'lib/cassandra/types.rb', line 1133 def to_cql cql = "CREATE TYPE #{Util.escape_name(@keyspace)}.#{Util.escape_name(@name)} (\n" first = true @fields.each do |field| if first first = false else cql << ",\n" unless first end cql << " #{field.name} #{type_to_cql(field.type)}" end cql << "\n);" cql end |
#to_s ⇒ String
Returns "keyspace.name"
.
1120 1121 1122 |
# File 'lib/cassandra/types.rb', line 1120 def to_s "#{Util.escape_name(@keyspace)}.#{Util.escape_name(@name)} {#{@fields.join(', ')}}" end |