Sha256: 662cc7766be7729da3d34f9d041ffc8d0e7d721f3fdfc8bf2cd0846447462122

Contents?: true

Size: 1.2 KB

Versions: 6

Compression:

Stored size: 1.2 KB

Contents

module Mobility
  module Backend
=begin

Internal class used by ActiveRecord backends that store values as a hash.

=end
    class ActiveRecord::HashValued
      include ActiveRecord

      # @!group Backend Accessors
      #
      # @!macro backend_reader
      def read(locale, **_)
        translations[locale]
      end

      # @!macro backend_writer
      def write(locale, value, **_)
        translations[locale] = value
      end
      # @!endgroup

      def translations
        model.read_attribute(attribute)
      end
      alias_method :new_cache, :translations

      def write_to_cache?
        true
      end

      setup do |attributes, options|
        attributes.each { |attribute| store attribute, coder: Coder }
      end

      class Coder
        def self.dump(obj)
          if obj.is_a? Hash
            obj = obj.inject({}) do |translations, (locale, value)|
              translations[locale] = value if value.present?
              translations
            end
          else
            raise ArgumentError, "Attribute is supposed to be a Hash, but was a #{obj.class}. -- #{obj.inspect}"
          end
        end

        def self.load(obj)
          obj
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
mobility-0.1.20 lib/mobility/backend/active_record/hash_valued.rb
mobility-0.1.19 lib/mobility/backend/active_record/hash_valued.rb
mobility-0.1.18 lib/mobility/backend/active_record/hash_valued.rb
mobility-0.1.17 lib/mobility/backend/active_record/hash_valued.rb
mobility-0.1.16 lib/mobility/backend/active_record/hash_valued.rb
mobility-0.1.15 lib/mobility/backend/active_record/hash_valued.rb