Sha256: 235f0c4831757a6cd1a3f0b975c40145b36b98746a81634f31e1e5c2cd0b82fc

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 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.

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

=end
    module Presence
      extend Plugin

      default true
      requires :backend, include: :before

      # Applies presence plugin to attributes.
      included_hook do |_, backend_class|
        backend_class.include(BackendMethods) if options[:presence]
      end

      module BackendMethods
        # @!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
      end

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

    register_plugin(:presence, Presence)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mobility-1.0.0.alpha lib/mobility/plugins/presence.rb