diff --git a/lib/hubspot/resource.rb b/lib/hubspot/resource.rb index a21c8d19..be22da36 100644 --- a/lib/hubspot/resource.rb +++ b/lib/hubspot/resource.rb @@ -95,6 +95,10 @@ def [](name) @changes[name] || @properties.dig(name, 'value') end + def []=(name, value) + @changes[name] = value unless @changes[name] == value + end + def reload raise(Hubspot::InvalidParams.new("Resource must have an ID")) if @id.nil? diff --git a/spec/lib/hubspot/resource_spec.rb b/spec/lib/hubspot/resource_spec.rb index 1fdcf08d..9199a2ab 100644 --- a/spec/lib/hubspot/resource_spec.rb +++ b/spec/lib/hubspot/resource_spec.rb @@ -73,6 +73,18 @@ end end + describe '#[]=' do + let(:resource) { described_class.from_result({ properties: }) } + let(:properties) { { id: { 'value' => 1 }, firstname: { 'value' => 'John' }, lastname: { 'value' => 'Wayne' } } } + + it 'stages a change to a property' do + resource[:firstname] = 'Jon' + + expect(resource[:firstname]).to eq 'Jon' + expect(resource.changes).to include(firstname: 'Jon') + end + end + describe '#adding_accessors' do describe 'getters' do context 'using new' do