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
# File 'lib/eventsimple/data_type.rb', line 16

def cast_value(value)
  case value
  when String
    decoded = ActiveSupport::JSON.decode(value)
    prepare_data(decoded)
  when Hash
    prepare_data(value)
  when message_class
    value
  end
end

#changed_in_place?(raw_old_value, new_value) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
# File 'lib/eventsimple/data_type.rb', line 47

def changed_in_place?(raw_old_value, new_value)
  old_value = deserialize(raw_old_value)
  old_value != new_value
end

#deserialize(value) ⇒ Object



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

def deserialize(value)
  decoded = ActiveSupport::JSON.decode(value)
  decrypted_data = decrypt_message_data(decoded)
  message_class ? message_class.new(decrypted_data) : decrypted_data
end

#mutable?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/eventsimple/data_type.rb', line 52

def mutable?
  true
end

#serialize(value) ⇒ Object



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

def serialize(value)
  case value
  when Hash
    encrypted_data = encrypt_message_data(value)
    ActiveSupport::JSON.encode(encrypted_data)
  when event_klass::Message
    encrypted_data = encrypt_message_data(value.attributes)
    ActiveSupport::JSON.encode(encrypted_data)
  else
    super
  end
end

#typeObject



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

def type
  :data_type
end