mirror_mirror 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -1
- data/lib/mirror_mirror/active_record_base.rb +72 -19
- data/lib/mirror_mirror/version.rb +1 -1
- metadata +3 -2
data/README.md
CHANGED
@@ -65,6 +65,6 @@ In this scenario a `contractor_id` is present, but we don't know if the `Contrac
|
|
65
65
|
<h1>Today's Punched-In Contractors</h1>
|
66
66
|
<ul>
|
67
67
|
<% @timesheets.each do |timesheet| %>
|
68
|
-
<li><%= "#{timesheet.
|
68
|
+
<li><%= "#{timesheet.contractor.first_name} #{timesheet.contractor.last_name}"</li>
|
69
69
|
<% end %>
|
70
70
|
</ul>
|
@@ -4,15 +4,20 @@ module MirrorMirror::ActiveRecordBase
|
|
4
4
|
included do
|
5
5
|
|
6
6
|
def reflect!
|
7
|
+
reflect
|
8
|
+
save!
|
9
|
+
self
|
10
|
+
end
|
11
|
+
|
12
|
+
def reflect
|
7
13
|
hash = self.class.mirror_request!(:get, mirror_url)
|
8
14
|
raise RecordNotFound, "Non-Hash returned from resource: #{mirror_url} => #{hash.inspect}" unless hash.is_a?(Hash)
|
9
15
|
raise RecordNotFound, "Empty hash returned from resource." unless hash.any?
|
10
16
|
hash.each {|k,v| send("#{k}=", v) if respond_to?("#{k}=") }
|
11
|
-
self.updated_at = Time.now
|
12
|
-
save!
|
13
17
|
self
|
14
18
|
end
|
15
19
|
|
20
|
+
# (See #mirror_url)
|
16
21
|
def mirror_url
|
17
22
|
self.class.mirror_url(id)
|
18
23
|
end
|
@@ -21,32 +26,65 @@ module MirrorMirror::ActiveRecordBase
|
|
21
26
|
|
22
27
|
module ClassMethods
|
23
28
|
|
29
|
+
# The setup/configuration method for mirror_mirror functionality.
|
30
|
+
# @param [String] url the url of the resource collection (e.g., http://example.com/api/books)
|
31
|
+
# @param [Hash] options
|
32
|
+
# * :find (Boolean) (default to: false) - If true, `#find` will automatically attempt to `#reflect!` the external record based on the given id.
|
33
|
+
# * :request (Symbol, Nil) (defaults to: nil) - Specify a custom class method within the model which will perform the HTTP request.
|
34
|
+
# @return [Nil]
|
24
35
|
def mirror_mirror(*args)
|
25
36
|
options = args.extract_options!
|
26
37
|
raise ArgumentError, "url is blank." if args[0].blank?
|
27
38
|
options[:url] = args[0]
|
28
39
|
@mirror_mirrior_options = options
|
40
|
+
nil
|
29
41
|
end
|
30
42
|
|
43
|
+
# Determine if this class has been setup to mirror an external resource using `MirrorMirror`.
|
44
|
+
#
|
45
|
+
# @return [Boolean]
|
31
46
|
def mirroring?
|
32
|
-
|
47
|
+
mirror_mirror_options[:url].present?
|
33
48
|
end
|
34
49
|
|
35
|
-
|
50
|
+
# Get one, or all, of the mirror_mirror class options.
|
51
|
+
#
|
52
|
+
# @param [Symbol] option the specific option key (e.g., :find, :url, :request)
|
53
|
+
# @return [Mixed, Hash] mixed option values, or a Hash containg all class options.
|
54
|
+
def mirror_mirror_options(option = nil)
|
36
55
|
@mirror_mirrior_options ||= {}
|
37
|
-
|
56
|
+
if option.nil?
|
57
|
+
@mirror_mirrior_options
|
58
|
+
else
|
59
|
+
@mirror_mirrior_options[option]
|
60
|
+
end
|
38
61
|
end
|
39
62
|
|
63
|
+
# Determine if the `:find => true` option has been specified for this class.
|
64
|
+
#
|
65
|
+
# @return [Boolean]
|
40
66
|
def mirror_find?
|
41
67
|
mirroring? ? @mirror_mirrior_options[:find] : false
|
42
68
|
end
|
43
69
|
|
70
|
+
# Get the url specified in the Model's `#mirror_mirror` setup. Optionally specify a record `id`.
|
71
|
+
#
|
72
|
+
# @param [Integer] id the resource record id.
|
73
|
+
# @return [String] the url.
|
44
74
|
def mirror_url(id = nil)
|
45
75
|
url = @mirror_mirrior_options[:url]
|
46
76
|
url += "/#{id.to_i}" if id.to_i > 0
|
47
77
|
url
|
48
78
|
end
|
49
79
|
|
80
|
+
# A universial `request` method, used internally, that maps to either the default `#MirrorMirror.request!` method, or
|
81
|
+
# to a the optional `:request` method specified in the #mirror_mirror options of the Model.
|
82
|
+
#
|
83
|
+
# @note Will probably go through sweeping changes in the near future.
|
84
|
+
# @param [Symbol] verb the http request verb. (`:get`, `:put`, `:post`, and `:delete` are supported)
|
85
|
+
# @param [String] url the url to be requested.
|
86
|
+
# @param [Hash] params the parameters sent along with the request.
|
87
|
+
# @return [Hash, Array] dependant on the context of the call (Record vs Collection)
|
50
88
|
def mirror_request!(verb, url, params = {})
|
51
89
|
method = @mirror_mirrior_options[:request]
|
52
90
|
if method.present?
|
@@ -65,6 +103,11 @@ module MirrorMirror::ActiveRecordBase
|
|
65
103
|
end
|
66
104
|
end
|
67
105
|
|
106
|
+
# Attempt to reflect all items from the external collection.
|
107
|
+
#
|
108
|
+
# @note Not ready for usage; just a thought worth building off from.
|
109
|
+
# @param [Hash] params the parameters to be passed to the request method.
|
110
|
+
# @return [Array] of active record objects.
|
68
111
|
def reflect_all!(params = {})
|
69
112
|
array = mirror_request(:get, mirror_url, params)
|
70
113
|
raise RecordNotFound, "Non-Array returned from resource: #{mirror_url} => #{hash.inspect}" unless array.is_a?(Array)
|
@@ -83,20 +126,10 @@ module MirrorMirror::ActiveRecordBase
|
|
83
126
|
all
|
84
127
|
end
|
85
128
|
|
86
|
-
#
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
super
|
91
|
-
rescue ActiveRecord::RecordNotFound
|
92
|
-
if (id = args[0].to_i) > 0
|
93
|
-
record = self.new
|
94
|
-
record.id = id
|
95
|
-
record.reflect!
|
96
|
-
end
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
129
|
+
# Initialize a new record, set the id, and call `#reflect!`
|
130
|
+
#
|
131
|
+
# @param [Integer] id the id of the record.
|
132
|
+
# @return [Object] the active record object.
|
100
133
|
def reflect_by_id!(id)
|
101
134
|
record = self.new
|
102
135
|
record.id = id
|
@@ -104,12 +137,32 @@ module MirrorMirror::ActiveRecordBase
|
|
104
137
|
record
|
105
138
|
end
|
106
139
|
|
140
|
+
# Call to find_or_initialize_by_id, and `#reflect!` if the record was not found locally.
|
141
|
+
#
|
142
|
+
# @param [Integer] id the id of the record.
|
143
|
+
# @return [Object] the active record object.
|
107
144
|
def find_or_reflect_by_id!(*args)
|
108
145
|
record = find_or_initialize_by_id(*args)
|
109
146
|
record.reflect! if record.new_record?
|
110
147
|
record
|
111
148
|
end
|
112
149
|
|
150
|
+
# Monkey-Patching of the `ActiveRecord::Base.find()` method to support the `:find => true` option (see #mirror_mirror)
|
151
|
+
def find(*args)
|
152
|
+
return super unless mirror_find?
|
153
|
+
begin
|
154
|
+
super
|
155
|
+
rescue ActiveRecord::RecordNotFound
|
156
|
+
if (id = args[0].to_i) > 0
|
157
|
+
record = self.new
|
158
|
+
record.id = id
|
159
|
+
record.reflect!
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
# Monkey-Patching of the `ActiveRecord::Base.belongs_to()` method to support an extra option `:auto_reflect => true`
|
165
|
+
# which allows for automatic retrival of the associated record based on the `:foreign_key` using `#find()`
|
113
166
|
def belongs_to(*args)
|
114
167
|
options = args.extract_options!
|
115
168
|
association_name = args[0].to_s
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: mirror_mirror
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.3
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Daniel Doezema
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2012-06-
|
13
|
+
date: 2012-06-19 00:00:00 Z
|
14
14
|
dependencies: []
|
15
15
|
|
16
16
|
description: Allows for easier interactions and integration with external REST resources.
|
@@ -62,3 +62,4 @@ specification_version: 3
|
|
62
62
|
summary: Allows for easier interactions and integration with external REST resources.
|
63
63
|
test_files: []
|
64
64
|
|
65
|
+
has_rdoc:
|