Module | Thrift::Struct |
In: |
lib/thrift/struct.rb
lib/thrift/struct.rb |
# File lib/thrift/struct.rb, line 150 150: def self.field_accessor(klass, field_info) 151: field_name_sym = field_info[:name].to_sym 152: klass.send :attr_reader, field_name_sym 153: klass.send :define_method, "#{field_info[:name]}=" do |value| 154: Thrift.check_type(value, field_info, field_info[:name]) if Thrift.type_checking 155: instance_variable_set("@#{field_name_sym}", value) 156: end 157: end
# File lib/thrift/struct.rb, line 150 150: def self.field_accessor(klass, field_info) 151: field_name_sym = field_info[:name].to_sym 152: klass.send :attr_reader, field_name_sym 153: klass.send :define_method, "#{field_info[:name]}=" do |value| 154: Thrift.check_type(value, field_info, field_info[:name]) if Thrift.type_checking 155: instance_variable_set("@#{field_name_sym}", value) 156: end 157: end
# File lib/thrift/struct.rb, line 159 159: def self.generate_accessors(klass) 160: klass::FIELDS.values.each do |field_info| 161: field_accessor(klass, field_info) 162: qmark_isset_method(klass, field_info) 163: end 164: end
# File lib/thrift/struct.rb, line 159 159: def self.generate_accessors(klass) 160: klass::FIELDS.values.each do |field_info| 161: field_accessor(klass, field_info) 162: qmark_isset_method(klass, field_info) 163: end 164: end
# File lib/thrift/struct.rb, line 24 24: def initialize(d={}, &block) 25: # get a copy of the default values to work on, removing defaults in favor of arguments 26: fields_with_defaults = fields_with_default_values.dup 27: 28: # check if the defaults is empty, or if there are no parameters for this 29: # instantiation, and if so, don't bother overriding defaults. 30: unless fields_with_defaults.empty? || d.empty? 31: d.each_key do |name| 32: fields_with_defaults.delete(name.to_s) 33: end 34: end 35: 36: # assign all the user-specified arguments 37: unless d.empty? 38: d.each do |name, value| 39: unless name_to_id(name.to_s) 40: raise Exception, "Unknown key given to #{self.class}.new: #{name}" 41: end 42: Thrift.check_type(value, struct_fields[name_to_id(name.to_s)], name) if Thrift.type_checking 43: instance_variable_set("@#{name}", value) 44: end 45: end 46: 47: # assign all the default values 48: unless fields_with_defaults.empty? 49: fields_with_defaults.each do |name, default_value| 50: instance_variable_set("@#{name}", (default_value.dup rescue default_value)) 51: end 52: end 53: 54: yield self if block_given? 55: end
# File lib/thrift/struct.rb, line 24 24: def initialize(d={}, &block) 25: # get a copy of the default values to work on, removing defaults in favor of arguments 26: fields_with_defaults = fields_with_default_values.dup 27: 28: # check if the defaults is empty, or if there are no parameters for this 29: # instantiation, and if so, don't bother overriding defaults. 30: unless fields_with_defaults.empty? || d.empty? 31: d.each_key do |name| 32: fields_with_defaults.delete(name.to_s) 33: end 34: end 35: 36: # assign all the user-specified arguments 37: unless d.empty? 38: d.each do |name, value| 39: unless name_to_id(name.to_s) 40: raise Exception, "Unknown key given to #{self.class}.new: #{name}" 41: end 42: Thrift.check_type(value, struct_fields[name_to_id(name.to_s)], name) if Thrift.type_checking 43: instance_variable_set("@#{name}", value) 44: end 45: end 46: 47: # assign all the default values 48: unless fields_with_defaults.empty? 49: fields_with_defaults.each do |name, default_value| 50: instance_variable_set("@#{name}", (default_value.dup rescue default_value)) 51: end 52: end 53: 54: yield self if block_given? 55: end
# File lib/thrift/struct.rb, line 166 166: def self.qmark_isset_method(klass, field_info) 167: klass.send :define_method, "#{field_info[:name]}?" do 168: !self.send(field_info[:name].to_sym).nil? 169: end 170: end
# File lib/thrift/struct.rb, line 166 166: def self.qmark_isset_method(klass, field_info) 167: klass.send :define_method, "#{field_info[:name]}?" do 168: !self.send(field_info[:name].to_sym).nil? 169: end 170: end
# File lib/thrift/struct.rb, line 198 198: def self.append_features(mod) 199: if mod.ancestors.include? ::Exception 200: mod.send :class_variable_set, '@@__thrift_struct_real_initialize''@@__thrift_struct_real_initialize', mod.instance_method(:initialize) 201: super 202: # set up our custom initializer so `raise Xception, 'message'` works 203: mod.send :define_method, :struct_initialize, mod.instance_method(:initialize) 204: mod.send :define_method, :initialize, mod.instance_method(:exception_initialize) 205: else 206: super 207: end 208: end
# File lib/thrift/struct.rb, line 198 198: def self.append_features(mod) 199: if mod.ancestors.include? ::Exception 200: mod.send :class_variable_set, '@@__thrift_struct_real_initialize''@@__thrift_struct_real_initialize', mod.instance_method(:initialize) 201: super 202: # set up our custom initializer so `raise Xception, 'message'` works 203: mod.send :define_method, :struct_initialize, mod.instance_method(:initialize) 204: mod.send :define_method, :initialize, mod.instance_method(:exception_initialize) 205: else 206: super 207: end 208: end
# File lib/thrift/struct.rb, line 172 172: def <=>(other) 173: if self.class == other.class 174: each_field do |fid, field_info| 175: v1 = self.send(field_info[:name]) 176: v1_set = !v1.nil? 177: v2 = other.send(field_info[:name]) 178: v2_set = !v2.nil? 179: if v1_set && !v2_set 180: return -1 181: elsif !v1_set && v2_set 182: return 1 183: elsif v1_set && v2_set 184: cmp = v1 <=> v2 185: if cmp != 0 186: return cmp 187: end 188: end 189: end 190: 0 191: else 192: self.class <=> other.class 193: end 194: end
# File lib/thrift/struct.rb, line 172 172: def <=>(other) 173: if self.class == other.class 174: each_field do |fid, field_info| 175: v1 = self.send(field_info[:name]) 176: v1_set = !v1.nil? 177: v2 = other.send(field_info[:name]) 178: v2_set = !v2.nil? 179: if v1_set && !v2_set 180: return -1 181: elsif !v1_set && v2_set 182: return 1 183: elsif v1_set && v2_set 184: cmp = v1 <=> v2 185: if cmp != 0 186: return cmp 187: end 188: end 189: end 190: 0 191: else 192: self.class <=> other.class 193: end 194: end
# File lib/thrift/struct.rb, line 116 116: def ==(other) 117: each_field do |fid, field_info| 118: name = field_info[:name] 119: return false unless self.instance_variable_get("@#{name}") == other.instance_variable_get("@#{name}") 120: end 121: true 122: end
# File lib/thrift/struct.rb, line 116 116: def ==(other) 117: each_field do |fid, field_info| 118: name = field_info[:name] 119: return false unless self.instance_variable_get("@#{name}") == other.instance_variable_get("@#{name}") 120: end 121: true 122: end
# File lib/thrift/struct.rb, line 137 137: def differences(other) 138: diffs = [] 139: unless other.is_a?(self.class) 140: diffs << "Different class!" 141: else 142: each_field do |fid, field_info| 143: name = field_info[:name] 144: diffs << "#{name} differs!" unless self.instance_variable_get("@#{name}") == other.instance_variable_get("@#{name}") 145: end 146: end 147: diffs 148: end
# File lib/thrift/struct.rb, line 137 137: def differences(other) 138: diffs = [] 139: unless other.is_a?(self.class) 140: diffs << "Different class!" 141: else 142: each_field do |fid, field_info| 143: name = field_info[:name] 144: diffs << "#{name} differs!" unless self.instance_variable_get("@#{name}") == other.instance_variable_get("@#{name}") 145: end 146: end 147: diffs 148: end
# File lib/thrift/struct.rb, line 124 124: def eql?(other) 125: self.class == other.class && self == other 126: end
# File lib/thrift/struct.rb, line 124 124: def eql?(other) 125: self.class == other.class && self == other 126: end
# File lib/thrift/struct.rb, line 57 57: def fields_with_default_values 58: fields_with_default_values = self.class.instance_variable_get("@fields_with_default_values") 59: unless fields_with_default_values 60: fields_with_default_values = {} 61: struct_fields.each do |fid, field_def| 62: unless field_def[:default].nil? 63: fields_with_default_values[field_def[:name]] = field_def[:default] 64: end 65: end 66: self.class.instance_variable_set("@fields_with_default_values", fields_with_default_values) 67: end 68: fields_with_default_values 69: end
# File lib/thrift/struct.rb, line 57 57: def fields_with_default_values 58: fields_with_default_values = self.class.instance_variable_get("@fields_with_default_values") 59: unless fields_with_default_values 60: fields_with_default_values = {} 61: struct_fields.each do |fid, field_def| 62: unless field_def[:default].nil? 63: fields_with_default_values[field_def[:name]] = field_def[:default] 64: end 65: end 66: self.class.instance_variable_set("@fields_with_default_values", fields_with_default_values) 67: end 68: fields_with_default_values 69: end
# File lib/thrift/struct.rb, line 128 128: def hash 129: field_values = [] 130: each_field do |fid, field_info| 131: name = field_info[:name] 132: field_values << self.instance_variable_get("@#{name}") 133: end 134: field_values.hash 135: end
# File lib/thrift/struct.rb, line 128 128: def hash 129: field_values = [] 130: each_field do |fid, field_info| 131: name = field_info[:name] 132: field_values << self.instance_variable_get("@#{name}") 133: end 134: field_values.hash 135: end
# File lib/thrift/struct.rb, line 71 71: def inspect(skip_optional_nulls = true) 72: fields = [] 73: each_field do |fid, field_info| 74: name = field_info[:name] 75: value = instance_variable_get("@#{name}") 76: unless skip_optional_nulls && field_info[:optional] && value.nil? 77: fields << "#{name}:#{inspect_field(value, field_info)}" 78: end 79: end 80: "<#{self.class} #{fields.join(", ")}>" 81: end
# File lib/thrift/struct.rb, line 71 71: def inspect(skip_optional_nulls = true) 72: fields = [] 73: each_field do |fid, field_info| 74: name = field_info[:name] 75: value = instance_variable_get("@#{name}") 76: unless skip_optional_nulls && field_info[:optional] && value.nil? 77: fields << "#{name}:#{inspect_field(value, field_info)}" 78: end 79: end 80: "<#{self.class} #{fields.join(", ")}>" 81: end
# File lib/thrift/struct.rb, line 83 83: def read(iprot) 84: iprot.read_struct_begin 85: loop do 86: fname, ftype, fid = iprot.read_field_begin 87: break if (ftype == Types::STOP) 88: handle_message(iprot, fid, ftype) 89: iprot.read_field_end 90: end 91: iprot.read_struct_end 92: validate 93: end
# File lib/thrift/struct.rb, line 83 83: def read(iprot) 84: iprot.read_struct_begin 85: loop do 86: fname, ftype, fid = iprot.read_field_begin 87: break if (ftype == Types::STOP) 88: handle_message(iprot, fid, ftype) 89: iprot.read_field_end 90: end 91: iprot.read_struct_end 92: validate 93: end
# File lib/thrift/struct.rb, line 95 95: def write(oprot) 96: validate 97: oprot.write_struct_begin(self.class.name) 98: each_field do |fid, field_info| 99: name = field_info[:name] 100: type = field_info[:type] 101: value = instance_variable_get("@#{name}") 102: unless value.nil? 103: if is_container? type 104: oprot.write_field_begin(name, type, fid) 105: write_container(oprot, value, field_info) 106: oprot.write_field_end 107: else 108: oprot.write_field(name, type, fid, value) 109: end 110: end 111: end 112: oprot.write_field_stop 113: oprot.write_struct_end 114: end
# File lib/thrift/struct.rb, line 95 95: def write(oprot) 96: validate 97: oprot.write_struct_begin(self.class.name) 98: each_field do |fid, field_info| 99: name = field_info[:name] 100: type = field_info[:type] 101: value = instance_variable_get("@#{name}") 102: unless value.nil? 103: if is_container? type 104: oprot.write_field_begin(name, type, fid) 105: write_container(oprot, value, field_info) 106: oprot.write_field_end 107: else 108: oprot.write_field(name, type, fid, value) 109: end 110: end 111: end 112: oprot.write_field_stop 113: oprot.write_struct_end 114: end
# File lib/thrift/struct.rb, line 210 210: def exception_initialize(*args, &block) 211: if args.size == 1 and args.first.is_a? Hash 212: # looks like it's a regular Struct initialize 213: method(:struct_initialize).call(args.first) 214: else 215: # call the Struct initializer first with no args 216: # this will set our field default values 217: method(:struct_initialize).call() 218: # now give it to the exception 219: self.class.send(:class_variable_get, '@@__thrift_struct_real_initialize''@@__thrift_struct_real_initialize').bind(self).call(*args, &block) if args.size > 0 220: # self.class.instance_method(:initialize).bind(self).call(*args, &block) 221: end 222: end
# File lib/thrift/struct.rb, line 210 210: def exception_initialize(*args, &block) 211: if args.size == 1 and args.first.is_a? Hash 212: # looks like it's a regular Struct initialize 213: method(:struct_initialize).call(args.first) 214: else 215: # call the Struct initializer first with no args 216: # this will set our field default values 217: method(:struct_initialize).call() 218: # now give it to the exception 219: self.class.send(:class_variable_get, '@@__thrift_struct_real_initialize''@@__thrift_struct_real_initialize').bind(self).call(*args, &block) if args.size > 0 220: # self.class.instance_method(:initialize).bind(self).call(*args, &block) 221: end 222: end
# File lib/thrift/struct.rb, line 224 224: def handle_message(iprot, fid, ftype) 225: field = struct_fields[fid] 226: if field and field[:type] == ftype 227: value = read_field(iprot, field) 228: instance_variable_set("@#{field[:name]}", value) 229: else 230: iprot.skip(ftype) 231: end 232: end