Sha256: dae6c5bb67d94a312aad604f1eb6ed4257534c3973b1346b9267fb1b67ebc8dc

Contents?: true

Size: 1.51 KB

Versions: 6

Compression:

Stored size: 1.51 KB

Contents

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

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|
          columns = attributes.map { |attribute| (options[:column_affix] % attribute).to_sym }

          before_validation = Module.new do
            define_method :before_validation do
              columns.each do |column|
                self[column].delete_if { |_, v| Util.blank?(v) }
              end
              super()
            end
          end
          include before_validation
          include Mobility::Sequel::HashInitializer.new(*columns)
          include Mobility::Sequel::ColumnChanges.new(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

6 entries across 6 versions & 1 rubygems

Version Path
mobility-0.7.5 lib/mobility/backends/sequel/pg_hash.rb
mobility-0.7.4 lib/mobility/backends/sequel/pg_hash.rb
mobility-0.7.3 lib/mobility/backends/sequel/pg_hash.rb
mobility-0.7.2 lib/mobility/backends/sequel/pg_hash.rb
mobility-0.7.1 lib/mobility/backends/sequel/pg_hash.rb
mobility-0.7.0 lib/mobility/backends/sequel/pg_hash.rb