Class: Skit::Attribute

Inherits:
ActiveModel::Type::Value
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/skit/attribute.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type_spec) ⇒ Attribute

Returns a new instance of Attribute.



17
18
19
20
21
22
23
24
# File 'lib/skit/attribute.rb', line 17

def initialize(type_spec)
  super()
  @type_spec = type_spec
  @processor = T.let(
    Serialization.default_registry.processor_for(type_spec),
    Serialization::Processor::Base
  )
end

Instance Attribute Details

#processorObject (readonly)

Returns the value of attribute processor.



27
28
29
# File 'lib/skit/attribute.rb', line 27

def processor
  @processor
end

Class Method Details

.[](type_spec) ⇒ Object



12
13
14
# File 'lib/skit/attribute.rb', line 12

def self.[](type_spec)
  new(type_spec)
end

Instance Method Details

#cast(value) ⇒ Object



32
33
34
35
36
# File 'lib/skit/attribute.rb', line 32

def cast(value)
  return nil if value.nil?

  @processor.deserialize(value)
end

#deserialize(value) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/skit/attribute.rb', line 51

def deserialize(value)
  return nil if value.nil?

  data = if value.is_a?(String)
           ActiveSupport::JSON.decode(value)
         else
           value
         end

  @processor.deserialize(data)
end

#serialize(value) ⇒ Object



41
42
43
44
45
46
# File 'lib/skit/attribute.rb', line 41

def serialize(value)
  return nil if value.nil?

  serialized = @processor.serialize(value)
  ActiveSupport::JSON.encode(serialized)
end