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 ⇒ InstanceMethods
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 ⇒ InstanceMethods
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
43 44 45 |
# File 'lib/domainic/attributer/instance_methods.rb', line 43 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
59 60 61 62 63 64 |
# File 'lib/domainic/attributer/instance_methods.rb', line 59 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 |