Class: ODBA::Stub
- Inherits:
-
Object
show all
- Defined in:
- lib/odba/stub.rb,
lib/odba/persistable.rb
Overview
Constant Summary
collapse
- NO_OVERRIDE =
no_override.collect { |name| name.to_sym }
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(odba_id, odba_container, receiver) ⇒ Stub
Returns a new instance of Stub.
10
11
12
13
14
15
|
# File 'lib/odba/stub.rb', line 10
def initialize(odba_id, odba_container, receiver)
@odba_id = odba_id
@odba_container = odba_container
@odba_class = receiver.class unless receiver.nil?
@receiver_loaded = true
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth_symbol, *args, &block) ⇒ Object
117
118
119
120
121
122
123
|
# File 'lib/odba/stub.rb', line 117
def method_missing(meth_symbol, *args, &block)
if NO_OVERRIDE.include?(meth_symbol)
super
else
odba_instance.send(meth_symbol, *args, &block)
end
end
|
Instance Attribute Details
#odba_container ⇒ Object
Returns the value of attribute odba_container.
9
10
11
|
# File 'lib/odba/stub.rb', line 9
def odba_container
@odba_container
end
|
Returns the value of attribute odba_id.
9
10
11
|
# File 'lib/odba/stub.rb', line 9
def odba_id
@odba_id
end
|
Instance Method Details
#==(other) ⇒ Object
145
146
147
|
# File 'lib/odba/stub.rb', line 145
def ==(other)
@odba_id == other.odba_id || odba_instance == other
end
|
#[](*args, &block) ⇒ Object
FIXME
implement full hash/array access - separate collection stub?
138
139
140
141
142
143
|
# File 'lib/odba/stub.rb', line 138
def [](*args, &block)
if @odba_class == Hash \
&& !ODBA.cache.include?(@odba_id)
ODBA.cache.fetch_collection_element(@odba_id, args.first)
end || method_missing(:[], *args, &block)
end
|
17
18
19
|
# File 'lib/odba/stub.rb', line 17
def class
@odba_class ||= odba_instance.class
end
|
#eql?(other) ⇒ Boolean
21
22
23
|
# File 'lib/odba/stub.rb', line 21
def eql?(other)
@odba_id == other.odba_id || odba_instance.eql?(other)
end
|
25
26
27
|
# File 'lib/odba/stub.rb', line 25
def inspect
"#<ODBA::Stub:#{object_id}##{@odba_id} @odba_class=#{@odba_class} @odba_container=#{@odba_container.object_id}##{@odba_container.odba_id}>"
end
|
#is_a?(klass) ⇒ Boolean
29
30
31
32
|
# File 'lib/odba/stub.rb', line 29
def is_a?(klass)
klass == Stub || klass == Persistable || klass == @odba_class \
|| odba_instance.is_a?(klass)
end
|
#odba_clear_receiver ⇒ Object
34
35
36
37
|
# File 'lib/odba/stub.rb', line 34
def odba_clear_receiver
@receiver = nil
@receiver_loaded = nil
end
|
39
40
41
|
# File 'lib/odba/stub.rb', line 39
def odba_dup
odba_isolated_stub
end
|
#odba_isolated_stub ⇒ Object
43
44
45
|
# File 'lib/odba/stub.rb', line 43
def odba_isolated_stub
Stub.new(@odba_id, nil, nil)
end
|
#odba_prefetch? ⇒ Boolean
47
48
49
|
# File 'lib/odba/stub.rb', line 47
def odba_prefetch?
false
end
|
#odba_receiver(name = nil) ⇒ Object
Also known as:
odba_instance
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/odba/stub.rb', line 51
def odba_receiver(name = nil)
if @receiver && !@receiver_loaded
warn "stub for #{@receiver.class}:#{@odba_id} was saved with receiver"
@receiver = nil
end
@receiver || begin
@receiver = ODBA.cache.fetch(@odba_id, @odba_container)
@receiver_loaded = true
if @odba_container
@odba_container.odba_replace_stubs(@odba_id, @receiver)
else
warn "Potential Memory-Leak: stub for #{@receiver.class}##{@odba_id} was saved without container"
end
@receiver
rescue OdbaError
puts "OdbaError"
puts caller[0..10].join("\n")
warn "ODBA::Stub was unable to replace #{@odba_class}##{@odba_id} from #{@odba_container.class}:##{@odba_container.odba_id}. raise OdbaError"
raise OdbaError
end
end
|
#odba_unsaved?(snapshot_level = nil) ⇒ Boolean
A stub always references a Persistable that has
already been saved.
76
77
78
|
# File 'lib/odba/stub.rb', line 76
def odba_unsaved?(snapshot_level = nil)
false
end
|
#respond_to?(msg_id, *args) ⇒ Boolean
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/odba/stub.rb', line 125
def respond_to?(msg_id, *args)
case msg_id
when :_dump, :marshal_dump
false
when *NO_OVERRIDE
super
else
odba_instance.respond_to?(msg_id, *args)
end
end
|
#to_yaml_properties ⇒ Object
80
81
82
|
# File 'lib/odba/stub.rb', line 80
def to_yaml_properties
["@odba_id", "@odba_container"]
end
|
#to_yaml_type ⇒ Object
84
85
86
|
# File 'lib/odba/stub.rb', line 84
def to_yaml_type
"!ruby/object:ODBA::Stub"
end
|
#yaml_initialize(tag, val) ⇒ Object
88
89
90
|
# File 'lib/odba/stub.rb', line 88
def yaml_initialize(tag, val)
val.each { |key, value| instance_variable_set(:"@#{key}", value) }
end
|