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

Commit

Permalink
Merge pull request #6 from fonji/consistency_between_accessors_and_ha…
Browse files Browse the repository at this point in the history
…sh_method

Using [] should give the same result than the accessor
  • Loading branch information
jplot authored Dec 31, 2019
2 parents 353763d + 8013ab3 commit 2f46b7f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
8 changes: 4 additions & 4 deletions lib/hubspot/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def changed?
end

def [](name)
@changes[name] || @properties[name]
@changes[name] || @properties.dig(name, 'value')
end

def reload
Expand Down Expand Up @@ -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|
Expand All @@ -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
Expand All @@ -267,4 +267,4 @@ def respond_to_missing?(method_name, include_private = false)
(@properties && @properties.key?(method_name)) || super
end
end
end
end
42 changes: 41 additions & 1 deletion spec/lib/hubspot/resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,44 @@
end
end
end
end

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

0 comments on commit 2f46b7f

Please sign in to comment.