Skip to content
Snippets Groups Projects
Verified Commit e7beb2c2 authored by Alexander Olofsson's avatar Alexander Olofsson
Browse files

Separate model data conversion methods

This allows storing the data in its expected format outside of CMDB
calls
parent 60446fef
No related branches found
No related tags found
No related merge requests found
Pipeline #101568 passed
...@@ -128,6 +128,36 @@ module LiudeskCMDB ...@@ -128,6 +128,36 @@ module LiudeskCMDB
superclass.fields.merge(@fields || {}) superclass.fields.merge(@fields || {})
end end
def convert_cmdb_to_ruby(data)
fields.each do |key, attributes|
next unless data[key]
case attributes[:convert].to_s
when Time.to_s
data[key] = Time.parse(data[key]).round
when Symbol.to_s
data[key] = data[key].to_sym
end
end
data
end
def convert_ruby_to_cmdb(data)
fields.each do |key, attributes|
next unless data.key? key
case attributes[:convert].to_s
when Time.to_s
data[key] = data[key].utc.round.strftime("%FT%T.%LZ") if data[key].is_a? Time
when Symbol.to_s
data[key] = data[key].to_s if data[key].is_a? Symbol
end
end
data
end
protected protected
def read_fields(*fields) def read_fields(*fields)
...@@ -266,16 +296,7 @@ module LiudeskCMDB ...@@ -266,16 +296,7 @@ module LiudeskCMDB
data = data.dup data = data.dup
write_fields = self.class.fields.select { |_, field| field[:access].to_s.end_with?("write") }.map { |k, _| k } write_fields = self.class.fields.select { |_, field| field[:access].to_s.end_with?("write") }.map { |k, _| k }
self.class.fields.each do |key, attributes| self.class.convert_ruby_to_cmdb(data)
next unless data.key? key
case attributes[:convert].to_s
when Time.to_s
data[key] = data[key].utc.round.strftime("%FT%T.%LZ") if data[key].is_a? Time
when Symbol.to_s
data[key] = data[key].to_s if data[key].is_a? Symbol
end
end
data.select { |k, _| write_fields.include?(k) }.transform_keys { |k| self.class.fields.dig(k, :name) } data.select { |k, _| write_fields.include?(k) }.transform_keys { |k| self.class.fields.dig(k, :name) }
end end
...@@ -291,16 +312,7 @@ module LiudeskCMDB ...@@ -291,16 +312,7 @@ module LiudeskCMDB
@data = {} unless merge @data = {} unless merge
@data.merge!(converted) @data.merge!(converted)
self.class.fields.each do |key, attributes| self.class.convert_cmdb_to_ruby(@data)
next unless @data[key]
case attributes[:convert].to_s
when Time.to_s
@data[key] = Time.parse(@data[key]).round
when Symbol.to_s
@data[key] = @data[key].to_sym
end
end
@retrieved = Time.now @retrieved = Time.now
self self
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment