Class: ROM::OpenStruct

Inherits:
Object
  • Object
show all
Includes:
Enumerable
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}" }
WRITER =
-> v { :"#{v}=" }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = EMPTY_HASH) ⇒ 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.



26
27
28
29
# File 'lib/rom/open_struct.rb', line 26

def initialize(attributes = EMPTY_HASH)
  @__keys__ = Set.new
  __load__(attributes)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rom/open_struct.rb', line 80

def method_missing(meth, *args, &block)
  if meth.to_s.end_with?("=")
    key = meth.to_s.tr("=", "").to_sym

    if methods.include?(key)
      super
    else
      __set__(key, *args)
    end
  elsif key?(meth)
    __get__(meth)
  else
    super
  end
end

Instance Attribute Details

#__keys__Object (readonly)

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.



23
24
25
# File 'lib/rom/open_struct.rb', line 23

def __keys__
  @__keys__
end

Instance Method Details

#[](key) ⇒ Object



53
54
55
# File 'lib/rom/open_struct.rb', line 53

def [](key)
  __send__(key)
end

#[]=(key, value) ⇒ Object



58
59
60
# File 'lib/rom/open_struct.rb', line 58

def []=(key, value)
  __set__(key, value)
end

#eachObject



32
33
34
# File 'lib/rom/open_struct.rb', line 32

def each
  __keys__.each { |key| yield(key, __get__(key)) }
end

#fetch(key, &block) ⇒ Object



48
49
50
# File 'lib/rom/open_struct.rb', line 48

def fetch(key, &block)
  to_h.fetch(key, &block)
end

#inspectObject



68
69
70
# File 'lib/rom/open_struct.rb', line 68

def inspect
  %(#<#{self.class} #{to_h}>)
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/rom/open_struct.rb', line 63

def key?(key)
  __keys__.include?(key)
end

#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)


73
74
75
# File 'lib/rom/open_struct.rb', line 73

def respond_to_missing?(meth, include_private = false)
  super || key?(meth)
end

#to_hObject Also known as: to_hash



37
38
39
# File 'lib/rom/open_struct.rb', line 37

def to_h
  map { |key, value| [key, value] }.to_h
end

#update(other) ⇒ Object



43
44
45
# File 'lib/rom/open_struct.rb', line 43

def update(other)
  __load__(other)
end