Class: RFlow::Message::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/rflow/message.rb

Overview

Should proxy most methods to data_object that we can serialize to avro using the schema. Extensions should use ‘extended’ hook to apply immediate changes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema_string, serialization_type = 'avro', serialized_data = nil) ⇒ Data

Returns a new instance of Data.

Raises:

  • (ArgumentError)


136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/rflow/message.rb', line 136

def initialize(schema_string, serialization_type = 'avro', serialized_data = nil)
  raise ArgumentError, 'Only Avro serialization_type supported at the moment' unless serialization_type.to_s == 'avro'

  @schema_string = schema_string
  @serialization_type = serialization_type.to_s

  begin
    @schema = ::Avro::Schema.parse(schema_string)
    @writer = ::Avro::IO::DatumWriter.new(@schema)
  rescue Exception => e
    raise ArgumentError, "Invalid schema '#{@schema_string}': #{e}: #{e.message}"
  end

  if serialized_data
    serialized_data.force_encoding 'BINARY'
    @data_object = RFlow::Avro.decode(::Avro::IO::DatumReader.new(schema, schema), serialized_data)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *args, &block) ⇒ Object

Proxy methods down to the underlying data_object, probably a Hash. Hopefully an extension will provide any additional functionality so this won’t be called unless needed



166
167
168
# File 'lib/rflow/message.rb', line 166

def method_missing(method_sym, *args, &block)
  @data_object.send(method_sym, *args, &block)
end

Instance Attribute Details

#data_objectObject

Returns the value of attribute data_object.



134
135
136
# File 'lib/rflow/message.rb', line 134

def data_object
  @data_object
end

#schemaObject (readonly)

Returns the value of attribute schema.



133
134
135
# File 'lib/rflow/message.rb', line 133

def schema
  @schema
end

#schema_stringObject (readonly)

Returns the value of attribute schema_string.



133
134
135
# File 'lib/rflow/message.rb', line 133

def schema_string
  @schema_string
end

#serialization_typeObject (readonly)

Returns the value of attribute serialization_type.



133
134
135
# File 'lib/rflow/message.rb', line 133

def serialization_type
  @serialization_type
end

Instance Method Details

#to_avroObject



159
160
161
# File 'lib/rflow/message.rb', line 159

def to_avro
  RFlow::Avro.encode @writer, @data_object
end

#valid?Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/rflow/message.rb', line 155

def valid?
  ::Avro::Schema.validate @schema, @data_object
end