Skip to content

Commit a17f3ab

Browse files
committed
Omit nil values when generating hashes for errors and warnings
1 parent 1dea49e commit a17f3ab

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

lib/jsonapi/error.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ def initialize(options = {})
1818
@status = Rack::Utils::SYMBOL_TO_STATUS_CODE[options[:status]].to_s
1919
@meta = options[:meta]
2020
end
21+
22+
def to_hash
23+
hash = {}
24+
instance_variables.each {|var| hash[var.to_s.delete('@')] = instance_variable_get(var) unless instance_variable_get(var).nil? }
25+
hash
26+
end
2127
end
2228

2329
class Warning
@@ -31,5 +37,11 @@ def initialize(options = {})
3137
options[:code]
3238
end
3339
end
40+
41+
def to_hash
42+
hash = {}
43+
instance_variables.each {|var| hash[var.to_s.delete('@')] = instance_variable_get(var) unless instance_variable_get(var).nil? }
44+
hash
45+
end
3446
end
3547
end

0 commit comments

Comments
 (0)