Sha256: ed2bd71bc2f53025aca532fbfcd62276c3c84cdd0440250dbe2cc2f2146c2a09

Contents?: true

Size: 697 Bytes

Versions: 40

Compression:

Stored size: 697 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.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

  end

end

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
locomotivecms_wagon-3.2.0.alpha2 lib/locomotive/wagon/decorators/concerns/to_hash_concern.rb
locomotivecms_wagon-3.2.0.alpha1 lib/locomotive/wagon/decorators/concerns/to_hash_concern.rb
locomotivecms_wagon-3.1.1 lib/locomotive/wagon/decorators/concerns/to_hash_concern.rb
locomotivecms_wagon-3.1.0 lib/locomotive/wagon/decorators/concerns/to_hash_concern.rb
locomotivecms_wagon-3.1.0.beta1 lib/locomotive/wagon/decorators/concerns/to_hash_concern.rb
locomotivecms_wagon-3.0.5 lib/locomotive/wagon/decorators/concerns/to_hash_concern.rb
locomotivecms_wagon-3.0.4 lib/locomotive/wagon/decorators/concerns/to_hash_concern.rb
locomotivecms_wagon-3.0.3 lib/locomotive/wagon/decorators/concerns/to_hash_concern.rb
locomotivecms_wagon-3.0.2 lib/locomotive/wagon/decorators/concerns/to_hash_concern.rb
locomotivecms_wagon-3.0.0 lib/locomotive/wagon/decorators/concerns/to_hash_concern.rb
locomotivecms_wagon-3.0.0.rc0 lib/locomotive/wagon/decorators/concerns/to_hash_concern.rb
locomotivecms_wagon-3.0.0.beta2 lib/locomotive/wagon/decorators/concerns/to_hash_concern.rb
locomotivecms_wagon-2.4.1 lib/locomotive/wagon/decorators/concerns/to_hash_concern.rb
locomotivecms_wagon-2.4.0 lib/locomotive/wagon/decorators/concerns/to_hash_concern.rb
locomotivecms_wagon-2.4.0.rc2 lib/locomotive/wagon/decorators/concerns/to_hash_concern.rb
locomotivecms_wagon-2.4.0.rc1 lib/locomotive/wagon/decorators/concerns/to_hash_concern.rb
locomotivecms_wagon-2.3.0 lib/locomotive/wagon/decorators/concerns/to_hash_concern.rb
locomotivecms_wagon-2.3.0.rc1 lib/locomotive/wagon/decorators/concerns/to_hash_concern.rb
locomotivecms_wagon-2.2.0 lib/locomotive/wagon/decorators/concerns/to_hash_concern.rb
locomotivecms_wagon-2.2.0.rc3 lib/locomotive/wagon/decorators/concerns/to_hash_concern.rb