Class: GlueGun::Serializers

Inherits:
Object
  • Object
show all
Defined in:
lib/glue_gun/serializers.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance) ⇒ Serializers

Returns a new instance of Serializers.



5
6
7
# File 'lib/glue_gun/serializers.rb', line 5

def initialize(instance)
  @instance = instance
end

Instance Attribute Details

#instanceObject

Returns the value of attribute instance.



3
4
5
# File 'lib/glue_gun/serializers.rb', line 3

def instance
  @instance
end

Instance Method Details

#allowed_names(names) ⇒ Object



49
50
51
52
# File 'lib/glue_gun/serializers.rb', line 49

def allowed_names(names)
  assoc_names = instance.class.reflect_on_all_associations.map(&:name)
  [names.map(&:to_sym) - assoc_names.map(&:to_sym)].flatten
end

#deserialize_associations(json) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/glue_gun/serializers.rb', line 78

def deserialize_associations(json)
  assoc_names = instance.class.reflect_on_all_associations.map(&:name)
  found_associations = assoc_names & json.keys
  found_associations.each do |association|
    json[association] = instance.send(association)
  end
  json
end

#deserialize_attrs(attrs) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/glue_gun/serializers.rb', line 132

def deserialize_attrs(attrs)
  return nil if attrs.nil?

  attrs.transform_values do |value|
    recursive_deserialize(value)
  end
end

#deserialize_dependencies(serialized_data, service_class) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/glue_gun/serializers.rb', line 101

def deserialize_dependencies(serialized_data, service_class)
  serialized_deps = (serialized_data.keys & allowed_names(service_class.dependency_definitions.keys))
  serialized_deps.each do |name|
    serialized = serialized_data[name]
    definition = service_class.dependency_definitions[name]
    serialized_data[name] = deserialize_dependency(serialized, definition)
  end
  serialized_data
end

#deserialize_dependency(serialized, definition) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/glue_gun/serializers.rb', line 87

def deserialize_dependency(serialized, definition)
  return serialized.map { |dep| deserialize_dependency(dep, definition) } if serialized.is_a?(Array)

  dep_name = serialized.keys.first
  selected_option = definition.option_configs[dep_name]
  dependency_class = selected_option.class_name
  arguments = serialized[dep_name]

  dependency_class.respond_to?(:deserialize) ? dependency_class.deserialize(arguments) : arguments
  {
    dep_name => arguments
  }
end

#deserialize_service_objectObject



111
112
113
114
115
116
117
118
119
# File 'lib/glue_gun/serializers.rb', line 111

def deserialize_service_object
  serialized_data = JSON.parse(instance.read_attribute(:configuration) || "{}")
  serialized_data.deep_symbolize_keys!
  serialized_data = deserialize_attrs(serialized_data)
  service_class = instance.send(:resolve_service_class, serialized_data)
  serialized_data = deserialize_associations(serialized_data)
  serialized_data = service_class.deserialize(serialized_data) if service_class.respond_to?(:deserialize)
  deserialize_dependencies(serialized_data, service_class)
end

#deserialize_special_type(value) ⇒ Object



155
156
157
158
159
160
161
162
# File 'lib/glue_gun/serializers.rb', line 155

def deserialize_special_type(value)
  case value[:__type__]
  when "ActiveSupport::TimeWithZone"
    Time.zone.parse(value[:value])
  else
    value[:value]
  end
end

#recursive_deserialize(value) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/glue_gun/serializers.rb', line 140

def recursive_deserialize(value)
  case value
  when Hash
    if value[:__type__]
      deserialize_special_type(value)
    else
      value.transform_values { |v| recursive_deserialize(v) }
    end
  when Array
    value.map { |v| recursive_deserialize(v) }
  else
    value
  end
end

#serializable!(json) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/glue_gun/serializers.rb', line 68

def serializable!(json)
  regular_args = json.slice(*allowed_names(json.keys))
  assoc_names = instance.class.reflect_on_all_associations.map(&:name)
  found_associations = assoc_names & json.keys
  found_associations.each do |association|
    regular_args[association] = true
  end
  regular_args
end

#serialize_attrs(attrs) ⇒ Object



121
122
123
124
125
126
127
128
129
130
# File 'lib/glue_gun/serializers.rb', line 121

def serialize_attrs(attrs)
  attrs.deep_transform_values do |value|
    case value
    when ActiveSupport::TimeWithZone
      { "__type__" => "ActiveSupport::TimeWithZone", "value" => value.iso8601 }
    else
      value
    end
  end
end

#serialize_dependency(service_object, dependency, dep_instance = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/glue_gun/serializers.rb', line 21

def serialize_dependency(service_object, dependency, dep_instance = nil)
  dep_instance = service_object.send(dependency) if dep_instance.nil?
  return nil unless dep_instance.present?

  if dep_instance.is_a?(Array)
    return dep_instance.map do |dep|
      serialize_dependency(service_object, dependency, dep)
    end
  end

  opts = service_object.dependency_definitions[dependency].option_configs
  selected_option = opts.detect do |_k, v|
    dep_instance.class == v.class_name
  end&.first
  unless selected_option.present?
    raise "Don't know how to serialize dependency of type #{dependency}, available options are #{opts.keys}. You didn't specify an option."
  end

  serialized = serialize_object(dep_instance)
  {
    selected_option => serialized
  }
end

#serialize_object(object) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/glue_gun/serializers.rb', line 9

def serialize_object(object)
  if object.respond_to?(:serialize)
    object.serialize
  elsif object.respond_to?(:attributes)
    object.attributes.deep_compact
  else
    Hash[object.instance_variables.map do |var|
      [var.to_s.delete("@"), object.instance_variable_get(var)]
    end].deep_compact
  end
end

#serialize_service_object(service_object) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/glue_gun/serializers.rb', line 54

def serialize_service_object(service_object)
  attrs = serialize_object(service_object)
  deps = allowed_names(service_object.dependency_definitions.keys).inject({}) do |hash, dep|
    hash.tap do
      serialized = serialize_dependency(service_object, dep)
      next if serialized.nil?

      hash[dep] = serialized
    end
  end
  json = serializable!(serialize_attrs(attrs.merge(deps).deep_symbolize_keys))
  instance.write_attribute(:configuration, json.to_json)
end

#service_classObject



45
46
47
# File 'lib/glue_gun/serializers.rb', line 45

def service_class
  service_object.class
end