Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deep merging of nested exposures and presenter exposures (with :using option) #154

Open
marshall-lee opened this issue Jul 23, 2015 · 2 comments

Comments

@marshall-lee
Copy link
Member

Suppose this case:

class GeneralInfo < Grape::Entity
  expose :gender, :age
end

class Student < Grape::Entity
  expose :info, using: GeneralInfo
  expose :info do
    expose :grade
  end
end

Student.represent({info: { gender: 'male', age: 25 }, grade: 5 }, serializable: true)
# => {:info=>{:grade=>5}}

But I assume info[:gender] and info[:age] to be exposed too. Also:

class Person < Grape::Entity
  expose :info do
    expose(:gender) { |obj| object[:info][:gender] }
    expose(:age) { |obj| object[:info][:age] }
  end
end

class Student < Person
  expose :info do
    expose :grade
  end
end

Student.represent({info: { gender: 'male', age: 25 }, grade: 5 }, serializable: true)
# => {:info=>{:gender=>"male", :age=>25, :grade=>5}}

Since we decided not to rewrite previously defined exposures in #151 then more consistent behavior would be make these cases act identical. It's good to think of presenter exposures (with :using option) as a nested exposures incapsulated in the separate classes.

Ones who want an old rewriting behavior should use conditional exposures (:if, :unless options).

@marshall-lee
Copy link
Member Author

On the other hand I expect from exposures that emit plain Hash values a rewriting behavior:

class Something < Grape::Entity
  expose :info do
    expose :x
    expose :y
  end
  expose :info do |obj, opts|
    { z: 123 }
  end
end

Something.represent({ x: 1, y: 2 }, serializable: true)
# => {:info=>{:z=>123}}

This is because second :info exposure is not nesting despite the fact that it exposes a hash too.

@marshall-lee
Copy link
Member Author

But maybe it's a not so useful feature as I think. There's also a problem with what to do if represent returns an Array of entities. The possibility of emitting an array makes this feature counterintuitive.

It's also very problematic to implement so I don't plan to do it in #151. Maybe later I'll rethink it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants