Module: Hexx::Coercible

Defined in:
lib/hexx/coercible.rb

Overview

Declares the attr_coerced private class method.

Examples:


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 = "Иоанн">

Instance Method Summary collapse

Instance Method Details

#attr_coerced(*names, options) ⇒ Object (private)

Coerced the attribute(s) with given type.

Examples:


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 = "Иоанн">

Options Hash (options):

  • :type (Class)

    The class for coercion.


31
32
33
# File 'lib/hexx/coercible.rb', line 31

def attr_coerced(*names, type:)
  names.flatten.each { |name| coercion.add self, name, type }
end