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 not defined as constants in any module, instead, generated mappers are configured to use anonymous struct classes as models.
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.
Instance Method Summary collapse
-
#fetch(name) ⇒ Object
Return attribute value.
-
#to_s ⇒ String
Returns a short string representation.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args) ⇒ Object (private)
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/rom/struct.rb', line 68 def method_missing(m, *args) inspected = inspect trace = caller # This is how MRI currently works # see func name_err_mesg_to_str in error.c name = inspected.size > 65 ? to_s : inspected raise NoMethodError.new("undefined method `#{ m }' for #{ name }", m, args).tap { |e| e.set_backtrace(trace) } end |
Instance Method Details
#fetch(name) ⇒ Object
Return attribute value
62 63 64 |
# File 'lib/rom/struct.rb', line 62 def fetch(name) __send__(name) end |
#to_s ⇒ String
Returns a short string representation
53 54 55 |
# File 'lib/rom/struct.rb', line 53 def to_s "#<#{self.class}:0x#{(object_id << 1).to_s(16)}>" end |