Module: Domainic::Attributer::InstanceMethods
- Defined in:
- lib/domainic/attributer/instance_methods.rb
Overview
A module providing instance-level attribute functionality.
This module defines instance methods for objects that include Domainic::Attributer. It provides initialization handling and attribute serialization capabilities, making it easy to work with attribute values in a consistent way.
Instance Method Summary collapse
-
#initialize ⇒ void
Initialize a new instance with attribute values.
-
#to_hash ⇒ Hash{Symbol => Object}
(also: #to_h)
Convert public attribute values to a hash.
Instance Method Details
#initialize ⇒ void
Initialize a new instance with attribute values.
Handles both positional arguments and keyword options, applying them to their corresponding attributes. This process includes:
-
Validating required arguments
-
Applying default values
-
Type validation and coercion
-
Change notifications
40 41 42 |
# File 'lib/domainic/attributer/instance_methods.rb', line 40 def initialize(...) DSL::Initializer.new(self).assign!(...) end |
#to_hash ⇒ Hash{Symbol => Object} Also known as: to_h
Convert public attribute values to a hash.
Creates a hash containing all public readable attributes and their current values. Any attributes marked as private or protected for reading are excluded from the result.
56 57 58 59 60 61 |
# File 'lib/domainic/attributer/instance_methods.rb', line 56 def to_hash public = self.class.send(:__attributes__).select { |_, attribute| attribute.signature.public_read? } public.attributes.each_with_object({}) do |attribute, result| result[attribute.name] = public_send(attribute.name) end end |