Module: ActiveJson::Pluck

Extended by:
Pluck
Included in:
Pluck
Defined in:
lib/active_json/pluck.rb

Instance Method Summary collapse

Instance Method Details

#dig_attributeObject



12
13
14
# File 'lib/active_json/pluck.rb', line 12

def dig_attribute
  -> (hash, attribute) { hash.dig(attribute) if hash }
end

#new(pluck) ⇒ Object



5
6
7
8
9
10
# File 'lib/active_json/pluck.rb', line 5

def new(pluck)
  attributes = split_nested(pluck)
  -> (hash) do
    attributes ? attributes.reduce(hash, &dig_attribute) : hash
  end
end

#split_nested(pluck) ⇒ Object



16
17
18
19
# File 'lib/active_json/pluck.rb', line 16

def split_nested(pluck)
  return unless pluck
  pluck.split('.').map(&:to_sym)
end