From 8013ab3c39d3e383201d2a9c5325f719944132e7 Mon Sep 17 00:00:00 2001 From: Yannick Fonjallaz Date: Thu, 19 Dec 2019 21:43:14 +0100 Subject: [PATCH] Using [] should give the same result than the accessor --- lib/hubspot/resource.rb | 8 +++--- spec/lib/hubspot/resource_spec.rb | 42 ++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/lib/hubspot/resource.rb b/lib/hubspot/resource.rb index 6cdfe165..92886d43 100644 --- a/lib/hubspot/resource.rb +++ b/lib/hubspot/resource.rb @@ -98,7 +98,7 @@ def changed? end def [](name) - @changes[name] || @properties[name] + @changes[name] || @properties.dig(name, 'value') end def reload @@ -238,7 +238,7 @@ def add_accessors(keys) singleton_class.instance_eval do keys.each do |k| # Define a getter - define_method(k) { @changes[k.to_sym] || @properties.dig(k, "value") } + define_method(k) { @changes[k.to_sym] || @properties.dig(k, 'value') } # Define a setter define_method("#{k}=") do |v| @@ -257,7 +257,7 @@ def method_missing(method_name, *arguments, &block) # Call the new setter return send(method_name, arguments[0]) elsif @properties.key?(method_name) - return @properties[method_name] + return @properties[method_name]['value'] else super end @@ -267,4 +267,4 @@ def respond_to_missing?(method_name, include_private = false) (@properties && @properties.key?(method_name)) || super end end -end \ No newline at end of file +end diff --git a/spec/lib/hubspot/resource_spec.rb b/spec/lib/hubspot/resource_spec.rb index d2c39d32..0d6fb95e 100644 --- a/spec/lib/hubspot/resource_spec.rb +++ b/spec/lib/hubspot/resource_spec.rb @@ -51,4 +51,44 @@ end end end -end \ No newline at end of file + + describe '#[]' do + context 'using new' do + let(:resource) { described_class.new(properties) } + let(:properties) { { id: 1, firstname: 'John', lastname: 'Wayne' } } + + it { expect(resource[:firstname]).to eq 'John' } + it { expect(resource['lastname']).to eq 'Wayne' } + it { expect(resource[:middlename]).to be nil } + end + + context 'using from_result' do + let(:resource) { described_class.from_result({ properties: properties }) } + let(:properties) { { id: { 'value' => 1 }, firstname: { 'value' => 'John' }, lastname: { 'value' => 'Wayne' } } } + + it { expect(resource[:firstname]).to eq 'John' } + it { expect(resource['lastname']).to eq 'Wayne' } + it { expect(resource[:middlename]).to be nil } + end + end + + describe '#adding_accessors' do + describe 'getters' do + context 'using new' do + let(:resource) { described_class.new(properties) } + let(:properties) { { id: 1, firstname: 'John', lastname: 'Wayne' } } + + it { expect(resource.firstname).to eq 'John' } + it { expect(resource.lastname).to eq 'Wayne' } + end + + context 'using from_result' do + let(:resource) { described_class.from_result({ properties: properties }) } + let(:properties) { { id: { 'value' => 1 }, firstname: { 'value' => 'John' }, lastname: { 'value' => 'Wayne' } } } + + it { expect(resource.firstname).to eq 'John' } + it { expect(resource.lastname).to eq 'Wayne' } + end + end + end +end