Method: XSD::BaseObject#method_missing

Defined in:
lib/xsd/base_object.rb

#method_missing(method, *args) ⇒ Object (protected)

Lookup for properties

Parameters:

  • method (Symbol)
  • args (Array)


309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/xsd/base_object.rb', line 309

def method_missing(method, *args)
  # check cache first
  return @cache[method] if @cache[method]

  # check for property first
  if (property = self.class.properties[method])
    value = property[:resolve] ? property[:resolve].call(self) : node[property[:name].to_s]
    r = if value.nil?
          if node['ref']
            # if object has reference - search property in referenced object
            reference.send(method, *args)
          else
            property[:default].is_a?(Proc) ? instance_eval(&property[:default]) : property[:default]
          end
        else
          case property[:type]
          when :integer
            property[:name] == :maxOccurs && value == 'unbounded' ? :unbounded : value.to_i
          when :boolean
            value == 'true'
          else
            value
          end
        end
    return @cache[method] = r
  end

  # if object has ref it cannot contain any type and children, so proxy call to target object
  if node['ref'] && method != :ref && method != :reference
    return reference.send(method, *args)
  end

  # then check for linked types
  if (link = self.class.links[method])
    name = link[:property] ? send(link[:property]) : nil
    if name
      return @cache[method] = object_by_name(link[:type], name)
    elsif is_a?(Restriction) && %i[base_simple_type base_complex_type].include?(method)
      # handle restriction without base
      return nil
    end
  end

  # last check for nested objects
  if (child = self.class.children[method])
    result = child[:type].is_a?(Array) ? map_children(child[:type][0]) : map_child(child[:type])
    return @cache[method] = result
  end

  super
  # api = self.class.properties.keys + self.class.links.keys + self.class.children.keys
  # raise Error, "Tried to access unknown object '#{method}' on '#{self.class.name}'. Available options are: #{api}"
end