Class: ASpaceImport::RecordProxy
- Inherits:
-
Object
- Object
- ASpaceImport::RecordProxy
- Defined in:
- backend/app/converters/lib/record_proxy.rb
Overview
This object can temporarily represent a JSONModel object in streaming contexts where, for example, an object may not be ready to link to other objects (because it’s not valid yet) or may not even be ready to be an object of a particular type. The proxy can handle object creation or can just act as a store for delayed jobs.
Instance Attribute Summary (collapse)
-
- (Object) discharged
readonly
Returns the value of attribute discharged.
-
- (Object) key
readonly
Returns the value of attribute key.
Instance Method Summary (collapse)
-
- (Object) discharge(proxied_obj)
-
- (RecordProxy) initialize(key, record_type = nil)
constructor
A new instance of RecordProxy.
-
- (Object) method_missing(meth)
-
- (Object) on_discharge(receiver, method, *args)
jobs to run when discharged by the importer.
-
- (Object) on_spawn(callback)
jobs to run after spawning an object.
-
- (Object) set(k, v)
-
- (Object) spawn
-
- (Object) to_s
Constructor Details
- (RecordProxy) initialize(key, record_type = nil)
Returns a new instance of RecordProxy
53 54 55 56 57 58 59 60 61 62 |
# File 'backend/app/converters/lib/record_proxy.rb', line 53 def initialize(key, record_type = nil) @key = key @jobs = [] @spawn_callbacks = [] @discharged = false @data = {} if record_type @record_type = record_type end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
- (Object) method_missing(meth)
76 77 78 |
# File 'backend/app/converters/lib/record_proxy.rb', line 76 def method_missing(meth) @data.has_key?(meth.to_s) ? @data[meth.to_s] : nil end |
Instance Attribute Details
- (Object) discharged (readonly)
Returns the value of attribute discharged
50 51 52 |
# File 'backend/app/converters/lib/record_proxy.rb', line 50 def discharged @discharged end |
- (Object) key (readonly)
Returns the value of attribute key
51 52 53 |
# File 'backend/app/converters/lib/record_proxy.rb', line 51 def key @key end |
Instance Method Details
- (Object) discharge(proxied_obj)
124 125 126 127 |
# File 'backend/app/converters/lib/record_proxy.rb', line 124 def discharge(proxied_obj) @jobs.each {|proc| proc.call(proxied_obj) } @discharged = true end |
- (Object) on_discharge(receiver, method, *args)
jobs to run when discharged by the importer
119 120 121 |
# File 'backend/app/converters/lib/record_proxy.rb', line 119 def on_discharge(receiver, method, *args) @jobs << Proc.new {|obj| receiver.send(method, *args, obj)} end |
- (Object) on_spawn(callback)
jobs to run after spawning an object
113 114 115 |
# File 'backend/app/converters/lib/record_proxy.rb', line 113 def on_spawn(callback) @spawn_callbacks << callback end |
- (Object) set(k, v)
71 72 73 |
# File 'backend/app/converters/lib/record_proxy.rb', line 71 def set(k, v) @data[k] = v end |
- (Object) spawn
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'backend/app/converters/lib/record_proxy.rb', line 81 def spawn raise "Can't spawn an object because record type is unknown" unless @record_type type = @record_type.respond_to?(:call) ? @record_type.call(@data) : @record_type return nil unless type obj = ASpaceImport::JSONModel(type).new obj.key = @key @data.each do |k,v| next unless obj.class.schema['properties'].has_key?(k) property_type = ASpaceImport::Utils.get_property_type(obj.class.schema['properties'][k]) filtered_val = ASpaceImport::Utils.value_filter(property_type[0]).call(v) if property_type[0] =~ /_list$/ obj.send(k) << filtered_val else obj.send("#{k}=", filtered_val) end end @spawn_callbacks.each do |callback| callback.call(@data, obj) end obj end |
- (Object) to_s
65 66 67 68 |
# File 'backend/app/converters/lib/record_proxy.rb', line 65 def to_s type = @record_type && !@record_type.respond_to?(:call) ? @record_type : "Anonymous or Typeless Object" "Record Proxy for <#{type}>" end |