Class: Ooor::OpenObjectResource

Inherits:
Object
  • Object
show all
Defined in:
lib/ooor/finders/open_object_resource.rb

Class Method Summary collapse

Class Method Details

._find_or_instantiator_by_attributes(match, arguments) ⇒ Object



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
# File 'lib/ooor/finders/open_object_resource.rb', line 34

def _find_or_instantiator_by_attributes(match, arguments)
  unique_keys = ['xml_id', 'id', 'oid']
  options = {}
  if arguments.last.is_a? Hash
    options = arguments.last
  end
  attributes = match.attribute_names
  unique_index = attributes.index{|x| unique_keys.include? x}
  if unique_index
    res = self.find(arguments[unique_index], options)
  else
    domain = []
    attributes.length.times do | index |
      domain.push([attributes[index] ,'=', arguments[index]])
    end
    options.merge!({:domain => domain})
    res = self.find(match.finder, options)
    if match.bang? && !match.instantiator?
      unless res
        raise "Could not find #{self.class} with for domain #{domain.inspect}"
      end
    end
  end
  if match.instantiator and not res
    return _instanciate_by_attributes(match, arguments)
  end
  return res
end

._instanciate_by_attributes(match, arguments) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ooor/finders/open_object_resource.rb', line 8

def _instanciate_by_attributes(match, arguments)
  # TODO check relation and send a warning
  res = self.new
  if RUBY_VERSION  >= '1.9.0' # related to system stack level error
    res.id = nil
  end
  attributes = match.attribute_names
  xml_id = nil
  attributes.length.times do |index|
    if ['xml_id', 'oid'].include? attributes[index]
      xml_id = arguments[index]
    elsif attributes[index] == 'id'
      raise "find_or_create_by_id is not supported"
    else
      eval("res.#{attributes[index]} = arguments[index]")
    end
  end
  if xml_id
    res.ir_model_data_id = xml_id.split('.')
  end
  if match.instantiator == :create
    res.save
  end
  return res
end

.method_missing(method_symbol, *arguments) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/ooor/finders/open_object_resource.rb', line 63

def method_missing(method_symbol, *arguments)
  match = DynamicFinderMatch.match(method_symbol.to_s)
  if match
    return send(:_find_or_instantiator_by_attributes, match, arguments)
  end
  return method_missing_original(method_symbol, *arguments)
end

.method_missing_originalObject



6
# File 'lib/ooor/finders/open_object_resource.rb', line 6

alias_method :method_missing_original, :method_missing