Class: SimplySerializable::Serializer
- Inherits:
-
Object
- Object
- SimplySerializable::Serializer
- Defined in:
- lib/simply_serializable/serializer.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#do_not_serialize_if_class_is ⇒ Object
readonly
Returns the value of attribute do_not_serialize_if_class_is.
-
#except ⇒ Object
readonly
Returns the value of attribute except.
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#only ⇒ Object
readonly
Returns the value of attribute only.
Instance Method Summary collapse
- #cache ⇒ Object
- #id ⇒ Object
-
#initialize(attributes: [], cache: {}, do_not_serialize_if_class_is: [], except: nil, include_readable_instance_variables: true, method_prefix: :serialize, object:, only: nil) ⇒ Serializer
constructor
A new instance of Serializer.
- #nestable? ⇒ Boolean
- #nested ⇒ Object
- #serialize ⇒ Object
- #to_s ⇒ Object
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
#attributes ⇒ Object (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_is ⇒ Object (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 |
#except ⇒ Object (readonly)
Returns the value of attribute except.
5 6 7 |
# File 'lib/simply_serializable/serializer.rb', line 5 def except @except end |
#object ⇒ Object (readonly)
Returns the value of attribute object.
5 6 7 |
# File 'lib/simply_serializable/serializer.rb', line 5 def object @object end |
#only ⇒ Object (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
#cache ⇒ Object
36 37 38 39 40 41 |
# File 'lib/simply_serializable/serializer.rb', line 36 def cache @cache ||= begin serialize @local_cache end end |
#id ⇒ Object
43 44 45 |
# File 'lib/simply_serializable/serializer.rb', line 43 def id @id ||= cache_key end |
#nestable? ⇒ 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 |
#nested ⇒ Object
54 55 56 57 58 |
# File 'lib/simply_serializable/serializer.rb', line 54 def nested @nested ||= begin deep_nest(serialize[:root]) end end |
#serialize ⇒ Object
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_s ⇒ Object
79 80 81 |
# File 'lib/simply_serializable/serializer.rb', line 79 def to_s @to_s ||= serialize.to_s end |