Skip to content
This repository was archived by the owner on Jun 30, 2020. It is now read-only.

Commit 2f46b7f

Browse files
authored
Merge pull request #6 from fonji/consistency_between_accessors_and_hash_method
Using [] should give the same result than the accessor
2 parents 353763d + 8013ab3 commit 2f46b7f

2 files changed

Lines changed: 45 additions & 5 deletions

File tree

lib/hubspot/resource.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def changed?
9898
end
9999

100100
def [](name)
101-
@changes[name] || @properties[name]
101+
@changes[name] || @properties.dig(name, 'value')
102102
end
103103

104104
def reload
@@ -238,7 +238,7 @@ def add_accessors(keys)
238238
singleton_class.instance_eval do
239239
keys.each do |k|
240240
# Define a getter
241-
define_method(k) { @changes[k.to_sym] || @properties.dig(k, "value") }
241+
define_method(k) { @changes[k.to_sym] || @properties.dig(k, 'value') }
242242

243243
# Define a setter
244244
define_method("#{k}=") do |v|
@@ -257,7 +257,7 @@ def method_missing(method_name, *arguments, &block)
257257
# Call the new setter
258258
return send(method_name, arguments[0])
259259
elsif @properties.key?(method_name)
260-
return @properties[method_name]
260+
return @properties[method_name]['value']
261261
else
262262
super
263263
end
@@ -267,4 +267,4 @@ def respond_to_missing?(method_name, include_private = false)
267267
(@properties && @properties.key?(method_name)) || super
268268
end
269269
end
270-
end
270+
end

spec/lib/hubspot/resource_spec.rb

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,44 @@
5151
end
5252
end
5353
end
54-
end
54+
55+
describe '#[]' do
56+
context 'using new' do
57+
let(:resource) { described_class.new(properties) }
58+
let(:properties) { { id: 1, firstname: 'John', lastname: 'Wayne' } }
59+
60+
it { expect(resource[:firstname]).to eq 'John' }
61+
it { expect(resource['lastname']).to eq 'Wayne' }
62+
it { expect(resource[:middlename]).to be nil }
63+
end
64+
65+
context 'using from_result' do
66+
let(:resource) { described_class.from_result({ properties: properties }) }
67+
let(:properties) { { id: { 'value' => 1 }, firstname: { 'value' => 'John' }, lastname: { 'value' => 'Wayne' } } }
68+
69+
it { expect(resource[:firstname]).to eq 'John' }
70+
it { expect(resource['lastname']).to eq 'Wayne' }
71+
it { expect(resource[:middlename]).to be nil }
72+
end
73+
end
74+
75+
describe '#adding_accessors' do
76+
describe 'getters' do
77+
context 'using new' do
78+
let(:resource) { described_class.new(properties) }
79+
let(:properties) { { id: 1, firstname: 'John', lastname: 'Wayne' } }
80+
81+
it { expect(resource.firstname).to eq 'John' }
82+
it { expect(resource.lastname).to eq 'Wayne' }
83+
end
84+
85+
context 'using from_result' do
86+
let(:resource) { described_class.from_result({ properties: properties }) }
87+
let(:properties) { { id: { 'value' => 1 }, firstname: { 'value' => 'John' }, lastname: { 'value' => 'Wayne' } } }
88+
89+
it { expect(resource.firstname).to eq 'John' }
90+
it { expect(resource.lastname).to eq 'Wayne' }
91+
end
92+
end
93+
end
94+
end

0 commit comments

Comments
 (0)