Module: Domainic::Attributer Abstract

Defined in:
lib/domainic/attributer.rb,
lib/domainic/attributer/attribute.rb,
lib/domainic/attributer/undefined.rb,
lib/domainic/attributer/errors/error.rb,
lib/domainic/attributer/attribute_set.rb,
lib/domainic/attributer/class_methods.rb,
lib/domainic/attributer/dsl/initializer.rb,
lib/domainic/attributer/instance_methods.rb,
lib/domainic/attributer/attribute/coercer.rb,
lib/domainic/attributer/attribute/callback.rb,
lib/domainic/attributer/attribute/signature.rb,
lib/domainic/attributer/attribute/validator.rb,
lib/domainic/attributer/dsl/method_injector.rb,
lib/domainic/attributer/dsl/attribute_builder.rb,
lib/domainic/attributer/errors/aggregate_error.rb,
lib/domainic/attributer/errors/callback_execution_error.rb,
lib/domainic/attributer/errors/coercion_execution_error.rb,
lib/domainic/attributer/errors/validation_execution_error.rb,
lib/domainic/attributer/dsl/attribute_builder/option_parser.rb,
lib/domainic/attributer/attribute/mixin/belongs_to_attribute.rb

Overview

This module is abstract.

Can be included directly with default method names or customized via Attributer

Domainic::Attributer is a powerful toolkit that brings clarity and safety to your Ruby class attributes. Ever wished your class attributes could:

  • Validate themselves to ensure they only accept correct values?
  • Transform input data automatically into the right format?
  • Have clear, enforced visibility rules?
  • Handle their own default values intelligently?
  • Tell you when they change?
  • Distinguish between required arguments and optional settings?

That's exactly what Domainic::Attributer does! It provides a declarative way to define and manage attributes in your Ruby classes, ensuring data integrity and clear interfaces. It's particularly valuable for:

  • Domain models and value objects
  • Service objects and command patterns
  • Configuration objects
  • Any class where attribute behavior matters

Think of it as giving your attributes a brain - they know what they want, how they should behave, and they're not afraid to speak up when something's not right!

Examples:

Basic usage

class SuperDev
  include Domainic::Attributer

  argument :code_name, String

  option :power_level, Integer, default: 9000
  option :favorite_gem do
    validate_with ->(val) { val.to_s.end_with?('ruby') }
    coerce_with ->(val) { val.to_s.downcase }
    non_nilable
  end
end

dev = SuperDev.new('RubyNinja', favorite_gem: 'RAILS_RUBY')
# => #<SuperDev:0x00000001083aeb58 @code_name="RubyNinja", @favorite_gem="rails_ruby", @power_level=9000>

dev.favorite_gem # => "rails_ruby"
dev.power_level = 9001 # => 9001
dev.power_level = 'over 9000'
# `SuperDev#power_level`: has invalid value: "over 9000" (ArgumentError)

See Also:

Author:

Since:

  • 0.1.0

Defined Under Namespace

Modules: ClassMethods, DSL, InstanceMethods Classes: CallbackExecutionError, CoercionExecutionError, ValidationExecutionError