ooor_finders 0.1
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/README.md +25 -0
- data/lib/dynamic_finder_match.rb +54 -0
- data/lib/ooor_finders.rb +1 -0
- data/lib/open_object_resource.rb +42 -0
- metadata +84 -0
data/README.md
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
Following behaviors are added
|
2
|
+
|
3
|
+
find_by_XX
|
4
|
+
find_first_by_XX
|
5
|
+
find_all_by_XX
|
6
|
+
find_last_by_XX
|
7
|
+
+
|
8
|
+
find_by_xml_id or alias find_by_oid
|
9
|
+
|
10
|
+
So we can do
|
11
|
+
|
12
|
+
ResUsers.find_by_xml_id('module.xml_id').id
|
13
|
+
ResUsers.find_by_id(23).id
|
14
|
+
ResUsers.find_by_name('Administrator').id
|
15
|
+
ResUsers.find_by_name!('Administrator').id Will throw an exception if you put a ! at the end
|
16
|
+
|
17
|
+
We can also do:
|
18
|
+
ResUsers.find_all_by_name('Administrator')
|
19
|
+
ResUsers.find_last_by_name('Administrator')
|
20
|
+
ResUsers.find_last_by_name_and_login('Administrator', 'admin')
|
21
|
+
ResUsers.find_by_name_and_login('Administrator', 'admin')
|
22
|
+
|
23
|
+
Other options are supported
|
24
|
+
|
25
|
+
ResUsers.find_all_by_name('Administrator', :fields=>['id'], :limit=>20)
|
@@ -0,0 +1,54 @@
|
|
1
|
+
|
2
|
+
# Active Record Dynamic Finder Match
|
3
|
+
#
|
4
|
+
# Taken from active record source code
|
5
|
+
#
|
6
|
+
class DynamicFinderMatch
|
7
|
+
def self.match(method)
|
8
|
+
finder = :first
|
9
|
+
bang = false
|
10
|
+
instantiator = nil
|
11
|
+
|
12
|
+
case method.to_s
|
13
|
+
when /^find_(all_|last_)?by_([_a-zA-Z]\w*)$/
|
14
|
+
finder = :last if $1 == 'last_'
|
15
|
+
finder = :all if $1 == 'all_'
|
16
|
+
names = $2
|
17
|
+
when /^find_by_([_a-zA-Z]\w*)\!$/
|
18
|
+
bang = true
|
19
|
+
names = $1
|
20
|
+
when /^find_or_(initialize|create)_by_([_a-zA-Z]\w*)$/
|
21
|
+
instantiator = $1 == 'initialize' ? :new : :create
|
22
|
+
names = $2
|
23
|
+
else
|
24
|
+
return nil
|
25
|
+
end
|
26
|
+
|
27
|
+
new(finder, instantiator, bang, names.split('_and_'))
|
28
|
+
end
|
29
|
+
|
30
|
+
def initialize(finder, instantiator, bang, attribute_names)
|
31
|
+
@finder = finder
|
32
|
+
@instantiator = instantiator
|
33
|
+
@bang = bang
|
34
|
+
@attribute_names = attribute_names
|
35
|
+
end
|
36
|
+
|
37
|
+
attr_reader :finder, :attribute_names, :instantiator
|
38
|
+
|
39
|
+
def finder?
|
40
|
+
@finder && !@instantiator
|
41
|
+
end
|
42
|
+
|
43
|
+
def instantiator?
|
44
|
+
@finder == :first && @instantiator
|
45
|
+
end
|
46
|
+
|
47
|
+
def creator?
|
48
|
+
@finder == :first && @instantiator == :create
|
49
|
+
end
|
50
|
+
|
51
|
+
def bang?
|
52
|
+
@bang
|
53
|
+
end
|
54
|
+
end
|
data/lib/ooor_finders.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "open_object_resource"
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'ooor'
|
3
|
+
|
4
|
+
module Ooor
|
5
|
+
class OpenObjectResource
|
6
|
+
class << self
|
7
|
+
alias_method :method_missing_original, :method_missing
|
8
|
+
|
9
|
+
def _dynamic_find(match, arguments)
|
10
|
+
options = {}
|
11
|
+
if arguments.last.is_a? Hash
|
12
|
+
options = arguments.last
|
13
|
+
end
|
14
|
+
attributes = match.attribute_names
|
15
|
+
if attributes[0] == 'xml_id' || attributes[0] == 'id' || attributes[0] == 'oid'
|
16
|
+
return self.find(arguments[0], options)
|
17
|
+
else
|
18
|
+
domain = []
|
19
|
+
attributes.length.times do | index |
|
20
|
+
domain.push([attributes[index] ,'=', arguments[index]])
|
21
|
+
end
|
22
|
+
options.merge!({:domain => domain})
|
23
|
+
res = self.find(match.finder, options)
|
24
|
+
if match.bang?
|
25
|
+
unless res
|
26
|
+
raise "Could not find #{self.class} with for domain #{domain.inspect}"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
return res
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def method_missing(method_symbol, *arguments)
|
34
|
+
match = DynamicFinderMatch.match(method_symbol.to_s)
|
35
|
+
if match
|
36
|
+
return send(:_dynamic_find, match, arguments)
|
37
|
+
end
|
38
|
+
return method_missing_original
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ooor_finders
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 9
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: "0.1"
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Nicolas Bessi - Camptocamp
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2013-03-06 00:00:00 +01:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: ooor
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 5
|
29
|
+
segments:
|
30
|
+
- 1
|
31
|
+
- 6
|
32
|
+
- 5
|
33
|
+
version: 1.6.5
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
description: Add Active Record like dynamic finder pattern
|
37
|
+
email:
|
38
|
+
executables: []
|
39
|
+
|
40
|
+
extensions: []
|
41
|
+
|
42
|
+
extra_rdoc_files: []
|
43
|
+
|
44
|
+
files:
|
45
|
+
- README.md
|
46
|
+
- lib/ooor_finders.rb
|
47
|
+
- lib/open_object_resource.rb
|
48
|
+
- lib/dynamic_finder_match.rb
|
49
|
+
has_rdoc: true
|
50
|
+
homepage:
|
51
|
+
licenses: []
|
52
|
+
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options: []
|
55
|
+
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
hash: 3
|
64
|
+
segments:
|
65
|
+
- 0
|
66
|
+
version: "0"
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
hash: 3
|
73
|
+
segments:
|
74
|
+
- 0
|
75
|
+
version: "0"
|
76
|
+
requirements: []
|
77
|
+
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 1.6.2
|
80
|
+
signing_key:
|
81
|
+
specification_version: 3
|
82
|
+
summary: OOOR - OpenObject On Ruby dynamic finder extention
|
83
|
+
test_files: []
|
84
|
+
|