module Thrift::Struct
Public Class Methods
Source
# File lib/thrift/struct.rb 217 def self.append_features(mod) 218 if mod.ancestors.include? ::Exception 219 mod.send :class_variable_set, :'@@__thrift_struct_real_initialize', mod.instance_method(:initialize) 220 super 221 # set up our custom initializer so `raise Xception, 'message'` works 222 mod.send :define_method, :struct_initialize, mod.instance_method(:initialize) 223 mod.send :define_method, :initialize, mod.instance_method(:exception_initialize) 224 else 225 super 226 end 227 end
Calls superclass method
Source
# File lib/thrift/struct.rb 169 def self.field_accessor(klass, field_info) 170 field_name_sym = field_info[:name].to_sym 171 klass.send :attr_reader, field_name_sym 172 klass.send :define_method, "#{field_info[:name]}=" do |value| 173 Thrift.check_type(value, field_info, field_info[:name]) if Thrift.type_checking 174 instance_variable_set("@#{field_name_sym}", value) 175 end 176 end
Source
# File lib/thrift/struct.rb 178 def self.generate_accessors(klass) 179 klass::FIELDS.values.each do |field_info| 180 field_accessor(klass, field_info) 181 qmark_isset_method(klass, field_info) 182 end 183 end
Source
# File lib/thrift/struct.rb 26 def initialize(d = {}, &block) 27 # get a copy of the default values to work on, removing defaults in favor of arguments 28 fields_with_defaults = fields_with_default_values.dup 29 30 # check if the defaults is empty, or if there are no parameters for this 31 # instantiation, and if so, don't bother overriding defaults. 32 unless fields_with_defaults.empty? || d.empty? 33 d.each_key do |name| 34 fields_with_defaults.delete(name.to_s) 35 end 36 end 37 38 # assign all the user-specified arguments 39 unless d.empty? 40 d.each do |name, value| 41 unless name_to_id(name.to_s) 42 raise Exception, "Unknown key given to #{self.class}.new: #{name}" 43 end 44 Thrift.check_type(value, struct_fields[name_to_id(name.to_s)], name) if Thrift.type_checking 45 instance_variable_set("@#{name}", value) 46 end 47 end 48 49 # assign all the default values 50 unless fields_with_defaults.empty? 51 fields_with_defaults.each do |name, default_value| 52 instance_variable_set("@#{name}", (default_value.dup rescue default_value)) 53 end 54 end 55 56 yield self if block 57 end
Source
# File lib/thrift/struct.rb 185 def self.qmark_isset_method(klass, field_info) 186 klass.send :define_method, "#{field_info[:name]}?" do 187 !send(field_info[:name].to_sym).nil? 188 end 189 end
Public Instance Methods
Source
# File lib/thrift/struct.rb 191 def <=>(other) 192 if self.class == other.class 193 each_field do |fid, field_info| 194 v1 = send(field_info[:name]) 195 v1_set = !v1.nil? 196 v2 = other.send(field_info[:name]) 197 v2_set = !v2.nil? 198 if v1_set && !v2_set 199 return -1 200 elsif !v1_set && v2_set 201 return 1 202 elsif v1_set && v2_set 203 cmp = v1 <=> v2 204 if cmp != 0 205 return cmp 206 end 207 end 208 end 209 0 210 else 211 self.class <=> other.class 212 end 213 end
Source
# File lib/thrift/struct.rb 132 def ==(other) 133 return false unless other.instance_of?(self.class) 134 each_field do |fid, field_info| 135 name = field_info[:name] 136 return false unless other.respond_to?(name) && send(name) == other.send(name) 137 end 138 true 139 end
Source
# File lib/thrift/struct.rb 156 def differences(other) 157 diffs = [] 158 unless other.is_a?(self.class) 159 diffs << "Different class!" 160 else 161 each_field do |fid, field_info| 162 name = field_info[:name] 163 diffs << "#{name} differs!" unless instance_variable_get("@#{name}") == other.instance_variable_get("@#{name}") 164 end 165 end 166 diffs 167 end
Source
# File lib/thrift/struct.rb 141 def eql?(other) 142 self.class == other.class && self == other 143 end
Source
# File lib/thrift/struct.rb 59 def fields_with_default_values 60 fields_with_default_values = self.class.instance_variable_get(:@fields_with_default_values) 61 unless fields_with_default_values 62 fields_with_default_values = {} 63 struct_fields.each do |fid, field_def| 64 unless field_def[:default].nil? 65 fields_with_default_values[field_def[:name]] = field_def[:default] 66 end 67 end 68 self.class.instance_variable_set(:@fields_with_default_values, fields_with_default_values) 69 end 70 fields_with_default_values 71 end
Source
# File lib/thrift/struct.rb 146 def hash 147 total = 17 148 each_field do |fid, field_info| 149 name = field_info[:name] 150 value = send(name) 151 total = ((total * 37) + value.hash) & 0xffffffff 152 end 153 total 154 end
This implementation of hash() is inspired by Apache’s Java HashCodeBuilder class.
Source
# File lib/thrift/struct.rb 73 def inspect(skip_optional_nulls = true) 74 fields = [] 75 each_field do |fid, field_info| 76 name = field_info[:name] 77 value = instance_variable_get("@#{name}") 78 unless skip_optional_nulls && field_info[:optional] && value.nil? 79 fields << "#{name}:#{inspect_field(value, field_info)}" 80 end 81 end 82 "<#{self.class} #{fields.join(", ")}>" 83 end
Source
# File lib/thrift/struct.rb 85 def read(iprot, remaining_depth = DEFAULT_RECURSION_DEPTH) 86 raise ProtocolException.new(ProtocolException::DEPTH_LIMIT, "Maximum recursion depth exceeded") if remaining_depth <= 0 87 unless instance_variables.empty? 88 defaults = fields_with_default_values 89 struct_fields.each_value do |field_info| 90 instance_variable_set("@#{field_info[:name]}", nil) 91 end 92 defaults.each do |name, default_value| 93 instance_variable_set("@#{name}", (default_value.dup rescue default_value)) 94 end 95 end 96 97 iprot.read_struct_begin 98 loop do 99 _, ftype, fid = iprot.read_field_begin 100 break if (ftype == Types::STOP) 101 handle_message(iprot, fid, ftype, remaining_depth) 102 iprot.read_field_end 103 end 104 iprot.read_struct_end 105 validate 106 end
Source
# File lib/thrift/struct.rb 108 def write(oprot, remaining_depth = DEFAULT_RECURSION_DEPTH) 109 raise ProtocolException.new(ProtocolException::DEPTH_LIMIT, "Maximum recursion depth exceeded") if remaining_depth <= 0 110 validate 111 oprot.write_struct_begin(self.class.name) 112 each_field do |fid, field_info| 113 name = field_info[:name] 114 type = field_info[:type] 115 value = instance_variable_get("@#{name}") 116 unless value.nil? 117 if is_container? type 118 oprot.write_field_begin(name, type, fid) 119 write_container(oprot, value, field_info, remaining_depth) 120 oprot.write_field_end 121 elsif type == Types::STRUCT 122 oprot.write_field(field_info, fid, value, remaining_depth) 123 else 124 oprot.write_field(field_info, fid, value) 125 end 126 end 127 end 128 oprot.write_field_stop 129 oprot.write_struct_end 130 end
Protected Instance Methods
Source
# File lib/thrift/struct.rb 229 def exception_initialize(*args, &block) 230 if args.size == 1 and args.first.is_a? Hash 231 # looks like it's a regular Struct initialize 232 method(:struct_initialize).call(args.first) 233 else 234 # call the Struct initializer first with no args 235 # this will set our field default values 236 method(:struct_initialize).call 237 # now give it to the exception 238 self.class.send(:class_variable_get, :'@@__thrift_struct_real_initialize').bind_call(self, *args, &block) if args.size > 0 239 # self.class.instance_method(:initialize).bind(self).call(*args, &block) 240 end 241 end
Source
# File lib/thrift/struct.rb 243 def handle_message(iprot, fid, ftype, remaining_depth) 244 field = struct_fields[fid] 245 if field and field[:type] == ftype 246 value = read_field(iprot, field, remaining_depth) 247 instance_variable_set("@#{field[:name]}", value) 248 else 249 iprot.skip(ftype) 250 end 251 end