Class: Tros::Schema::RecordSchema
- Inherits:
-
NamedSchema
- Object
- Tros::Schema
- NamedSchema
- Tros::Schema::RecordSchema
- Defined in:
- lib/tros/schema.rb
Constant Summary
Constants inherited from Tros::Schema
INT_MAX_VALUE, INT_MIN_VALUE, LONG_MAX_VALUE, LONG_MIN_VALUE, NAMED_TYPES, NAMED_TYPES_SYM, PRIMITIVE_TYPES, PRIMITIVE_TYPES_SYM, VALID_TYPES, VALID_TYPES_SYM
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
Attributes inherited from NamedSchema
Attributes inherited from Tros::Schema
Class Method Summary collapse
Instance Method Summary collapse
- #fields_hash ⇒ Object
-
#initialize(name, namespace, fields, names = nil, schema_type = :record) ⇒ RecordSchema
constructor
A new instance of RecordSchema.
- #to_avro(names = Set.new) ⇒ Object
Methods inherited from NamedSchema
Methods inherited from Tros::Schema
#==, #hash, parse, real_parse, #subparse, #to_s, #type, validate, validate_strictly
Constructor Details
#initialize(name, namespace, fields, names = nil, schema_type = :record) ⇒ RecordSchema
Returns a new instance of RecordSchema.
228 229 230 231 232 233 234 235 236 |
# File 'lib/tros/schema.rb', line 228 def initialize(name, namespace, fields, names=nil, schema_type=:record) if schema_type == :request || schema_type == 'request' @type_sym = schema_type.to_sym @namespace = namespace else super(schema_type, name, namespace, names) end @fields = RecordSchema.make_field_objects(fields, names, self.namespace) end |
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
204 205 206 |
# File 'lib/tros/schema.rb', line 204 def fields @fields end |
Class Method Details
.make_field_objects(field_data, names, namespace = nil) ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/tros/schema.rb', line 206 def self.make_field_objects(field_data, names, namespace=nil) field_objects, field_names = [], Set.new field_data.each_with_index do |field, i| if field.respond_to?(:[]) # TODO(jmhodges) wtffffff type = field['type'] name = field['name'] default = field['default'] order = field['order'] new_field = Field.new(type, name, default, order, names, namespace) # make sure field name has not been used yet if field_names.include?(new_field.name) raise SchemaParseError, "Field name #{new_field.name.inspect} is already in use" end field_names << new_field.name else raise SchemaParseError, "Not a valid field: #{field}" end field_objects << new_field end field_objects end |
Instance Method Details
#fields_hash ⇒ Object
238 239 240 |
# File 'lib/tros/schema.rb', line 238 def fields_hash @fields_hash ||= fields.inject({}){|hsh, field| hsh[field.name] = field; hsh } end |
#to_avro(names = Set.new) ⇒ Object
242 243 244 245 246 247 248 249 250 251 |
# File 'lib/tros/schema.rb', line 242 def to_avro(names=Set.new) hsh = super return hsh unless hsh.is_a?(Hash) hsh['fields'] = @fields.map {|f| f.to_avro(names) } if type_sym == :request hsh['fields'] else hsh end end |