Class: ROM::OpenStruct

Inherits:
Object
  • Object
show all
Defined in:
lib/rom/open_struct.rb

Overview

ROM's open structs are used for relations with empty schemas. Such relations may exist in cases like using raw SQL strings where schema was not explicitly defined using view DSL.

Constant Summary collapse

IVAR =
-> v { :"@#{v}" }

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ OpenStruct

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.

Returns a new instance of OpenStruct.


13
14
15
16
17
# File 'lib/rom/open_struct.rb', line 13

def initialize(attributes)
  attributes.each do |key, value|
    instance_variable_set(IVAR[key], value)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object (private)

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.


27
28
29
30
31
32
33
34
35
# File 'lib/rom/open_struct.rb', line 27

def method_missing(meth, *args, &block)
  ivar = IVAR[meth]

  if instance_variables.include?(ivar)
    instance_variable_get(ivar)
  else
    super
  end
end

Instance Method Details

#respond_to_missing?(meth, include_private = false) ⇒ 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.

Returns:

  • (Boolean)

20
21
22
# File 'lib/rom/open_struct.rb', line 20

def respond_to_missing?(meth, include_private = false)
  super || instance_variables.include?(IVAR[meth])
end