Class: Icss::RecordField

Inherits:
Object
  • Object
show all
Includes:
Receiver, Receiver::ActsAsHash
Defined in:
lib/icss/type.rb

Overview

Describes a field in an Avro Record object.

Each field has the following attributes:

  • name: a string providing the name of the field (required), and

  • doc: a string describing this field for users (optional).

  • type: a schema, or a string or symbol naming a record definition (required).

    avro type     json type       ruby type       example
    null          null            NilClass        nil
    boolean       boolean         Boolean         true
    int,long      integer         Integer         1
    float,double  number          Float           1.1
    bytes         string          String          "\u00FF"
    string        string          String          "foo"
    record        object          RecordType      {"a": 1}
    enum          string          Enum            "FOO"
    array         array           Array           [1]
    map           object          Hash            { "a": 1 }
    fixed         string          String          "\u00ff"
    
  • default: a default value for this field, used when reading instances that

    lack this field (optional). Permitted values depend on the
    field's schema type, according to the table below. Default
    values for union fields correspond to the first schema in the
    union. Default values for bytes and fixed fields are JSON
    strings, where Unicode code points 0-255 are mapped to unsigned
    8-bit byte values 0-255.
    
  • order: specifies how this field impacts sort ordering of this record

    (optional). Valid values are "ascending" (the default),
    "descending", or "ignore". For more details on how this is used,
    see the the sort order section below.
    
  • required: raises an error if the field is unset when validate! is called

See RecordType for examples.

Constant Summary collapse

ALLOWED_ORDERS =
%w[ascending descending ignore].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#is_referenceObject

is_reference is true if the type is a named reference to a defined type; false if the type was defined right here in the schema.



275
276
277
# File 'lib/icss/type.rb', line 275

def is_reference
  @is_reference
end

#typeObject

work around a bug in ruby 1.8, which has defined (and deprecated) type



267
268
269
# File 'lib/icss/type.rb', line 267

def type
  @type
end

Instance Method Details

#enum?Boolean

Returns:

  • (Boolean)


298
# File 'lib/icss/type.rb', line 298

def enum?()   type.is_a? Icss::EnumType   end

#expand_typeObject



309
310
311
312
313
314
315
316
317
# File 'lib/icss/type.rb', line 309

def expand_type
  case
  when is_reference? && type.respond_to?(:name) then type.name
  when is_reference?                            then '(_unspecified_)'
  when type.is_a?(Array) then type.map{|t| t.to_hash }
  when type.respond_to?(:to_hash) then type.to_hash
  else type.to_s
  end
end

#is_reference?Boolean

Returns:

  • (Boolean)


276
# File 'lib/icss/type.rb', line 276

def is_reference?() is_reference ; end

#orderObject



284
285
286
# File 'lib/icss/type.rb', line 284

def order
  @order || 'ascending'
end

#order=(v) ⇒ Object

QUESTION: should this be an override of order=, or of receive_order?



291
292
293
294
# File 'lib/icss/type.rb', line 291

def order= v
  raise "'order' may only take the values ascending (the default), descending, or ignore." unless v.nil? || ALLOWED_ORDERS.include?(v)
  @order = v
end

#order_directionObject



287
288
289
# File 'lib/icss/type.rb', line 287

def order_direction
  case order when 'ascending' then 1 when 'descending' then -1 else 0 ; end
end

#receive_type(type_info) ⇒ Object



278
279
280
281
# File 'lib/icss/type.rb', line 278

def receive_type type_info
  self.is_reference = type_info.is_a?(String) || type_info.is_a?(Symbol)
  self.type = TypeFactory.receive(type_info)
end

#record?Boolean

Returns:

  • (Boolean)


296
# File 'lib/icss/type.rb', line 296

def record?() type.is_a? Icss::RecordType end

#to_hashObject



300
301
302
303
304
305
306
307
# File 'lib/icss/type.rb', line 300

def to_hash()
  { :name    => name,
    :type    => expand_type,
    :default => default,
    :order   => @order,
    :doc     => doc,
  }.reject{|k,v| v.nil? }
end

#union?Boolean

Returns:

  • (Boolean)


297
# File 'lib/icss/type.rb', line 297

def union?()  type.is_a? Array            end