-
Notifications
You must be signed in to change notification settings - Fork 23
/
search.rb
124 lines (111 loc) · 3.18 KB
/
search.rb
1
2
3
4
5
6
7
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
33
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
require "active_remote/persistence"
require "active_remote/rpc"
module ActiveRemote
module Search
extend ActiveSupport::Concern
included do
include Persistence
include RPC
end
module ClassMethods
# Tries to load the first record; if it fails, an exception is raised.
#
# ====Examples
#
# # A single hash
# Tag.find(:guid => 'foo')
#
# # Active remote object
# Tag.find(Tag.new(:guid => 'foo'))
#
# # Protobuf object
# Tag.find(Generic::Remote::TagRequest.new(:guid => 'foo'))
#
def find(args)
remote = self.search(args).first
raise RemoteRecordNotFound, self if remote.nil?
remote
end
# Tries to load the first record; if it fails, then create is called
# with the same arguments.
#
# ====Examples
#
# # A single hash
# Tag.first_or_create(:name => 'foo')
#
# # Protobuf object
# Tag.first_or_create(Generic::Remote::TagRequest.new(:name => 'foo'))
#
def first_or_create(attributes)
remote = self.search(attributes).first
remote ||= self.create(attributes)
remote
end
# Tries to load the first record; if it fails, then create! is called
# with the same arguments.
#
def first_or_create!(attributes)
remote = self.search(attributes).first
remote ||= self.create!(attributes)
remote
end
# Tries to load the first record; if it fails, then a new record is
# initialized with the same arguments.
#
# ====Examples
#
# # A single hash
# Tag.first_or_initialize(:name => 'foo')
#
# # Protobuf object
# Tag.first_or_initialize(Generic::Remote::TagRequest.new(:name => 'foo'))
#
def first_or_initialize(attributes)
remote = self.search(attributes).first
remote ||= self.new(attributes)
remote
end
# Searches for records with the given arguments. Returns a collection of
# Active Remote objects.
#
# ====Examples
#
# # A single hash
# Tag.search(:name => 'foo')
#
# # Protobuf object
# Tag.search(Generic::Remote::TagRequest.new(:name => 'foo'))
#
def search(args)
args = validate_search_args!(args)
response = remote_call(:search, args)
if response.respond_to?(:records)
serialize_records(response.records)
else
response
end
end
# Validates the given args to ensure they are compatible
# Search args must be a hash or respond to to_hash
#
def validate_search_args!(args)
unless args.is_a?(Hash)
if args.respond_to?(:to_hash)
args = args.to_hash
else
raise "Invalid parameter: #{args}. Search args must respond to :to_hash."
end
end
args
end
end
# Reload this record from the remote service.
#
def reload
fresh_object = self.class.find(scope_key_hash)
@attributes = fresh_object.instance_variable_get("@attributes")
self
end
end
end