Class: Eventsimple::DataType

Inherits:
ActiveModel::Type::Value
  • Object
show all
Defined in:
lib/eventsimple/data_type.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event_klass) ⇒ DataType

Returns a new instance of DataType.



5
6
7
8
# File 'lib/eventsimple/data_type.rb', line 5

def initialize(event_klass)
  @event_klass = event_klass
  super()
end

Instance Attribute Details

#event_klassObject (readonly)

Returns the value of attribute event_klass.



10
11
12
# File 'lib/eventsimple/data_type.rb', line 10

def event_klass
  @event_klass
end

Instance Method Details

#cast_value(value) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/eventsimple/data_type.rb', line 16

def cast_value(value)
  case value
  when String
    decoded = ActiveSupport::JSON.decode(value)
    return event_klass::Message.new(decoded) if event_klass.const_defined?(:Message)

    decoded
  when Hash
    return event_klass::Message.new(value) if event_klass.const_defined?(:Message)

    value
  when event_klass::Message
    value
  end
end

#deserialize(value) ⇒ Object



41
42
43
44
45
46
# File 'lib/eventsimple/data_type.rb', line 41

def deserialize(value)
  decoded = ActiveSupport::JSON.decode(value)
  return event_klass::Message.new(decoded) if event_klass.const_defined?(:Message)

  decoded
end

#serialize(value) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/eventsimple/data_type.rb', line 32

def serialize(value)
  case value
  when Hash, event_klass::Message
    ActiveSupport::JSON.encode(value)
  else
    super
  end
end

#typeObject



12
13
14
# File 'lib/eventsimple/data_type.rb', line 12

def type
  :data_type
end