Sha256: 5f4f1bfb014c14d3f7f2b5a949e86568350d8ebfab5297228150d24cf69f7708

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 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

    # @api hide
    def self.extended(klass)
      klass.send :extend, Configurable
      klass.send :depends_on, :coersion, default: Helpers::Coersion
    end

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

Version data entries

1 entries across 1 versions & 1 rubygems

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