buttercms-ruby 1.6 → 1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 33b36093743686ac542292da5bbf4c0c1c7ade36e9d09c57d72161f8094a722b
4
- data.tar.gz: d75f6c5f94c7882c8aa105f10d267e78a31f98e660bdc6900827536da4891ce4
3
+ metadata.gz: 2c4b746f372b9a3255947360799f205470880bf4e0c07b45ffbf0bc9ce2ca107
4
+ data.tar.gz: 2ff93eef484952c2e236af4e28f69e922d58ddaf20d7dcfd76d1ef9f30a80685
5
5
  SHA512:
6
- metadata.gz: da1631dd94328b6c62786002de75bb33de70d4e584bb2f1bd0fb5481847a587a4113cc85d3d19fd69e8ef8a00be25b016c205e4a656c822ffca12140ede8ac34
7
- data.tar.gz: 8c900e9cc1cfef9eb187e2e5b6b0d1cee3e2dada2ef6c3ba3eff52fb429e07d77660cc29d2ea8a70fdecccc902abcf150b4177c1abf4dfe096a04c52f38840f0
6
+ metadata.gz: 277dd34304c21ab48b6e479d9c8a76bd6c504bac3059452da754728dd062ee3ab7c271c6bec91b6d295121452e08bc9dd51c7cdea09c699438aebd802ec3f0a8
7
+ data.tar.gz: f1e8800efbb85ecf77cfea176068b93b4d73e9f6d7fa3bc9784b41af4d862ef9d2bcca861bb5f5c61389bd6a36b6744ad45046c7ab3d65ed4bee332b0e447ccc
@@ -7,12 +7,19 @@ module ButterCMS
7
7
  @data = HashToObject.convert(json["data"])
8
8
  @meta = HashToObject.convert(json["meta"]) if json["meta"]
9
9
 
10
- if json["data"].is_a?(Hash)
11
- json["data"].each do |key, value|
12
- instance_variable_set("@#{key}", @data.send(key))
13
- self.class.send(:attr_reader, key)
14
- end
15
- end
10
+ define_attribute_methods(@data)
11
+ end
12
+
13
+ def marshal_dump
14
+ { json: @json, data: @data, meta: @meta }
15
+ end
16
+
17
+ def marshal_load(dump)
18
+ @json = dump[:json]
19
+ @data = dump[:data]
20
+ @meta = dump[:meta]
21
+
22
+ define_attribute_methods(@data)
16
23
  end
17
24
 
18
25
  def inspect
@@ -25,7 +32,7 @@ module ButterCMS
25
32
  # API expects all endpoints to include trailing slashes
26
33
  resource_path + (id ? "#{id}/" : '')
27
34
  end
28
-
35
+
29
36
  def self.patch_endpoint(id)
30
37
  # Append trailing slash when id is added to path because
31
38
  # API expects all endpoints to include trailing slashes
@@ -47,14 +54,14 @@ module ButterCMS
47
54
 
48
55
  self.create_object(response)
49
56
  end
50
-
57
+
51
58
  def self.create(options = {})
52
59
  options[:method] = 'Post'
53
60
  response = ButterCMS.write_request(self.endpoint, options)
54
61
 
55
62
  self.create_object(response)
56
63
  end
57
-
64
+
58
65
  def self.update(id, options = {})
59
66
  options[:method] = 'Patch'
60
67
  _endpoint = if resource_path.include?("/pages/")
@@ -76,5 +83,14 @@ module ButterCMS
76
83
  def self.create_object(response)
77
84
  self.new(response)
78
85
  end
86
+
87
+ def define_attribute_methods(methods)
88
+ return unless methods.respond_to?(:each_pair)
89
+
90
+ methods.each_pair do |key, value|
91
+ instance_variable_set("@#{key}", value)
92
+ self.class.send(:attr_reader, key)
93
+ end
94
+ end
79
95
  end
80
96
  end
@@ -1,3 +1,3 @@
1
1
  module ButterCMS
2
- VERSION = '1.6'
2
+ VERSION = '1.7'
3
3
  end
@@ -21,4 +21,20 @@ describe ButterCMS::ButterCollection do
21
21
 
22
22
  expect(collection.count).to eq 1
23
23
  end
24
+
25
+ # Marshal.load (used by Rails for caching) was not restoring the ButterResource's dynamic methods
26
+ # See https://github.com/ButterCMS/buttercms-ruby/issues/13
27
+ describe 'marshal load' do
28
+ subject { described_class.new(ButterCMS::ButterResource, 'data' => [{ 'name' => 'Test Name', 'description' => 'Test Description' }]) }
29
+
30
+ it 'restores the ButterResource dynamic methods' do
31
+ collection = Marshal.load(Marshal.dump(subject))
32
+ resource = collection.first
33
+
34
+ aggregate_failures do
35
+ expect(resource.name).to eq('Test Name')
36
+ expect(resource.description).to eq('Test Description')
37
+ end
38
+ end
39
+ end
24
40
  end
@@ -8,6 +8,17 @@ describe ButterCMS::ButterResource do
8
8
  allow(ButterCMS::ButterResource).to receive(:resource_path).and_return('')
9
9
  end
10
10
 
11
+ describe 'auto-generated methods' do
12
+ let(:resource) { described_class.new('data' => { 'name' => 'Test Name', 'description' => 'Test Description' }) }
13
+
14
+ it 'creates attribute reader methods for data pairs' do
15
+ aggregate_failures do
16
+ expect(resource.name).to eq('Test Name')
17
+ expect(resource.description).to eq('Test Description')
18
+ end
19
+ end
20
+ end
21
+
11
22
  describe '.all' do
12
23
 
13
24
  it 'should make a request with the correct endpoint' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: buttercms-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.6'
4
+ version: '1.7'
5
5
  platform: ruby
6
6
  authors:
7
7
  - ButterCMS
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-01-27 00:00:00.000000000 Z
11
+ date: 2020-02-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec