Class: SimplySerializable::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/simply_serializable/serializer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes: [], cache: {}, do_not_serialize_if_class_is: [], except: nil, include_readable_instance_variables: true, method_prefix: :serialize, object:, only: nil) ⇒ Serializer

Returns a new instance of Serializer.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/simply_serializable/serializer.rb', line 11

def initialize(
  attributes: [],
  cache: {},
  do_not_serialize_if_class_is: [],
  except: nil,
  include_readable_instance_variables: true,
  method_prefix: :serialize,
  object:,
  only: nil
)
  @object = object
  @id = id
  @attributes = attributes
  @do_not_serialize_if_class_is = do_not_serialize_if_class_is || []
  @do_not_serialize_if_class_is = [@do_not_serialize_if_class_is] unless @do_not_serialize_if_class_is.is_a?(Array)
  @include_readable_instance_variables = include_readable_instance_variables
  @except = except&.map(&:to_sym)
  @only = only&.map(&:to_sym)
  @method_prefix = method_prefix
  @local_cache = cache
  @local_cache[cache_key] = nil

  populate_attributes
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



5
6
7
# File 'lib/simply_serializable/serializer.rb', line 5

def attributes
  @attributes
end

#do_not_serialize_if_class_isObject (readonly)

Returns the value of attribute do_not_serialize_if_class_is.



5
6
7
# File 'lib/simply_serializable/serializer.rb', line 5

def do_not_serialize_if_class_is
  @do_not_serialize_if_class_is
end

#exceptObject (readonly)

Returns the value of attribute except.



5
6
7
# File 'lib/simply_serializable/serializer.rb', line 5

def except
  @except
end

#objectObject (readonly)

Returns the value of attribute object.



5
6
7
# File 'lib/simply_serializable/serializer.rb', line 5

def object
  @object
end

#onlyObject (readonly)

Returns the value of attribute only.



5
6
7
# File 'lib/simply_serializable/serializer.rb', line 5

def only
  @only
end

Instance Method Details

#cacheObject



36
37
38
39
40
41
# File 'lib/simply_serializable/serializer.rb', line 36

def cache
  @cache ||= begin
    serialize
    @local_cache
  end
end

#idObject



43
44
45
# File 'lib/simply_serializable/serializer.rb', line 43

def id
  @id ||= cache_key
end

#nestable?Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
# File 'lib/simply_serializable/serializer.rb', line 47

def nestable?
  return @nestable unless @nestable.nil?

  serialize
  @nestable
end

#nestedObject



54
55
56
57
58
# File 'lib/simply_serializable/serializer.rb', line 54

def nested
  @nested ||= begin
    deep_nest(serialize[:root])
  end
end

#serializeObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/simply_serializable/serializer.rb', line 60

def serialize
  @serialize ||= begin
    @nestable = true
    ret = deep_serialize(object_value_hash)

    @local_cache[cache_key] = {
      id: cache_key,
      object: object.class.name,
      fingeprint: fingerprint,
      data: ret
    }

    {
      root: cache_key,
      objects: @local_cache
    }
  end
end

#to_sObject



79
80
81
# File 'lib/simply_serializable/serializer.rb', line 79

def to_s
  @to_s ||= serialize.to_s
end