module Locomotive::Wagon::ToHashConcern

Public Instance Methods

prepare_value_for_hash(value) click to toggle source
# File lib/locomotive/wagon/decorators/concerns/to_hash_concern.rb, line 19
def prepare_value_for_hash(value)
  if value.is_a?(Array) && value.any? { |v| v.respond_to?(:__attributes__) }
    value.map(&:to_hash)
  elsif value.is_a?(Array) && value.empty?
    nil # reset the array
  elsif value.respond_to?(:translations)
    !value.translations.empty? ? value.translations : nil
  else
    value
  end
end
to_hash() click to toggle source
# File lib/locomotive/wagon/decorators/concerns/to_hash_concern.rb, line 7
def to_hash
  {}.tap do |hash|
    __attributes__.each do |name|
      value = self.public_send(name)

      next if value.nil?

      hash[name] = prepare_value_for_hash(value)
    end
  end
end