Class: Plucky::Normalizers::FieldsValue

Inherits:
Object
  • Object
show all
Defined in:
lib/plucky/normalizers/fields_value.rb

Instance Method Summary collapse

Instance Method Details

#call(value) ⇒ Object

Public: Given a value returns it normalized for Mongo’s fields option



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/plucky/normalizers/fields_value.rb', line 6

def call(value)
  return nil if value.respond_to?(:empty?) && value.empty?

  case value
    when Array
      if value.size == 1 && value.first.is_a?(Hash)
        value.first
      else
        value.flatten
      end
    when Symbol
      [value]
    when String
      value.split(',').map { |v| v.strip }
    else
      value
  end
end