ooor-finders 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +2 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +31 -0
- data/README.md +53 -0
- data/Rakefile +3 -0
- data/lib/ooor/finders.rb +1 -0
- data/lib/ooor/finders/dynamic_finder_match.rb +53 -0
- data/lib/ooor/finders/open_object_resource.rb +69 -0
- data/lib/ooor/finders/version.rb +5 -0
- data/lib/ooor_finders.rb +3 -0
- data/ooor-finders.gemspec +20 -0
- metadata +106 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
ooor-finders (0.1.6)
|
5
|
+
ooor (>= 1.6.5)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (3.2.2)
|
11
|
+
activesupport (= 3.2.2)
|
12
|
+
builder (~> 3.0.0)
|
13
|
+
activeresource (3.2.2)
|
14
|
+
activemodel (= 3.2.2)
|
15
|
+
activesupport (= 3.2.2)
|
16
|
+
activesupport (3.2.2)
|
17
|
+
i18n (~> 0.6)
|
18
|
+
multi_json (~> 1.0)
|
19
|
+
builder (3.0.0)
|
20
|
+
i18n (0.6.0)
|
21
|
+
multi_json (1.1.0)
|
22
|
+
ooor (1.6.5)
|
23
|
+
activeresource (>= 2.3.5)
|
24
|
+
rake (0.9.2.2)
|
25
|
+
|
26
|
+
PLATFORMS
|
27
|
+
ruby
|
28
|
+
|
29
|
+
DEPENDENCIES
|
30
|
+
ooor-finders!
|
31
|
+
rake
|
data/README.md
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
============
|
2
|
+
OOOR Finders
|
3
|
+
============
|
4
|
+
|
5
|
+
Following behaviors are added
|
6
|
+
|
7
|
+
* find_by_XX
|
8
|
+
* find_first_by_XX
|
9
|
+
* find_all_by_XX
|
10
|
+
* find_last_by_XX
|
11
|
+
* find_or_create_by
|
12
|
+
* find_or_initialize_by
|
13
|
+
* find_by_xml_id or alias find_by_oid
|
14
|
+
|
15
|
+
So we can do:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
require "ooor/finders"
|
19
|
+
|
20
|
+
ResUsers.find_by_xml_id('module.xml_id').id
|
21
|
+
ResUsers.find_by_id(23).id
|
22
|
+
ResUsers.find_by_name('Administrator').id
|
23
|
+
ResUsers.find_by_name!('Administrator').id # Will throw an exception if you put a ! at the end and nothing is found
|
24
|
+
```
|
25
|
+
|
26
|
+
We can also use all or last:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
ResUsers.find_all_by_name('Administrator')
|
30
|
+
ResUsers.find_last_by_name('Administrator')
|
31
|
+
ResUsers.find_last_by_name_and_login('Administrator', 'admin')
|
32
|
+
ResUsers.find_by_name_and_login('Administrator', 'admin')
|
33
|
+
```
|
34
|
+
|
35
|
+
Plus the create mode:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
ResUsers.find_or_create_by_login_and_name_and_oid 'toto', 'toto', 'base.toto'
|
39
|
+
ResUsers.find_or_create_by_login_and_name'toto', 'toto'
|
40
|
+
```
|
41
|
+
|
42
|
+
Other options are supported:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
ResUsers.find_all_by_name('Administrator', :fields=>['id'], :limit=>20)
|
46
|
+
```
|
47
|
+
|
48
|
+
*************
|
49
|
+
Installation
|
50
|
+
*************
|
51
|
+
```
|
52
|
+
gem install ooor-finders
|
53
|
+
```
|
data/Rakefile
ADDED
data/lib/ooor/finders.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'ooor/finders/open_object_resource'
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# Active Record Dynamic Finder Match
|
2
|
+
#
|
3
|
+
# Taken from active record source code
|
4
|
+
#
|
5
|
+
class DynamicFinderMatch
|
6
|
+
def self.match(method)
|
7
|
+
finder = :first
|
8
|
+
bang = false
|
9
|
+
instantiator = nil
|
10
|
+
|
11
|
+
case method.to_s
|
12
|
+
when /^find_(all_|last_)?by_([_a-zA-Z]\w*)$/
|
13
|
+
finder = :last if $1 == 'last_'
|
14
|
+
finder = :all if $1 == 'all_'
|
15
|
+
names = $2
|
16
|
+
when /^find_by_([_a-zA-Z]\w*)\!$/
|
17
|
+
bang = true
|
18
|
+
names = $1
|
19
|
+
when /^find_or_(initialize|create)_by_([_a-zA-Z]\w*)$/
|
20
|
+
instantiator = $1 == 'initialize' ? :new : :create
|
21
|
+
names = $2
|
22
|
+
else
|
23
|
+
return nil
|
24
|
+
end
|
25
|
+
|
26
|
+
new(finder, instantiator, bang, names.split('_and_'))
|
27
|
+
end
|
28
|
+
|
29
|
+
def initialize(finder, instantiator, bang, attribute_names)
|
30
|
+
@finder = finder
|
31
|
+
@instantiator = instantiator
|
32
|
+
@bang = bang
|
33
|
+
@attribute_names = attribute_names
|
34
|
+
end
|
35
|
+
|
36
|
+
attr_reader :finder, :attribute_names, :instantiator
|
37
|
+
|
38
|
+
def finder?
|
39
|
+
@finder && !@instantiator
|
40
|
+
end
|
41
|
+
|
42
|
+
def instantiator?
|
43
|
+
@finder == :first && @instantiator
|
44
|
+
end
|
45
|
+
|
46
|
+
def creator?
|
47
|
+
@finder == :first && @instantiator == :create
|
48
|
+
end
|
49
|
+
|
50
|
+
def bang?
|
51
|
+
@bang
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'ooor'
|
2
|
+
require 'ooor/finders/dynamic_finder_match'
|
3
|
+
module Ooor
|
4
|
+
class OpenObjectResource
|
5
|
+
class << self
|
6
|
+
alias_method :method_missing_original, :method_missing
|
7
|
+
|
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
|
+
if xml_id
|
23
|
+
res.ir_model_data_id = xml_id.split('.')
|
24
|
+
end
|
25
|
+
if match.instantiator == :create
|
26
|
+
res.save
|
27
|
+
end
|
28
|
+
return res
|
29
|
+
end
|
30
|
+
|
31
|
+
def _find_or_instantiator_by_attributes(match, arguments)
|
32
|
+
unique_keys = ['xml_id', 'id', 'oid']
|
33
|
+
options = {}
|
34
|
+
if arguments.last.is_a? Hash
|
35
|
+
options = arguments.last
|
36
|
+
end
|
37
|
+
attributes = match.attribute_names
|
38
|
+
unique_index = attributes.index{|x| unique_keys.include? x}
|
39
|
+
if unique_index
|
40
|
+
res = self.find(arguments[unique_index], options)
|
41
|
+
else
|
42
|
+
domain = []
|
43
|
+
attributes.length.times do | index |
|
44
|
+
domain.push([attributes[index] ,'=', arguments[index]])
|
45
|
+
end
|
46
|
+
options.merge!({:domain => domain})
|
47
|
+
res = self.find(match.finder, options)
|
48
|
+
if match.bang? && !match.instantiator?
|
49
|
+
unless res
|
50
|
+
raise "Could not find #{self.class} with for domain #{domain.inspect}"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
if match.instantiator and not res
|
55
|
+
return _instanciate_by_attributes(match, arguments)
|
56
|
+
end
|
57
|
+
return res
|
58
|
+
end
|
59
|
+
|
60
|
+
def method_missing(method_symbol, *arguments)
|
61
|
+
match = DynamicFinderMatch.match(method_symbol.to_s)
|
62
|
+
if match
|
63
|
+
return send(:_find_or_instantiator_by_attributes, match, arguments)
|
64
|
+
end
|
65
|
+
return method_missing_original(method_symbol, *arguments)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
data/lib/ooor_finders.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
lib = File.expand_path('../lib/', __FILE__)
|
2
|
+
$:.unshift lib unless $:.include?(lib)
|
3
|
+
require 'ooor/finders/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'ooor-finders'
|
7
|
+
s.version = Ooor::Finders::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.date = '2012-03-06'
|
10
|
+
s.authors = ["Nicolas Bessi - Camptocamp"]
|
11
|
+
s.summary = %q{OOOR - OpenObject On Ruby dynamic finder extention}
|
12
|
+
s.description = %q{Add Active Record like dynamic finder pattern}
|
13
|
+
s.files = `git ls-files`.split("\n")
|
14
|
+
s.require_path = 'lib'
|
15
|
+
s.homepage = "http://github.com/camptocamp/ooor_finders"
|
16
|
+
|
17
|
+
s.add_dependency('ooor', [">= 1.6.5"])
|
18
|
+
|
19
|
+
s.add_development_dependency "rake"
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ooor-finders
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 6
|
10
|
+
version: 0.1.6
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Nicolas Bessi - Camptocamp
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-03-06 00:00:00 +01:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: ooor
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 5
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 6
|
33
|
+
- 5
|
34
|
+
version: 1.6.5
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: rake
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 3
|
46
|
+
segments:
|
47
|
+
- 0
|
48
|
+
version: "0"
|
49
|
+
type: :development
|
50
|
+
version_requirements: *id002
|
51
|
+
description: Add Active Record like dynamic finder pattern
|
52
|
+
email:
|
53
|
+
executables: []
|
54
|
+
|
55
|
+
extensions: []
|
56
|
+
|
57
|
+
extra_rdoc_files: []
|
58
|
+
|
59
|
+
files:
|
60
|
+
- .gitignore
|
61
|
+
- Gemfile
|
62
|
+
- Gemfile.lock
|
63
|
+
- README.md
|
64
|
+
- Rakefile
|
65
|
+
- lib/ooor/finders.rb
|
66
|
+
- lib/ooor/finders/dynamic_finder_match.rb
|
67
|
+
- lib/ooor/finders/open_object_resource.rb
|
68
|
+
- lib/ooor/finders/version.rb
|
69
|
+
- lib/ooor_finders.rb
|
70
|
+
- ooor-finders.gemspec
|
71
|
+
has_rdoc: true
|
72
|
+
homepage: http://github.com/camptocamp/ooor_finders
|
73
|
+
licenses: []
|
74
|
+
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
hash: 3
|
86
|
+
segments:
|
87
|
+
- 0
|
88
|
+
version: "0"
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
hash: 3
|
95
|
+
segments:
|
96
|
+
- 0
|
97
|
+
version: "0"
|
98
|
+
requirements: []
|
99
|
+
|
100
|
+
rubyforge_project:
|
101
|
+
rubygems_version: 1.6.2
|
102
|
+
signing_key:
|
103
|
+
specification_version: 3
|
104
|
+
summary: OOOR - OpenObject On Ruby dynamic finder extention
|
105
|
+
test_files: []
|
106
|
+
|