Sha256: bbd94c62fb95247354e07f215a12186ffbba478492ff43c9d51108a28d73335c

Contents?: true

Size: 1.56 KB

Versions: 17

Compression:

Stored size: 1.56 KB

Contents

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

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

17 entries across 17 versions & 1 rubygems

Version Path
mobility-1.0.0.beta2 lib/mobility/backends/sequel/pg_hash.rb
mobility-1.0.0.beta1 lib/mobility/backends/sequel/pg_hash.rb
mobility-1.0.0.alpha lib/mobility/backends/sequel/pg_hash.rb
mobility-0.8.13 lib/mobility/backends/sequel/pg_hash.rb
mobility-0.8.11 lib/mobility/backends/sequel/pg_hash.rb
mobility-0.8.10 lib/mobility/backends/sequel/pg_hash.rb
mobility-0.8.9 lib/mobility/backends/sequel/pg_hash.rb
mobility-0.8.8 lib/mobility/backends/sequel/pg_hash.rb
mobility-0.8.7 lib/mobility/backends/sequel/pg_hash.rb
mobility-0.8.6 lib/mobility/backends/sequel/pg_hash.rb
mobility-0.8.5 lib/mobility/backends/sequel/pg_hash.rb
mobility-0.8.4 lib/mobility/backends/sequel/pg_hash.rb
mobility-0.8.3 lib/mobility/backends/sequel/pg_hash.rb
mobility-0.8.2 lib/mobility/backends/sequel/pg_hash.rb
mobility-0.8.1 lib/mobility/backends/sequel/pg_hash.rb
mobility-0.8.0 lib/mobility/backends/sequel/pg_hash.rb
mobility-0.7.6 lib/mobility/backends/sequel/pg_hash.rb