class Thrift::Union
Public Class Methods
field_accessor(klass, field_info)
click to toggle source
# File lib/thrift/union.rb 101 def self.field_accessor(klass, field_info) 102 klass.send :define_method, field_info[:name] do 103 if field_info[:name].to_sym == @setfield 104 @value 105 else 106 raise RuntimeError, "#{field_info[:name]} is not union's set field." 107 end 108 end 109 110 klass.send :define_method, "#{field_info[:name]}=" do |value| 111 Thrift.check_type(value, field_info, field_info[:name]) if Thrift.type_checking 112 @setfield = field_info[:name].to_sym 113 @value = value 114 end 115 end
generate_accessors(klass)
click to toggle source
# File lib/thrift/union.rb 123 def self.generate_accessors(klass) 124 klass::FIELDS.values.each do |field_info| 125 field_accessor(klass, field_info) 126 qmark_isset_method(klass, field_info) 127 end 128 end
new(name=nil, value=nil)
click to toggle source
# File lib/thrift/union.rb 22 def initialize(name=nil, value=nil) 23 if name 24 if name.is_a? Hash 25 if name.size > 1 26 raise "#{self.class} cannot be instantiated with more than one field!" 27 end 28 29 name, value = name.keys.first, name.values.first 30 end 31 32 if Thrift.type_checking 33 raise Exception, "#{self.class} does not contain a field named #{name}!" unless name_to_id(name.to_s) 34 end 35 36 if value.nil? 37 raise Exception, "Union #{self.class} cannot be instantiated with setfield and nil value!" 38 end 39 40 Thrift.check_type(value, struct_fields[name_to_id(name.to_s)], name) if Thrift.type_checking 41 elsif !value.nil? 42 raise Exception, "Value provided, but no name!" 43 end 44 @setfield = name 45 @value = value 46 end
qmark_isset_method(klass, field_info)
click to toggle source
# File lib/thrift/union.rb 117 def self.qmark_isset_method(klass, field_info) 118 klass.send :define_method, "#{field_info[:name]}?" do 119 get_set_field == field_info[:name].to_sym && !get_value.nil? 120 end 121 end
Public Instance Methods
<=>(other)
click to toggle source
# File lib/thrift/union.rb 142 def <=>(other) 143 if self.class == other.class 144 if get_set_field == other.get_set_field 145 if get_set_field.nil? 146 0 147 else 148 get_value <=> other.get_value 149 end 150 else 151 if get_set_field && other.get_set_field.nil? 152 -1 153 elsif get_set_field.nil? && other.get_set_field 154 1 155 elsif get_set_field.nil? && other.get_set_field.nil? 156 0 157 else 158 name_to_id(get_set_field.to_s) <=> name_to_id(other.get_set_field.to_s) 159 end 160 end 161 else 162 self.class <=> other.class 163 end 164 end
==(other)
click to toggle source
# File lib/thrift/union.rb 89 def ==(other) 90 other != nil && @setfield == other.get_set_field && @value == other.get_value 91 end
eql?(other)
click to toggle source
# File lib/thrift/union.rb 93 def eql?(other) 94 self.class == other.class && self == other 95 end
get_set_field()
click to toggle source
get the symbol that indicates what the currently set field type is.
# File lib/thrift/union.rb 131 def get_set_field 132 @setfield 133 end
get_value()
click to toggle source
get the current value of this union, regardless of what the set field is. generally, you should only use this method when you don't know in advance what field to expect.
# File lib/thrift/union.rb 138 def get_value 139 @value 140 end
hash()
click to toggle source
# File lib/thrift/union.rb 97 def hash 98 [self.class.name, @setfield, @value].hash 99 end
inspect()
click to toggle source
# File lib/thrift/union.rb 48 def inspect 49 if get_set_field 50 "<#{self.class} #{@setfield}: #{inspect_field(@value, struct_fields[name_to_id(@setfield.to_s)])}>" 51 else 52 "<#{self.class} >" 53 end 54 end
read(iprot)
click to toggle source
# File lib/thrift/union.rb 56 def read(iprot) 57 iprot.read_struct_begin 58 fname, ftype, fid = iprot.read_field_begin 59 handle_message(iprot, fid, ftype) 60 iprot.read_field_end 61 62 fname, ftype, fid = iprot.read_field_begin 63 raise "Too many fields for union" unless (ftype == Types::STOP) 64 65 iprot.read_struct_end 66 validate 67 end
write(oprot)
click to toggle source
# File lib/thrift/union.rb 69 def write(oprot) 70 validate 71 oprot.write_struct_begin(self.class.name) 72 73 fid = self.name_to_id(@setfield.to_s) 74 75 field_info = struct_fields[fid] 76 type = field_info[:type] 77 if is_container? type 78 oprot.write_field_begin(@setfield, type, fid) 79 write_container(oprot, @value, field_info) 80 oprot.write_field_end 81 else 82 oprot.write_field(@setfield, type, fid, @value) 83 end 84 85 oprot.write_field_stop 86 oprot.write_struct_end 87 end
Protected Instance Methods
handle_message(iprot, fid, ftype)
click to toggle source
# File lib/thrift/union.rb 168 def handle_message(iprot, fid, ftype) 169 field = struct_fields[fid] 170 if field and field[:type] == ftype 171 @value = read_field(iprot, field) 172 name = field[:name].to_sym 173 @setfield = name 174 else 175 iprot.skip(ftype) 176 end 177 end