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.inject({}) {|acc, field| acc.merge(field => 1)}
      end
    when Symbol
      {value => 1}
    when String
      value.split(',').inject({}) { |acc, v| acc.merge(v.strip => 1) }
    else
      value
  end
end