Sha256: b02240295330524ce837d4ff8fbdb9c8b15707590e655e88f805c151863e2239

Contents?: true

Size: 688 Bytes

Versions: 2

Compression:

Stored size: 688 Bytes

Contents

require 'locomotive/coal'

module Locomotive::Wagon

  module ToHashConcern

    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

    def prepare_value_for_hash(value)
      if value.is_a?(Array) && value.first.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

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
locomotivecms_wagon-2.0.0.pre.alpha.3 lib/locomotive/wagon/decorators/concerns/to_hash_concern.rb
locomotivecms_wagon-2.0.0.pre.alpha.2 lib/locomotive/wagon/decorators/concerns/to_hash_concern.rb