Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.2.2
4.0.1
13 changes: 13 additions & 0 deletions lib/dvla/atlas/artefacts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ def define_fields(*vargs, **kwargs)
end
end

sig { params(_options: T.untyped).returns(T::Hash[String, T.untyped]) }
def as_json(_options = nil)
instance_variables.each_with_object({}) do |var, hash|
name = var.to_s.delete_prefix('@')
value = send(:"#{name}")

next if value.nil?
next if name.end_with?('_history') && value.empty?

hash[name] = value
end
end

private

sig { params(name: T.any(String, Symbol)).void }
Expand Down
2 changes: 1 addition & 1 deletion lib/dvla/atlas/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module DVLA
module Atlas
VERSION = '1.1.0'.freeze
VERSION = '1.1.1'.freeze
end
end
21 changes: 21 additions & 0 deletions spec/dvla/atlas/artefacts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,25 @@
expect(artefacts.first_expected_method_history).to eq(%w[abc])
expect(artefacts.second_expected_method_history).to eq([123])
end

describe '#as_json' do
it 'excludes fields that are nil' do
artefacts.define_fields('unset_field', set_field: 'value')
expect(artefacts.as_json).to eq({ 'set_field' => 'value' })
end

it 'excludes history fields for fields that have never been changed' do
artefacts.define_fields(username: 'initial')
json = artefacts.as_json
expect(json).to eq({ 'username' => 'initial' })
expect(json).not_to have_key('username_history')
end

it 'includes history fields for fields that have been changed' do
artefacts.define_fields(username: 'initial')
artefacts.username = 'updated'
json = artefacts.as_json
expect(json).to eq({ 'username' => 'updated', 'username_history' => ['initial'] })
end
end
end
Loading