Module: Thrift::Struct
- Defined in:
- lib/thrift/struct.rb
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #differences(other) ⇒ Object
- #each_field ⇒ Object
- #eql?(other) ⇒ Boolean
- #fields_with_default_values ⇒ Object
- #hash ⇒ Object
- #initialize(d = {}) ⇒ Object
- #inspect(skip_optional_nulls = true) ⇒ Object
- #name_to_id(name) ⇒ Object
- #read(iprot) ⇒ Object
- #write(oprot) ⇒ Object
Class Method Details
.field_accessor(klass, *fields) ⇒ Object
166 167 168 169 170 171 172 173 174 |
# File 'lib/thrift/struct.rb', line 166 def self.field_accessor(klass, *fields) fields.each do |field| klass.send :attr_reader, field klass.send :define_method, "#{field}=" do |value| Thrift.check_type(value, klass::FIELDS.values.find { |f| f[:name].to_s == field.to_s }, field) if Thrift.type_checking instance_variable_set("@#{field}", value) end end end |
Instance Method Details
#==(other) ⇒ Object
132 133 134 135 136 137 138 |
# File 'lib/thrift/struct.rb', line 132 def ==(other) each_field do |fid, field_info| name = field_info[:name] return false unless self.instance_variable_get("@#{name}") == other.instance_variable_get("@#{name}") end true end |
#differences(other) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/thrift/struct.rb', line 153 def differences(other) diffs = [] unless other.is_a?(self.class) diffs << "Different class!" else each_field do |fid, field_info| name = field_info[:name] diffs << "#{name} differs!" unless self.instance_variable_get("@#{name}") == other.instance_variable_get("@#{name}") end end diffs end |
#each_field ⇒ Object
81 82 83 84 85 86 |
# File 'lib/thrift/struct.rb', line 81 def each_field struct_fields.keys.sort.each do |fid| data = struct_fields[fid] yield fid, data end end |
#eql?(other) ⇒ Boolean
140 141 142 |
# File 'lib/thrift/struct.rb', line 140 def eql?(other) self.class == other.class && self == other end |
#fields_with_default_values ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/thrift/struct.rb', line 55 def fields_with_default_values fields_with_default_values = self.class.instance_variable_get("@fields_with_default_values") unless fields_with_default_values fields_with_default_values = {} struct_fields.each do |fid, field_def| unless field_def[:default].nil? fields_with_default_values[field_def[:name]] = field_def[:default] end end self.class.instance_variable_set("@fields_with_default_values", fields_with_default_values) end fields_with_default_values end |
#hash ⇒ Object
144 145 146 147 148 149 150 151 |
# File 'lib/thrift/struct.rb', line 144 def hash field_values = [] each_field do |fid, field_info| name = field_info[:name] field_values << self.instance_variable_get("@#{name}") end field_values.hash end |
#initialize(d = {}) ⇒ Object
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 |
# File 'lib/thrift/struct.rb', line 24 def initialize(d={}) # get a copy of the default values to work on, removing defaults in favor of arguments fields_with_defaults = fields_with_default_values.dup # check if the defaults is empty, or if there are no parameters for this # instantiation, and if so, don't bother overriding defaults. unless fields_with_defaults.empty? || d.empty? d.each_key do |name| fields_with_defaults.delete(name.to_s) end end # assign all the user-specified arguments unless d.empty? d.each do |name, value| unless name_to_id(name.to_s) raise Exception, "Unknown key given to #{self.class}.new: #{name}" end Thrift.check_type(value, struct_fields[name_to_id(name.to_s)], name) if Thrift.type_checking instance_variable_set("@#{name}", value) end end # assign all the default values unless fields_with_defaults.empty? fields_with_defaults.each do |name, default_value| instance_variable_set("@#{name}", (default_value.dup rescue default_value)) end end end |
#inspect(skip_optional_nulls = true) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/thrift/struct.rb', line 88 def inspect(skip_optional_nulls = true) fields = [] each_field do |fid, field_info| name = field_info[:name] value = instance_variable_get("@#{name}") unless skip_optional_nulls && field_info[:optional] && value.nil? fields << "#{name}:#{value.inspect}" end end "<#{self.class} #{fields.join(", ")}>" end |
#name_to_id(name) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/thrift/struct.rb', line 69 def name_to_id(name) names_to_ids = self.class.instance_variable_get("@names_to_ids") unless names_to_ids names_to_ids = {} struct_fields.each do |fid, field_def| names_to_ids[field_def[:name]] = fid end self.class.instance_variable_set("@names_to_ids", names_to_ids) end names_to_ids[name] end |
#read(iprot) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/thrift/struct.rb', line 100 def read(iprot) iprot.read_struct_begin loop do fname, ftype, fid = iprot.read_field_begin break if (ftype == Types::STOP) (iprot, fid, ftype) iprot.read_field_end end iprot.read_struct_end validate end |
#write(oprot) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/thrift/struct.rb', line 112 def write(oprot) validate oprot.write_struct_begin(self.class.name) each_field do |fid, field_info| name = field_info[:name] type = field_info[:type] if (value = instance_variable_get("@#{name}")) if is_container? type oprot.write_field_begin(name, type, fid) write_container(oprot, value, field_info) oprot.write_field_end else oprot.write_field(name, type, fid, value) end end end oprot.write_field_stop oprot.write_struct_end end |