Class: ServiceContract::Avro::Type

Inherits:
ServiceContract::AbstractType show all
Defined in:
lib/service_contract/avro/type.rb

Instance Attribute Summary

Attributes inherited from ServiceContract::AbstractType

#definition

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(definition) ⇒ Object



33
34
35
# File 'lib/service_contract/avro/type.rb', line 33

def self.build(definition)
  Type.new(definition)
end

Instance Method Details

#array?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/service_contract/avro/type.rb', line 29

def array?
  type_string == "array"
end

#complex?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/service_contract/avro/type.rb', line 37

def complex?
  type_string == "record"
end

#fieldsObject



12
13
14
15
16
# File 'lib/service_contract/avro/type.rb', line 12

def fields
  definition.fields.map do |field|
    Parameter.new(field)
  end
end

#nameObject



4
5
6
7
8
9
10
# File 'lib/service_contract/avro/type.rb', line 4

def name
  array? ? 
    "Array(#{subtype.name})" :
    complex? ?
      definition.name :
      definition.type.to_s
end

#subtypeObject



24
25
26
27
# File 'lib/service_contract/avro/type.rb', line 24

def subtype
  return nil unless definition.respond_to?(:items)
  Type.build(definition.items)
end

#to_sObject



18
19
20
21
22
# File 'lib/service_contract/avro/type.rb', line 18

def to_s
  return name unless union?
 
  union_types.map(&:name).join(", ")
end

#union?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/service_contract/avro/type.rb', line 41

def union?
  type_string == "union"
end

#valid_ruby_typesObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/service_contract/avro/type.rb', line 45

def valid_ruby_types
  case type_string
  when "array"
    [Array]
  when "int"
    [Fixnum]
  when "string"
    [String]
  when "float"
    [Float]
  when "boolean"
    [TrueClass, FalseClass]
  when "null"
    [NilClass]
  when "union"
    union_types.map(&:valid_ruby_types).flatten
  else # a complex type
    [Hash]
  end
end