Module: BentoSearch::Results::Serialization

Extended by:
ActiveSupport::Concern
Included in:
Author, Link, BentoSearch::ResultItem
Defined in:
app/models/bento_search/results/serialization.rb

Overview

Call #dump_to_json on a BentoSearch value object (such as BentoSearch::Result or ::Author) to get it in Json

Values marked with serializable_attr in BentoSearch::Result are included in seralization.

At present metadata and configuration are NOT serialized: #decorator, #display_configuration, and #engine_id are not included in the serialization, so when loaded from serialization, ResultItems will not have such things set.

  • Works by getting and setting instance variables directly, ignores getters/setters

  • This means decorated values are NOT included in serialization, the raw values are what is serialized. This is intended, we serialize internal state, not decoration which can be recreated. You should make sure the decorators you want are applied after de-serialization.

  • preserves html_safety status in serialization, by adding extra ‘_attr_htmlsafe: true` key/value

Defined Under Namespace

Classes: Date

Instance Method Summary collapse

Instance Method Details

#dump_to_jsonObject



123
124
125
# File 'app/models/bento_search/results/serialization.rb', line 123

def dump_to_json
  JSON.dump self.internal_state_hash
end

#internal_state_hashObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/models/bento_search/results/serialization.rb', line 97

def internal_state_hash
  hash = {}
  self._serializable_attrs.each do |accessor|
    accessor = accessor.to_s
    value = self.instance_variable_get("@#{accessor}")

    next if value.blank?

    if _serializable_attr_options[accessor] && _serializable_attr_options[accessor][:serializer]
      klass = self.class.qualified_const_get(_serializable_attr_options[accessor][:serializer])
      value = klass.dump(value)
    elsif value.respond_to?(:to_ary)
      value = value.to_ary.collect do |item|
        item.respond_to?(:internal_state_hash) ? item.internal_state_hash : item
      end
    end

    hash[accessor] = value

    if value.respond_to?(:html_safe?) && value.html_safe?
      hash["_#{accessor}_htmlsafe"] = true
    end
  end
  return hash
end