Sha256: a875187142e46f19ed7e7c214f8f37d084365b7f2a5a77021ee19d5fc6b4766a

Contents?: true

Size: 1.3 KB

Versions: 20

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true
require "mobility/util"

module Mobility
  module Plugins
=begin

Applies presence filter to values fetched from backend and to values set on
backend. Included by default, but can be disabled with +presence: false+ option.

@note For performance reasons, the presence plugin filters only for empty
  strings, not other values continued "blank" like empty arrays.

=end
    module Presence
      # Applies presence plugin to attributes.
      # @param [Attributes] attributes
      # @param [Boolean] option
      def self.apply(attributes, option)
        attributes.backend_class.include(self) if option
      end

      # @!group Backend Accessors
      # @!macro backend_reader
      # @option options [Boolean] presence
      #   *false* to disable presence filter.
      def read(locale, **options)
        options.delete(:presence) == false ? super : Presence[super]
      end

      # @!macro backend_writer
      # @option options [Boolean] presence
      #   *false* to disable presence filter.
      def write(locale, value, **options)
        if options.delete(:presence) == false
          super
        else
          super(locale, Presence[value], options)
        end
      end
      # @!endgroup

      def self.[](value)
        (value == "") ? nil : value
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
mobility-0.8.13 lib/mobility/plugins/presence.rb
mobility-0.8.11 lib/mobility/plugins/presence.rb
mobility-0.8.10 lib/mobility/plugins/presence.rb
mobility-0.8.9 lib/mobility/plugins/presence.rb
mobility-0.8.8 lib/mobility/plugins/presence.rb
mobility-0.8.7 lib/mobility/plugins/presence.rb
mobility-0.8.6 lib/mobility/plugins/presence.rb
mobility-0.8.5 lib/mobility/plugins/presence.rb
mobility-0.8.4 lib/mobility/plugins/presence.rb
mobility-0.8.3 lib/mobility/plugins/presence.rb
mobility-0.8.2 lib/mobility/plugins/presence.rb
mobility-0.8.1 lib/mobility/plugins/presence.rb
mobility-0.8.0 lib/mobility/plugins/presence.rb
mobility-0.7.6 lib/mobility/plugins/presence.rb
mobility-0.7.5 lib/mobility/plugins/presence.rb
mobility-0.7.4 lib/mobility/plugins/presence.rb
mobility-0.7.3 lib/mobility/plugins/presence.rb
mobility-0.7.2 lib/mobility/plugins/presence.rb
mobility-0.7.1 lib/mobility/plugins/presence.rb
mobility-0.7.0 lib/mobility/plugins/presence.rb