class IknowParams::Serializer::ArrayOf
Serialize an array of application types to JSON types. Does not support fully rendering to/from a JSON string.
Attributes
allow_singleton[R]
serializer[R]
Public Class Methods
new(serializer, allow_singleton: false)
click to toggle source
Calls superclass method
IknowParams::Serializer::new
# File lib/iknow_params/serializer/array_of.rb, line 9 def initialize(serializer, allow_singleton: false) super(::Array) @serializer = serializer @allow_singleton = allow_singleton end
Public Instance Methods
dump(vals, json: true)
click to toggle source
# File lib/iknow_params/serializer/array_of.rb, line 34 def dump(vals, json: true) matches_type!(vals) vals.map { |val| serializer.dump(val, json: json) } end
load(jvals)
click to toggle source
# File lib/iknow_params/serializer/array_of.rb, line 15 def load(jvals) if allow_singleton jvals = Array.wrap(jvals) else unless jvals.is_a?(Array) raise IknowParams::Serializer::LoadError.new( "Incorrect type for ArrayOf: #{jvals.inspect}:#{jvals.class.name} is not an array") end end # Special case thanks to Rails' array query param format not differentating # empty arrays (i.e. `route?param[]=`). Since we can only express one of # empty array and singleton array containing empty string, we pick the more # useful former. return [] if jvals == [''] jvals.map { |jval| serializer.load(jval) } end
matches_type?(vals)
click to toggle source
Calls superclass method
IknowParams::Serializer#matches_type?
# File lib/iknow_params/serializer/array_of.rb, line 39 def matches_type?(vals) super(vals) && vals.all? { |val| serializer.matches_type?(val) } end