active_remote 1.4.1 → 1.5.0

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.
@@ -4,6 +4,7 @@ require 'active_remote/bulk'
4
4
  require 'active_remote/dirty'
5
5
  require 'active_remote/dsl'
6
6
  require 'active_remote/persistence'
7
+ require 'active_remote/publication'
7
8
  require 'active_remote/rpc'
8
9
  require 'active_remote/search'
9
10
  require 'active_remote/serialization'
@@ -19,6 +20,7 @@ module ActiveRemote
19
20
  include ::ActiveRemote::Bulk
20
21
  include ::ActiveRemote::DSL
21
22
  include ::ActiveRemote::Persistence
23
+ include ::ActiveRemote::Publication
22
24
  include ::ActiveRemote::RPC
23
25
  include ::ActiveRemote::Search
24
26
  include ::ActiveRemote::Serialization
@@ -75,7 +75,7 @@ module ActiveRemote
75
75
  #
76
76
  def delete
77
77
  raise ReadOnlyRemoteRecord if readonly?
78
- execute(:delete, attributes.slice("guid"))
78
+ execute(:delete, @attributes.slice("guid"))
79
79
 
80
80
  return success? ? freeze : false
81
81
  end
@@ -98,7 +98,7 @@ module ActiveRemote
98
98
  #
99
99
  def destroy
100
100
  raise ReadOnlyRemoteRecord if readonly?
101
- execute(:destroy, attributes.slice("guid"))
101
+ execute(:destroy, @attributes.slice("guid"))
102
102
 
103
103
  return success? ? freeze : false
104
104
  end
@@ -197,10 +197,9 @@ module ActiveRemote
197
197
  # errors from the response.
198
198
  #
199
199
  def create
200
- new_attributes = attributes.deep_dup
201
- new_attributes.delete("guid")
200
+ @attributes.delete("guid")
202
201
 
203
- execute(:create, new_attributes)
202
+ execute(:create, @attributes)
204
203
 
205
204
  assign_attributes(last_response.to_hash)
206
205
  add_errors_from_response
@@ -222,7 +221,7 @@ module ActiveRemote
222
221
  # (plus :guid) will be updated. Defaults to all attributes.
223
222
  #
224
223
  def update(attribute_names = @attributes.keys)
225
- updated_attributes = attributes.slice(*attribute_names)
224
+ updated_attributes = @attributes.slice(*attribute_names)
226
225
  updated_attributes.merge!("guid" => self[:guid])
227
226
 
228
227
  execute(:update, updated_attributes)
@@ -0,0 +1,54 @@
1
+ module ActiveRemote
2
+ module Publication
3
+ # Returns a hash of publishable attributes.
4
+ #
5
+ def publishable_hash
6
+ keys = _publishable_attributes_or_attribute_keys
7
+
8
+ attributes_hash = keys.inject({}) do |publishable_hash, key|
9
+ value = respond_to?(key) ? __send__(key) : @attributes[key]
10
+
11
+ publishable_hash[key] = case
12
+ when value.respond_to?(:map) then
13
+ _map_value(value)
14
+ when value.respond_to?(:publishable_hash) then
15
+ value.publishable_hash
16
+ when value.respond_to?(:to_hash) then
17
+ value.to_hash
18
+ else
19
+ value
20
+ end
21
+
22
+ publishable_hash
23
+ end
24
+
25
+ attributes_hash
26
+ end
27
+
28
+ def _publishable_json_attributes
29
+ _publishable_attributes_or_attribute_keys - _publishable_json_methods
30
+ end
31
+
32
+ def _publishable_json_methods
33
+ _publishable_attributes_or_attribute_keys.reject { |attribute| @attributes.key?(attribute) }
34
+ end
35
+
36
+ private
37
+
38
+ def _map_value(value)
39
+ case
40
+ when value.any? { |obj| obj.respond_to?(:publishable_hash) } then
41
+ value.map(&:publishable_hash)
42
+ when value.any? { |obj| obj.respond_to?(:to_hash) } then
43
+ value.map(&:to_hash)
44
+ else
45
+ value
46
+ end
47
+ end
48
+
49
+ def _publishable_attributes_or_attribute_keys
50
+ @_publishable_attributes_or_attribute_keys = _publishable_attributes
51
+ @_publishable_attributes_or_attribute_keys ||= @attributes.keys
52
+ end
53
+ end
54
+ end
@@ -2,13 +2,9 @@ module ActiveRemote
2
2
  module Serializers
