ooor_finders 0.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +7 -0
- data/lib/open_object_resource.rb +34 -8
- metadata +3 -2
data/README.md
CHANGED
@@ -9,6 +9,8 @@ find_by_xml_id or alias find_by_oid
|
|
9
9
|
|
10
10
|
So we can do
|
11
11
|
|
12
|
+
require "ooor_finders"
|
13
|
+
|
12
14
|
ResUsers.find_by_xml_id('module.xml_id').id
|
13
15
|
ResUsers.find_by_id(23).id
|
14
16
|
ResUsers.find_by_name('Administrator').id
|
@@ -20,6 +22,11 @@ ResUsers.find_last_by_name('Administrator')
|
|
20
22
|
ResUsers.find_last_by_name_and_login('Administrator', 'admin')
|
21
23
|
ResUsers.find_by_name_and_login('Administrator', 'admin')
|
22
24
|
|
25
|
+
Plus the create mode
|
26
|
+
|
27
|
+
ResUsers.find_or_create_by_login_and_name_and_oid 'toto', 'toto', 'base.toto'
|
28
|
+
ResUsers.find_or_create_by_login_and_name'toto', 'toto'
|
29
|
+
|
23
30
|
Other options are supported
|
24
31
|
|
25
32
|
ResUsers.find_all_by_name('Administrator', :fields=>['id'], :limit=>20)
|
data/lib/open_object_resource.rb
CHANGED
@@ -1,19 +1,42 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'ooor'
|
3
|
-
|
4
3
|
module Ooor
|
5
4
|
class OpenObjectResource
|
6
5
|
class << self
|
7
6
|
alias_method :method_missing_original, :method_missing
|
8
7
|
|
9
|
-
def
|
8
|
+
def _instanciate_by_attributes(match, arguments)
|
9
|
+
# TODO check relation and send a warning
|
10
|
+
res = self.new
|
11
|
+
attributes = match.attribute_names
|
12
|
+
xml_id = nil
|
13
|
+
attributes.length.times do |index|
|
14
|
+
if ['xml_id', 'oid'].include? attributes[index]
|
15
|
+
xml_id = arguments[index]
|
16
|
+
elsif attributes[index] == 'id'
|
17
|
+
raise "find_or_create_by_id is not supported"
|
18
|
+
else
|
19
|
+
eval("res.#{attributes[index]} = arguments[index]")
|
20
|
+
end
|
21
|
+
end
|
22
|
+
res.create
|
23
|
+
if xml_id
|
24
|
+
res.ir_model_data_id = xml_id
|
25
|
+
res.save
|
26
|
+
end
|
27
|
+
return res
|
28
|
+
end
|
29
|
+
|
30
|
+
def _find_or_instantiator_by_attributes(match, arguments)
|
31
|
+
unique_keys = ['xml_id', 'id', 'oid']
|
10
32
|
options = {}
|
11
33
|
if arguments.last.is_a? Hash
|
12
34
|
options = arguments.last
|
13
35
|
end
|
14
36
|
attributes = match.attribute_names
|
15
|
-
|
16
|
-
|
37
|
+
unique_index = attributes.index{|x| unique_keys.include? x}
|
38
|
+
if unique_index
|
39
|
+
res = self.find(arguments[unique_index], options)
|
17
40
|
else
|
18
41
|
domain = []
|
19
42
|
attributes.length.times do | index |
|
@@ -21,21 +44,24 @@ module Ooor
|
|
21
44
|
end
|
22
45
|
options.merge!({:domain => domain})
|
23
46
|
res = self.find(match.finder, options)
|
24
|
-
if match.bang?
|
47
|
+
if match.bang? && !match.instantiator?
|
25
48
|
unless res
|
26
49
|
raise "Could not find #{self.class} with for domain #{domain.inspect}"
|
27
50
|
end
|
28
51
|
end
|
29
|
-
return res
|
30
52
|
end
|
53
|
+
if match.instantiator and not res
|
54
|
+
return _instanciate_by_attributes(match, arguments)
|
55
|
+
end
|
56
|
+
return res
|
31
57
|
end
|
32
58
|
|
33
59
|
def method_missing(method_symbol, *arguments)
|
34
60
|
match = DynamicFinderMatch.match(method_symbol.to_s)
|
35
61
|
if match
|
36
|
-
return send(:
|
62
|
+
return send(:_find_or_instantiator_by_attributes, match, arguments)
|
37
63
|
end
|
38
|
-
return method_missing_original
|
64
|
+
return method_missing_original(method_symbol, arguments)
|
39
65
|
end
|
40
66
|
end
|
41
67
|
end
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ooor_finders
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 31
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
|
9
|
+
- 2
|
10
|
+
version: 0.1.2
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Nicolas Bessi - Camptocamp
|