Sha256: c5c464ddd2d41d976c75f789ff99cf8414e5a0d032e85d0dd4a8ce6a426a691b

Contents?: true

Size: 1.19 KB

Versions: 2

Compression:

Stored size: 1.19 KB

Contents

# encoding: utf-8

module Hexx

  # Declares the +attr_coerced+ private class method.
  #
  # @example
  #
  #   require "hexx"
  #   require_relative "attributes/string"
  #
  #   class User
  #     extend Hexx::Coercible
  #     attr_coerced :name, type: ActiveSupport::Multibyte::Chars
  #   end
  #
  #   # This will coerce user name with mb chars:
  #   user = User.new name: "Иоанн"
  #   user.name
  #   # => #<ActiveSupport::Multibyte::Chars @wrapped_string = "Иоанн">
  module Coercible

    private

    # @!method attr_coerced(*names, options)
    # Coerced the attribute(s) with given type.
    # @example (see Hexx::Coercible)
    # @param [Array<Symbol, String>] names The list of attributes to be coerced.
    # @param [Hash] options The coercion options.
    # @option options [Class] :type The class for coercion.
    def attr_coerced(*names, type:)
      names.flatten.each { |name| coercion.add self, name, type }
    end

    # @api hide
    # The method returns a coercion creator for PORO object only.
    # To be reloaded for ActiveRecord models
    # @return [Class] attribute coercion (Hexx::Helpers::Coercion by default)
    def coercion
      Hexx::Helpers::Coercion
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hexx-6.0.3 lib/hexx/coercible.rb
hexx-6.0.2 lib/hexx/coercible.rb