3
3
  module JSON
4
4
  # Returns a json representation of the whitelisted publishable attributes.
5
+ #
5
6
  def as_json(options = {})
6
- json_attributes = _publishable_attributes || attributes.keys
7
-
8
- json_methods = json_attributes.reject { |attribute| attributes.key?(attribute) }
9
- json_attributes -= json_methods
10
-
11
- default_options = { :only => json_attributes, :methods => json_methods }
7
+ default_options = { :only => _publishable_json_attributes, :methods => _publishable_json_methods }
12
8
  default_options.merge!(options)
13
9
 
14
10
  super(default_options)
@@ -1,3 +1,3 @@
1
1
  module ActiveRemote
2
- VERSION = "1.4.1"
2
+ VERSION = "1.5.0"
3
3
  end
@@ -21,15 +21,6 @@ describe ActiveRemote::DSL do
21
21
  end
22
22
  end
23
23
 
24
- describe ".auto_paging_size" do
25
- context "when given a value" do
26
- it "sets @auto_paging_size to the value" do
27
- Tag.auto_paging_size 100
28
- Tag.auto_paging_size.should eq 100
29
- end
30
- end
31
- end
32
-
33
24
  describe ".namespace" do
34
25
  context "when given a value" do
35
26
  it "sets @namespace to the value" do
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActiveRemote::Publication do
4
+ let(:attributes) { { :guid => 'foo', :name => 'bar' } }
5
+
6
+ subject { Tag.new(attributes) }
7
+
8
+ context "with publishable attributes defined" do
9
+ let(:expected_hash) { attributes.slice(:name) }
10
+
11
+ before { Tag.attr_publishable :name }
12
+ after { reset_publishable_attributes(Tag) }
13
+
14
+ it "serializes to a hash with only the publishable attributes" do
15
+ subject.publishable_hash.should eq expected_hash
16
+ end
17
+ end
18
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_remote
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.1
4
+ version: 1.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-07 00:00:00.000000000 Z
12
+ date: 2013-02-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: active_attr
@@ -164,6 +164,7 @@ files:
164
164
  - lib/active_remote/dsl.rb
165
165
  - lib/active_remote/errors.rb
166
166
  - lib/active_remote/persistence.rb
167
+ - lib/active_remote/publication.rb
167
168
  - lib/active_remote/rpc.rb
168
169
  - lib/active_remote/search.rb
169
170
  - lib/active_remote/serialization.rb
@@ -181,6 +182,7 @@ files:
181
182
  - spec/lib/active_remote/dirty_spec.rb
182
183
  - spec/lib/active_remote/dsl_spec.rb
183
184
  - spec/lib/active_remote/persistence_spec.rb
185
+ - spec/lib/active_remote/publication_spec.rb
184
186
  - spec/lib/active_remote/rpc_spec.rb
185
187
  - spec/lib/active_remote/search_spec.rb
186
188
  - spec/lib/active_remote/serialization_spec.rb
@@ -219,7 +221,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
219
221
  version: '0'
220
222
  segments:
221
223
  - 0
222
- hash: -2023512064389771027
224
+ hash: -3057893291356198545
223
225
  required_rubygems_version: !ruby/object:Gem::Requirement
224
226
  none: false
225
227
  requirements:
@@ -228,7 +230,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
228
230
  version: '0'
229
231
  segments:
230
232
  - 0
231
- hash: -2023512064389771027
233
+ hash: -3057893291356198545
232
234
  requirements: []
233
235
  rubyforge_project:
234
236
  rubygems_version: 1.8.24
@@ -243,6 +245,7 @@ test_files:
243
245
  - spec/lib/active_remote/dirty_spec.rb
244
246
  - spec/lib/active_remote/dsl_spec.rb
245
247
  - spec/lib/active_remote/persistence_spec.rb
248
+ - spec/lib/active_remote/publication_spec.rb
246
249
  - spec/lib/active_remote/rpc_spec.rb
247
250
  - spec/lib/active_remote/search_spec.rb
248
251
  - spec/lib/active_remote/serialization_spec.rb