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
111
112
113
114
115
116
117
|
# File 'lib/odba/stub.rb', line 111
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
136
137
138
|
# File 'lib/odba/stub.rb', line 136
def ==(other)
@odba_id == other.odba_id || odba_instance == other
end
|
#[](*args, &block) ⇒ Object
FIXME
implement full hash/array access - separate collection stub?
130
131
132
133
134
135
|
# File 'lib/odba/stub.rb', line 130
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
|
16
17
18
|
# File 'lib/odba/stub.rb', line 16
def class
@odba_class ||= odba_instance.class
end
|
#eql?(other) ⇒ Boolean
19
20
21
|
# File 'lib/odba/stub.rb', line 19
def eql?(other)
@odba_id == other.odba_id || odba_instance.eql?(other)
end
|
22
23
24
|
# File 'lib/odba/stub.rb', line 22
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
25
26
27
28
|
# File 'lib/odba/stub.rb', line 25
def is_a?(klass)
klass == Stub || klass == Persistable || klass == @odba_class \
|| odba_instance.is_a?(klass)
end
|
#odba_clear_receiver ⇒ Object
29
30
31
32
|
# File 'lib/odba/stub.rb', line 29
def odba_clear_receiver
@receiver = nil
@receiver_loaded = nil
end
|
33
34
35
|
# File 'lib/odba/stub.rb', line 33
def odba_dup
odba_isolated_stub
end
|
#odba_isolated_stub ⇒ Object
36
37
38
|
# File 'lib/odba/stub.rb', line 36
def odba_isolated_stub
Stub.new(@odba_id, nil, nil)
end
|
#odba_prefetch? ⇒ Boolean
39
40
41
|
# File 'lib/odba/stub.rb', line 39
def odba_prefetch?
false
end
|
#odba_receiver(name = nil) ⇒ Object
Also known as:
odba_instance
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/odba/stub.rb', line 42
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 => e
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.
67
68
69
|
# File 'lib/odba/stub.rb', line 67
def odba_unsaved?(snapshot_level=nil)
false
end
|
#respond_to?(msg_id, *args) ⇒ Boolean
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/odba/stub.rb', line 118
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
70
71
72
|
# File 'lib/odba/stub.rb', line 70
def to_yaml_properties
['@odba_id', '@odba_container']
end
|
#to_yaml_type ⇒ Object
73
74
75
|
# File 'lib/odba/stub.rb', line 73
def to_yaml_type
"!ruby/object:ODBA::Stub"
end
|
#yaml_initialize(tag, val) ⇒ Object
76
77
78
79
80
81
82
|
# File 'lib/odba/stub.rb', line 76
def yaml_initialize(tag, val)
if RUBY_VERSION >= '1.9'
val.each { |key, value| instance_variable_set(:"@#{key}", value) }
else
val.each { |key, value| instance_variable_set("@#{key}", value) }
end
end
|