Class: ROM::Struct
- Inherits:
-
Dry::Struct
- Object
- Dry::Struct
- ROM::Struct
- Defined in:
- lib/rom/struct.rb
Overview
Simple data-struct class
ROM structs are plain data structures loaded by repositories. They implement Hash protocol which means that they can be used in places where Hash-like objects are supported.
Repositories define subclasses of ROM::Struct automatically, they are defined in the ROM::Struct namespace by default, but you set it up to use your namespace/module as well.
Structs are based on dry-struct gem, they include ‘schema` with detailed information about attribute types returned from relations, thus can be introspected to build additional functionality when desired.
There is a caveat you should know about when working with structs. Struct classes have names but at the same time they’re anonymous, i.e. you can’t get the User struct class with ROM::Struct::User. ROM will create as many struct classes for User as needed, they all will have the same name and ROM::Struct::User will be the common parent class for them. Combined with the ability to provide your own namespace for structs this enables to pre-define the parent class.
Constant Summary collapse
- MissingAttribute =
Class.new(NameError) do def initialize(&block) super @message_proc = block end def @message_proc.call end end
Instance Method Summary collapse
-
#fetch(name) ⇒ Object
Return attribute value.
- #respond_to_missing? ⇒ Boolean private
-
#to_s ⇒ String
Returns a short string representation.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing ⇒ Object (private)
109 110 111 112 113 |
# File 'lib/rom/struct.rb', line 109 def method_missing(*) super rescue NameError => error raise MissingAttribute.new { "#{ error. } (attribute not loaded?)" } end |
Instance Method Details
#fetch(name) ⇒ Object
Return attribute value
98 99 100 |
# File 'lib/rom/struct.rb', line 98 def fetch(name) __send__(name) end |
#respond_to_missing? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
103 104 105 |
# File 'lib/rom/struct.rb', line 103 def respond_to_missing?(*) super end |
#to_s ⇒ String
Returns a short string representation
89 90 91 |
# File 'lib/rom/struct.rb', line 89 def to_s "#<#{self.class}:0x#{(object_id << 1).to_s(16)}>" end |