Sha256: c1e67b2ba8343a030eabf6f4ef31a0ac25285d585a924bd8043038b0bd9cbd7f

Contents?: true

Size: 1.22 KB

Versions: 20

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true
require "mobility/backends/active_record"
require "mobility/backends/hash_valued"

module Mobility
  module Backends
=begin

Internal class used by ActiveRecord backends backed by a Postgres data type
(hstore, jsonb).

=end
    module ActiveRecord
      class PgHash
        include ActiveRecord
        include HashValued

        # @!macro backend_iterator
        def each_locale
          super { |l| yield l.to_sym }
        end

        def translations
          model.read_attribute(column_name)
        end

        setup do |attributes, options = {}|
          attributes.each { |attribute| store (options[:column_affix] % attribute), coder: Coder }
        end

        class Coder
          def self.dump(obj)
            if obj.is_a? Hash
              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
      private_constant :PgHash
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
mobility-0.8.13 lib/mobility/backends/active_record/pg_hash.rb
mobility-0.8.11 lib/mobility/backends/active_record/pg_hash.rb
mobility-0.8.10 lib/mobility/backends/active_record/pg_hash.rb
mobility-0.8.9 lib/mobility/backends/active_record/pg_hash.rb
mobility-0.8.8 lib/mobility/backends/active_record/pg_hash.rb
mobility-0.8.7 lib/mobility/backends/active_record/pg_hash.rb
mobility-0.8.6 lib/mobility/backends/active_record/pg_hash.rb
mobility-0.8.5 lib/mobility/backends/active_record/pg_hash.rb
mobility-0.8.4 lib/mobility/backends/active_record/pg_hash.rb
mobility-0.8.3 lib/mobility/backends/active_record/pg_hash.rb
mobility-0.8.2 lib/mobility/backends/active_record/pg_hash.rb
mobility-0.8.1 lib/mobility/backends/active_record/pg_hash.rb
mobility-0.8.0 lib/mobility/backends/active_record/pg_hash.rb
mobility-0.7.6 lib/mobility/backends/active_record/pg_hash.rb
mobility-0.7.5 lib/mobility/backends/active_record/pg_hash.rb
mobility-0.7.4 lib/mobility/backends/active_record/pg_hash.rb
mobility-0.7.3 lib/mobility/backends/active_record/pg_hash.rb
mobility-0.7.2 lib/mobility/backends/active_record/pg_hash.rb
mobility-0.7.1 lib/mobility/backends/active_record/pg_hash.rb
mobility-0.7.0 lib/mobility/backends/active_record/pg_hash.rb