Skip to content

Commit d9e1e6e

Browse files
Merge branch 'main' into nil-dates
2 parents 4d6dc7f + 05573f9 commit d9e1e6e

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

lib/seam/base_resource.rb

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,12 @@ class BaseResource
1111
def initialize(data, client = nil)
1212
@data = data
1313
@client = client
14-
15-
@data.each do |key, value|
16-
value = Seam::DeepHashAccessor.new(value) if value.is_a?(Hash)
17-
instance_variable_set(:"@#{key}", value)
18-
end
14+
process_data_attributes(@data)
1915
end
2016

2117
def update_from_response(data)
2218
@data = data
23-
@data.each do |key, value|
24-
value = Seam::DeepHashAccessor.new(value) if value.is_a?(Hash)
25-
instance_variable_set(:"@#{key}", value)
26-
end
19+
process_data_attributes(@data)
2720
end
2821

2922
def self.load_from_response(data, client = nil)
@@ -58,6 +51,21 @@ def self.date_accessor(*attrs)
5851
def parse_datetime(value)
5952
Time.parse(value)
6053
end
54+
55+
def process_data_attributes(data)
56+
data.each do |key, value|
57+
value = process_hash_value(value)
58+
instance_variable_set(:"@#{key}", value)
59+
end
60+
end
61+
62+
def process_hash_value(value)
63+
if value.is_a?(Hash) && !value.empty?
64+
Seam::DeepHashAccessor.new(value)
65+
else
66+
value
67+
end
68+
end
6169
end
6270
end
6371
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe Seam::Resources::BaseResource do
4+
describe "hash handling" do
5+
let(:client) { Seam.new(api_key: "seam_some_api_key") }
6+
7+
it "does not wrap empty hashes in DeepHashAccessor" do
8+
data = {
9+
device_id: "123",
10+
empty_hash: {},
11+
non_empty_hash: {key: "value"}
12+
}
13+
14+
resource = described_class.new(data, client)
15+
16+
expect(resource.instance_variable_get(:@empty_hash)).to eq({})
17+
expect(resource.instance_variable_get(:@empty_hash)).to be_a(Hash)
18+
expect(resource.instance_variable_get(:@empty_hash)).not_to be_a(Seam::DeepHashAccessor)
19+
20+
expect(resource.instance_variable_get(:@non_empty_hash)).to be_a(Seam::DeepHashAccessor)
21+
end
22+
end
23+
end

0 commit comments

Comments
 (0)