Class: NestedRecord::Type

Inherits:
ActiveModel::Type::Value
  • Object
show all
Defined in:
lib/nested_record/type.rb,
lib/nested_record/type/one.rb,
lib/nested_record/type/many.rb

Direct Known Subclasses

Many, One

Defined Under Namespace

Classes: Many, One

Instance Method Summary collapse

Constructor Details

#initialize(setup) ⇒ Type

Returns a new instance of Type.



7
8
9
# File 'lib/nested_record/type.rb', line 7

def initialize(setup)
  @setup = setup
end

Instance Method Details

#cast(data) ⇒ Object



11
12
13
# File 'lib/nested_record/type.rb', line 11

def cast(data)
  cast_value(data)
end

#deserialize(value) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/nested_record/type.rb', line 15

def deserialize(value)
  value = if value.is_a?(::String)
    ActiveSupport::JSON.decode(value) rescue nil
  else
    value
  end
  cast_value(value)
end

#serialize(obj) ⇒ Object



24
25
26
# File 'lib/nested_record/type.rb', line 24

def serialize(obj)
  ActiveSupport::JSON.encode(obj.as_json) unless obj.nil?
end