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.



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

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.



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

def separator
  @separator
end

Class Method Details

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



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

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



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

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



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

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

#split_patternObject



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

def split_pattern
  @split_pattern || @separator
end