Sha256: a0cab959a941d46b7b47a31976bc9c094e1cf0e3e9f9b1cf630bceeac45aabc5

Contents?: true

Size: 1.16 KB

Versions: 10

Compression:

Stored size: 1.16 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::Models
  #     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 Models

    private

    # @!method attr_coerced(*names, options)
    # Coerced the attribute(s) with given type.
    # @example (see Hexx::Models)
    # @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.each { |name| coercer.new(self, name, type).coerce }
    end

    # @api hide
    # Applied coercer for attributes.
    # @note The method is reloaded in the 'hexx-active_record' gem to allow
    #   coersion of ActiveRecord models' attributes.
    def coercer
      @coercer ||= BaseCoercer
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
hexx-5.4.0 lib/hexx/models.rb
hexx-5.3.0 lib/hexx/models.rb
hexx-5.2.0 lib/hexx/models.rb
hexx-5.1.0 lib/hexx/models.rb
hexx-5.0.0 lib/hexx/models.rb
hexx-4.0.0 lib/hexx/models.rb
hexx-3.2.1 lib/hexx/models.rb
hexx-3.2.0 lib/hexx/models.rb
hexx-3.1.0 lib/hexx/models.rb
hexx-3.0.0 lib/hexx/models.rb