Sha256: f6b3a5bb3d91036749be9a0c41abc842991351818d85c9ba07a1bc4026bede1b

Contents?: true

Size: 1.27 KB

Versions: 10

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true
require "mobility/backends/sequel"
require "mobility/backends/column"

module Mobility
  module Backends
=begin

Implements the {Mobility::Backends::Column} backend for Sequel models.

=end
    class Sequel::Column
      include Sequel
      include Column

      require 'mobility/backends/sequel/column/query_methods'

      # @!group Backend Accessors
      # @!macro backend_reader
      def read(locale, _options = nil)
        column = column(locale)
        model[column] if model.columns.include?(column)
      end

      # @!group Backend Accessors
      # @!macro backend_writer
      def write(locale, value, _options = nil)
        column = column(locale)
        model[column] = value if model.columns.include?(column)
      end

      # @!macro backend_iterator
      def each_locale
        available_locales.each { |l| yield(l) if present?(l) }
      end

      setup_query_methods(QueryMethods)

      private

      def available_locales
        @available_locales ||= get_column_locales
      end

      def get_column_locales
        column_name_regex = /\A#{attribute}_([a-z]{2}(_[a-z]{2})?)\z/.freeze
        model.columns.map do |c|
          (match = c.to_s.match(column_name_regex)) && match[1].to_sym
        end.compact
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
mobility-0.7.6 lib/mobility/backends/sequel/column.rb
mobility-0.7.5 lib/mobility/backends/sequel/column.rb
mobility-0.7.4 lib/mobility/backends/sequel/column.rb
mobility-0.7.3 lib/mobility/backends/sequel/column.rb
mobility-0.7.2 lib/mobility/backends/sequel/column.rb
mobility-0.7.1 lib/mobility/backends/sequel/column.rb
mobility-0.7.0 lib/mobility/backends/sequel/column.rb
mobility-0.6.0 lib/mobility/backends/sequel/column.rb
mobility-0.5.1 lib/mobility/backends/sequel/column.rb
mobility-0.5.0 lib/mobility/backends/sequel/column.rb