Replies: 1 comment 2 replies
-
Fortunately, we won't need to actually transform the "external name" for any given property with the Ruby code above -- all of this is predetermined in the IR that the Ruby generator acts upon so we can just generate the models in whatever way we need. I see a couple approaches so far: 1. Explicit mappersclass Customer
attr_accessor :full_name, :email_address
def initialize(attributes = {})
@full_name = attributes[:full_name]
@email_address = attributes[:email_address]
end
def as_json(options = {})
{
'name' => @full_name,
'email' => @email_address
}
end
def self.from_json(json)
data = JSON.parse(json)
new(
full_name: data['name'],
email_address: data['email']
)
end
end 2. Metaprogramming (w/ base class)A class Customer < BaseModel
map_attribute :full_name, 'name'
map_attribute :email_address, 'email'
end I'm not sure if we already have other With that said, we could also make it easy for the serialization layer to track additional properties and surface them in each class. What are your thoughts @ethnt? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Serialization/deserialization
Depending on the data object structure, the serialization method should be straightforward enough using the standard library JSON gem. Previously the
oj
gem was preferred here for performance reasons, but the JSON gem was recently significantly optimized, where it is now faster thanoj
.Dealing with snake_case and CamelCase
If we are storing some sort of information about each property in our data objects, we can also store an "external name" for the property and have a different one for the actual attribute (e.g., the attribute
per_page
would haveperPage
as the external name). The actual conversion is fairly easy:Beta Was this translation helpful? Give feedback.
All reactions