Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using [] should give the same result than the accessor #218

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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