Sha256: 67562745054a08d9c9404069c651309ba7afabf28ef2541aaf13c36dada49136

Contents?: true

Size: 1.45 KB

Versions: 15

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true
require "mobility/util"
require "mobility/backends/sequel"
require "mobility/backends/hash_valued"

module Mobility
  module Backends
=begin

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

=end
    module Sequel
      class PgHash
        include Sequel
        include HashValued

        def read(locale, options = {})
          super(locale.to_s, options)
        end

        def write(locale, value, options = {})
          super(locale.to_s, value, options)
        end

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

        def translations
          model[column_name.to_sym]
        end

        setup do |attributes, options, backend_class|
          columns = attributes.map { |attribute| (options[:column_affix] % attribute).to_sym }

          mod = Module.new do
            define_method :before_validation do
              columns.each do |column|
                self[column].delete_if { |_, v| v.nil? }
              end
              super()
            end
          end
          include mod
          backend_class.define_hash_initializer(mod, columns)
          backend_class.define_column_changes(mod, attributes, column_affix: options[:column_affix])

          plugin :defaults_setter
          columns.each { |column| default_values[column] = {} }
        end
      end
      private_constant :PgHash
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
mobility-1.3.1 lib/mobility/backends/sequel/pg_hash.rb
mobility-1.3.0 lib/mobility/backends/sequel/pg_hash.rb
mobility-1.3.0.rc3 lib/mobility/backends/sequel/pg_hash.rb
mobility-1.3.0.rc2 lib/mobility/backends/sequel/pg_hash.rb
mobility-1.3.0.rc1 lib/mobility/backends/sequel/pg_hash.rb
mobility-1.2.9 lib/mobility/backends/sequel/pg_hash.rb
mobility-1.2.8 lib/mobility/backends/sequel/pg_hash.rb
mobility-1.2.7 lib/mobility/backends/sequel/pg_hash.rb
mobility-1.2.6 lib/mobility/backends/sequel/pg_hash.rb
mobility-1.2.5 lib/mobility/backends/sequel/pg_hash.rb
mobility-1.2.4 lib/mobility/backends/sequel/pg_hash.rb
mobility-1.2.3 lib/mobility/backends/sequel/pg_hash.rb
mobility-1.2.2 lib/mobility/backends/sequel/pg_hash.rb
mobility-1.2.1 lib/mobility/backends/sequel/pg_hash.rb
mobility-1.2.0 lib/mobility/backends/sequel/pg_hash.rb