Sha256: 7cfe236ed614c40d7d38b122d519ee29cfea0668a6d6644ee0f6ad3c7f0e0504

Contents?: true

Size: 1.23 KB

Versions: 11

Compression:

Stored size: 1.23 KB

Contents

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, _ = {})
        column = column(locale)
        model[column] if model.columns.include?(column)
      end

      # @!group Backend Accessors
      # @!macro backend_writer
      def write(locale, value, _ = {})
        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

11 entries across 11 versions & 1 rubygems

Version Path
mobility-0.3.6 lib/mobility/backends/sequel/column.rb
mobility-0.3.5 lib/mobility/backends/sequel/column.rb
mobility-0.3.4 lib/mobility/backends/sequel/column.rb
mobility-0.3.3 lib/mobility/backends/sequel/column.rb
mobility-0.3.2 lib/mobility/backends/sequel/column.rb
mobility-0.3.1 lib/mobility/backends/sequel/column.rb
mobility-0.3.0 lib/mobility/backends/sequel/column.rb
mobility-0.2.3 lib/mobility/backends/sequel/column.rb
mobility-0.2.2 lib/mobility/backends/sequel/column.rb
mobility-0.2.1 lib/mobility/backends/sequel/column.rb
mobility-0.2.0 lib/mobility/backends/sequel/column.rb