Class: ASpaceImport::RecordProxy

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(key, record_type = nil) ⇒ RecordProxy

Returns a new instance of RecordProxy.



52
53
54
55
56
57
58
59
60
61
# File 'backend/app/converters/lib/record_proxy.rb', line 52

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

#method_missing(meth) ⇒ Object



75
76
77
# File 'backend/app/converters/lib/record_proxy.rb', line 75

def method_missing(meth)
  @data.has_key?(meth.to_s) ? @data[meth.to_s] : nil
end

Instance Attribute Details

#dischargedObject (readonly)

Returns the value of attribute discharged



49
50
51
# File 'backend/app/converters/lib/record_proxy.rb', line 49

def discharged
  @discharged
end

#keyObject (readonly)

Returns the value of attribute key



50
51
52
# File 'backend/app/converters/lib/record_proxy.rb', line 50

def key
  @key
end

Instance Method Details

#discharge(proxied_obj) ⇒ Object



122
123
124
125
# File 'backend/app/converters/lib/record_proxy.rb', line 122

def discharge(proxied_obj)
  @jobs.each {|proc| proc.call(proxied_obj) }
  @discharged = true
end

#on_discharge(receiver, method, *args) ⇒ Object

jobs to run when discharged by the importer



117
118
119
# File 'backend/app/converters/lib/record_proxy.rb', line 117

def on_discharge(receiver, method, *args)
  @jobs << Proc.new {|obj| receiver.send(method, *args, obj)}
end

#on_spawn(callback) ⇒ Object

jobs to run after spawning an object



111
112
113
# File 'backend/app/converters/lib/record_proxy.rb', line 111

def on_spawn(callback)
  @spawn_callbacks << callback
end

#set(k, v) ⇒ Object



70
71
72
# File 'backend/app/converters/lib/record_proxy.rb', line 70

def set(k, v)
  @data[k] = v
end

#spawnObject



80
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
# File 'backend/app/converters/lib/record_proxy.rb', line 80

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

#to_sObject



64
65
66
67
# File 'backend/app/converters/lib/record_proxy.rb', line 64

def to_s
  type = @record_type && !@record_type.respond_to?(:call) ? @record_type : "Anonymous or Typeless Object"
  "Record Proxy for <#{type}>"
end