module ActiveSerialize::ToH

Public Instance Methods

to_h(*groups, rmv: [ ], add: [ ], flat_add: [ ], recursive: [ ], plucked: { }, merge: { }, **opts) click to toggle source
# File lib/active_serialize.rb, line 34
def to_h(*groups, rmv: [ ], add: [ ], flat_add: [ ], recursive: [ ], plucked: { }, merge: { }, **opts)
  recursion = (_active_serialize[:recursive] + recursive).map { |k| [ k, public_send(k)&.to_ha(*groups) ] }.to_h
  merge.merge!(flat_add.map(&method(:public_send)).reduce({ }, :merge)) if flat_add.present?
  keys = active_serialize_keys(*groups, rmv: rmv, add: add, **opts)
  keys = instance_exec(&keys[:if].first) \
      ? keys[:if].last[:only] || keys[:normal] + (keys[:if].last[:add] || [ ]) - (keys[:if].last[:rmv] || [ ])
      : keys[:normal] \
    if keys.is_a?(Hash)

  KeyFormatter.(_active_serialize[:key_format],
      keys.map { |key| [ key, public_send(key) ] }.to_h
          .merge(plucked).merge(recursion).merge(merge)
          .transform_keys { |key| _active_serialize[:map][key] || key }
          .deep_stringify_keys!
  )
end
Also aliased as: to_ha
to_ha(*groups, rmv: [ ], add: [ ], flat_add: [ ], recursive: [ ], plucked: { }, merge: { }, **opts)
Alias for: to_h