Sha256: 06179c1f43c407ef627306db0858f816276946e73adda818f2057216410d1f1a

Contents?: true

Size: 1.62 KB

Versions: 11

Compression:

Stored size: 1.62 KB

Contents

# encoding: utf-8

module Axiom
  module Types

    # Add encoding constraints to a type
    module Encodable
      # stub module for ruby 1.8
    end

    module Encodable

      # Hook called when module is extended
      #
      # Add #encoding DSL method to descendant and set the default to UTF-8.
      #
      # @param [Class<Axiom::Types::Type>] descendant
      #
      # @return [undefined]
      #
      # @api private
      def self.extended(descendant)
        super
        descendant.accept_options :encoding
        descendant.encoding Encoding::UTF_8
      end

      private_class_method :extended

      # Finalize by setting up a primitive constraint
      #
      # @return [Axiom::Types::Encodable]
      #
      # @api private
      def finalize
        return self if frozen?
        ascii_compatible? ? use_ascii_compatible_encoding : use_encoding
        super
      end

    private

      # Test if the encoding is ascii compatible
      #
      # @return [Boolean]
      #
      # @api private
      def ascii_compatible?
        encoding.ascii_compatible?
      end

      # Add constraint for the ascii compatible encoding
      #
      # @return [undefined]
      #
      # @api private
      def use_ascii_compatible_encoding
        constraint do |object|
          object.encoding.equal?(encoding) || object.to_s.ascii_only?
        end
      end

      # Add constraint for the encoding
      #
      # @return [undefined]
      #
      # @api private
      def use_encoding
        constraint { |object| object.encoding.equal?(encoding) }
      end

    end # module Encodable
  end # module Types
end # module Axiom

Version data entries

11 entries across 9 versions & 4 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/axiom-types-0.1.1/lib/axiom/types/encodable.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/axiom-types-0.1.1/lib/axiom/types/encodable.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/axiom-types-0.1.1/lib/axiom/types/encodable.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/axiom-types-0.1.1/lib/axiom/types/encodable.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/axiom-types-0.1.1/lib/axiom/types/encodable.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/axiom-types-0.1.1/lib/axiom/types/encodable.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/axiom-types-0.1.1/lib/axiom/types/encodable.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/axiom-types-0.1.1/lib/axiom/types/encodable.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/axiom-types-0.1.1/lib/axiom/types/encodable.rb
axiom-types-0.1.1 lib/axiom/types/encodable.rb
axiom-types-0.1.0 lib/axiom/types/encodable.rb