Sha256: 5220d41cecb02cbf6f5fa4843e985db96252e9467e781b297ff5dafca7784cc5

Contents?: true

Size: 1.41 KB

Versions: 19

Compression:

Stored size: 1.41 KB

Contents

module Mobility
  module Backend
=begin

Stores translations as serialized attributes in a single text column. This
implies that the translated values are not searchable, and thus this backend is
not recommended unless specific constraints prevent use of other solutions.

To use this backend, ensure that the model table has a text column on its table
with the same name as the translated attribute.

==Backend Options

===+format+

Format for serialization. Either +:yaml+ (default) or +:json+.

@see Mobility::Backend::ActiveRecord::Serialized
@see Mobility::Backend::Sequel::Serialized

=end
    module Serialized
      include OrmDelegator

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

            obj.send("to_#{format}")
          end
        end

        def deserializer_for(format)
          case format
          when :yaml
            lambda { |v| YAML.load(v) }
          when :json
            lambda { |v| JSON.parse(v, symbolize_names: true) }
          end
        end
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
mobility-0.1.20 lib/mobility/backend/serialized.rb
mobility-0.1.19 lib/mobility/backend/serialized.rb
mobility-0.1.18 lib/mobility/backend/serialized.rb
mobility-0.1.17 lib/mobility/backend/serialized.rb
mobility-0.1.16 lib/mobility/backend/serialized.rb
mobility-0.1.15 lib/mobility/backend/serialized.rb
mobility-0.1.14 lib/mobility/backend/serialized.rb
mobility-0.1.13 lib/mobility/backend/serialized.rb
mobility-0.1.12 lib/mobility/backend/serialized.rb
mobility-0.1.11 lib/mobility/backend/serialized.rb
mobility-0.1.10 lib/mobility/backend/serialized.rb
mobility-0.1.7 lib/mobility/backend/serialized.rb
mobility-0.1.6 lib/mobility/backend/serialized.rb
mobility-0.1.5 lib/mobility/backend/serialized.rb
mobility-0.1.4 lib/mobility/backend/serialized.rb
mobility-0.1.3 lib/mobility/backend/serialized.rb
mobility-0.1.2 lib/mobility/backend/serialized.rb
mobility-0.1.1 lib/mobility/backend/serialized.rb
mobility-0.1.0 lib/mobility/backend/serialized.rb