Module: Coupdoeil::Params
Constant Summary collapse
- SerializationError =
Class.new(StandardError)
Instance Method Summary collapse
- #convert_to_global_id_hash(param) ⇒ Object
- #deserialize(params) ⇒ Object
- #deserialize_global_id(hash) ⇒ Object
- #deserialize_hash(serialized_hash) ⇒ Object
- #deserialize_param(param) ⇒ Object
- #serialize(*params) ⇒ Object
- #serialize_hash(param) ⇒ Object
- #serialize_hash_key(key) ⇒ Object
- #serialize_param(param) ⇒ Object
- #serialized_global_id?(hash) ⇒ Boolean
Instance Method Details
#convert_to_global_id_hash(param) ⇒ Object
61 62 63 64 65 66 |
# File 'app/models/coupdoeil/params.rb', line 61 def convert_to_global_id_hash(param) { GLOBAL_ID_KEY => param.to_global_id.to_s } rescue URI::GID::MissingModelIdError raise SerializationError, "Unable to serialize #{param.class} " \ "without an id. (Maybe you forgot to call save?)" end |
#deserialize(params) ⇒ Object
25 26 27 28 29 |
# File 'app/models/coupdoeil/params.rb', line 25 def deserialize(params) Array.wrap(params).map { |param| deserialize_param(param) } rescue StandardError raise DeserializationError end |
#deserialize_global_id(hash) ⇒ Object
15 |
# File 'app/models/coupdoeil/params.rb', line 15 def deserialize_global_id(hash) = GlobalID::Locator.locate hash[GLOBAL_ID_KEY] |
#deserialize_hash(serialized_hash) ⇒ Object
16 |
# File 'app/models/coupdoeil/params.rb', line 16 def deserialize_hash(serialized_hash) = serialized_hash.transform_values { deserialize_param(_1) } |
#deserialize_param(param) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'app/models/coupdoeil/params.rb', line 68 def deserialize_param(param) case param when Array param.map { |arg| deserialize_param(arg) } when Hash if serialized_global_id?(param) deserialize_global_id param else deserialize_hash(param) end else param end end |
#serialize(*params) ⇒ Object
19 20 21 22 23 |
# File 'app/models/coupdoeil/params.rb', line 19 def serialize(*params) ActiveSupport::Notifications.instrument("serialize_popover_params.coupdoeil") do params.map { |param| serialize_param(param) } end end |
#serialize_hash(param) ⇒ Object
44 45 46 47 48 |
# File 'app/models/coupdoeil/params.rb', line 44 def serialize_hash(param) param.each_with_object({}) do |(key, value), hash| hash[serialize_hash_key(key)] = serialize_param(value) end end |
#serialize_hash_key(key) ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'app/models/coupdoeil/params.rb', line 50 def serialize_hash_key(key) case key when *RESERVED_KEYS raise SerializationError, "Can't serialize a Hash with reserved key #{key.inspect}" when String, Symbol key.to_s else raise SerializationError, "Only string and symbol hash keys may be serialized as popover params, but #{key.inspect} is a #{key.class}" end end |
#serialize_param(param) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'app/models/coupdoeil/params.rb', line 31 def serialize_param(param) case param when GlobalID::Identification convert_to_global_id_hash(param) when Array param.map { serialize_param(_1) } when Hash serialize_hash(param) else param end end |
#serialized_global_id?(hash) ⇒ Boolean
17 |
# File 'app/models/coupdoeil/params.rb', line 17 def serialized_global_id?(hash) = hash.size == 1 && hash.include?(GLOBAL_ID_KEY) |