Class: ParamsReady::Marshaller::ArrayMarshallers::StringMarshaller

Inherits:
Object
  • Object
show all
Includes:
AbstractMarshaller
Defined in:
lib/params_ready/marshaller/array_marshallers.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AbstractMarshaller

#marshal

Constructor Details

#initialize(separator, split_pattern) ⇒ StringMarshaller

Returns a new instance of StringMarshaller.



99
100
101
102
# File 'lib/params_ready/marshaller/array_marshallers.rb', line 99

def initialize(separator, split_pattern)
  @separator = separator.to_s.freeze
  @split_pattern = split_pattern.freeze
end

Instance Attribute Details

#separatorObject (readonly)

Returns the value of attribute separator.



92
93
94
# File 'lib/params_ready/marshaller/array_marshallers.rb', line 92

def separator
  @separator
end

Class Method Details

.instance(separator:, split_pattern: nil) ⇒ Object



94
95
96
97
# File 'lib/params_ready/marshaller/array_marshallers.rb', line 94

def self.instance(separator:, split_pattern: nil)
  instance = new separator, split_pattern
  [String, instance.freeze]
end

Instance Method Details

#canonicalize(definition, string, context, validator) ⇒ Object



108
109
110
111
# File 'lib/params_ready/marshaller/array_marshallers.rb', line 108

def canonicalize(definition, string, context, validator)
  array = string.split(split_pattern).map(&:strip).reject(&:empty?)
  ArrayMarshaller.canonicalize(definition, array, context, validator)
end

#do_marshal(array, _, _) ⇒ Object



113
114
115
# File 'lib/params_ready/marshaller/array_marshallers.rb', line 113

def do_marshal(array, _, _)
  array.join(separator)
end

#split_patternObject



104
105
106
# File 'lib/params_ready/marshaller/array_marshallers.rb', line 104

def split_pattern
  @split_pattern || @separator
end