Module: ASpaceImport::Utils

Defined in:
backend/app/converters/lib/utils.rb

Defined Under Namespace

Classes: ASpaceImportException

Class Method Summary (collapse)

Class Method Details

+ (Object) get_property_type(property_def)



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'backend/app/converters/lib/utils.rb', line 12

def self.get_property_type(property_def)
  # subrecord slots taking more than one type
  if property_def['type'].is_a? Array
    if ((property_def['type'] | ["integer", "string"]) - (property_def['type'] & ["integer", "string"])).empty?
      return [:string_or_integer, nil]
    elsif property_def['type'].reject {|t| t['type'].match(/object$/)}.length != 0
      raise ASpaceImportException.new(:property_def => property_def)
    end

    return [:record_inline, property_def['type'].map {|t| t['type'].scan(/:([a-zA-Z_]*)/)[0][0] }]
  end

  # dynamic enums

  if property_def['type'] == 'string' && property_def.has_key?('dynamic_enum')
    return [:dynamic_enum, nil]
  end

  # all other cases

  case property_def['type']

  when 'boolean'
    [:boolean, nil]

  when 'integer'
    [:integer, nil]

  when 'date'
    [:string, nil]

  when 'string'
    [:string, nil]

  when 'object'
    if property_def['subtype'] == 'ref'
      [:record_ref, ref_type_list(property_def['properties']['ref']['type'])]
    else
      [:object_inline, nil] # e.g., resource - external_id
    end

  when 'array'
    arr = get_property_type(property_def['items'])
    [(arr[0].to_s + '_list').to_sym, arr[1]]

  when /^JSONModel\(:([a-z_]*)\)\s(uri)$/
    [:record_uri, [$1]]

  when /^JSONModel\(:([a-z_]*)\)\s(uri_or_object)$/
    [:record_uri_or_record_inline, [$1]]

  when /^JSONModel\(:([a-z_]*)\)\sobject$/
    [:record_inline, [$1]]

  else

    raise ASpaceImportException.new(:property_def => property_def)
  end
end

+ (Object) mint_id

Fake an ID to create URIs



7
8
9
# File 'backend/app/converters/lib/utils.rb', line 7

def self.mint_id
  "import_#{SecureRandom.uuid}"
end

+ (Object) ref_type_list(property_ref_type)



141
142
143
144
145
146
147
148
149
150
151
# File 'backend/app/converters/lib/utils.rb', line 141

def self.ref_type_list(property_ref_type)

  if property_ref_type.is_a?(Array) && property_ref_type[0].is_a?(Hash)
    property_ref_type.map { |t| t['type'].scan(/:([a-zA-Z_]*)/)[0][0] }

  elsif property_ref_type.is_a?(Array)
    property_ref_type.map { |t| t.scan(/:([a-zA-Z_]*)/)[0][0] }
  else
    property_ref_type.scan(/:([a-zA-Z_]*)/)[0][0]
  end
end

+ (Object) update_record_references(record, ref_source)



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'backend/app/converters/lib/utils.rb', line 154

def self.update_record_references(record, ref_source)
  if record.is_a?(Array) || record.respond_to?(:to_array)
    record.map {|e| update_record_references(e, ref_source)}
  elsif record.is_a?(Hash) || record.respond_to?(:each)
    fixed = {}

    record.each do |k, v|
      fixed[k] = update_record_references(v, ref_source)
    end

    fixed
  else
    ref_source[record] || record
  end
end

+ (Object) value_filter(property_type)



73
74
75
76
77
78
79
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'backend/app/converters/lib/utils.rb', line 73

def self.value_filter(property_type)
 case property_type

 when /^record_uri_or_record_inline/
   lambda {|val|
     val.block_further_reception if val.respond_to? :block_further_reception
     if val.class.method_defined? :uri
       val.uri
     else
       val
     end
   }

 when /^record_uri/
   lambda {|val|
     if val.class.method_defined? :uri
       val.uri
     else
       val.to_s
     end
   }

 when /^record_inline/
   lambda {|val|
     val
   }

 when /^record_ref/
   lambda {|val|
     if val.respond_to?(:uri)
       {'ref' => val.uri}
     else
       val
     end
   }

 when :boolean
   lambda {|val|
     if [false, true].include? val
       val
     elsif val.to_s == '0'
       false
     elsif val.to_s == '1'
       true
     end
   }

 when :dynamic_enum
   lambda {|val|
     val
   }

  when /^string/
    lambda {|val|
      val.split("\n").map {|s| s.strip }.join("\n")
    }
  
  when :integer
    lambda {|val|
      val.to_i
    }

  else
    raise "Can't handle #{property_type}"
  end
end