pb-serializer icon indicating copy to clipboard operation
pb-serializer copied to clipboard

Enable to give a hash object to initializer

Open NotFounds opened this issue 3 years ago • 0 comments

Why

I would often like to create a Pb object from a hash object. But a hash object does not allow dot access.

What

Add an argument to specify the access policy which uses dot access or symbol access. That uses OpenStruct internally.

e.g.

UserPbSerializer.new({
  id: 1234,
  name: "John Smith"
}, use_symbol_access: true)

alternatives Wrap an object by OpenStruct before giving it to the initializer.

e.g.

UserPbSerializer.new(OpenStruct.new({
  id: 1234,
  name: `John Smith`,
}))

I'm using this solution temporarily, but it's a little complicated :(

NotFounds avatar Dec 27 '21 09:12 NotFounds