Sha256: 645fac97ff640012b72c33c180c379715d1f63c1dcf48cdac07a3f20df10adef

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 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 coersion options.
    # @option options [Class] :type The class for coersion.
    def attr_coerced(*names, type:)
      names.flatten.each { |name| coersion.add self, name, type }
    end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hexx-6.0.1 lib/hexx/coercible.rb