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

support dynamic attributes & merging collection values #172

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

danhodge
Copy link

This is the start of my proposed solution to the problem I outlined on the mailing list here. It doesn't add general purpose dynamic attribute, it just allows you to evaluate the 'parent' attribute of a nested attribute structure dynamically through a hacky naming convention (__ prefix on the attribute name). I would have rather used a block to evaluate the value of the dynamic attribute, but I wasn't sure what all of the the implications of creating a NestingExposure were.

Work in progress. Allows the parent attribute of a nested attribute
structure to be a dynamic attribute that is evaluated during
rendering. Adds a 'combine' option that can be used when exposing a
collection of items in a single presenter that controls how the
individual items are combined. No value maintains the existing behavior,
where each item is rendered as JSON and appended to the Array, while a
value of :merge results in each item's JSON representation being
Hash#merge'd into a single Hash.
@dblock
Copy link
Member

dblock commented Sep 15, 2015

I am personally not a huge fan of naming conventions. There should be an explicit way to tackle this. Try to propose a cleaner DSL? Maybe something as simple as an expose that doesn't take a name? What if the following exposed each field in the hash?

expose do
 { x: 1, y: 2 }
end

@marshall-lee
Copy link
Member

Hello!

This feature seems good, but:

a hacky naming convention (__ prefix on the attribute name).

Please don't!

It'll be better to implement it in terms of :as option:

expose :by_date, as: -> (obj, opts) { obj.date } do
  expose :first_name
  expose :last_name
end

Also the question is how it will work with :only and :except options?

@marshall-lee
Copy link
Member

And:

merging collection values

What is this? I don't see any merging in your code.

@danhodge
Copy link
Author

I only used the naming convention for prototyping purposes, I like the as: <lambda> approach much better. I'll switch to that approach. As for the merge, it happens here. Instead of mapping over the objects in the collection, it reduces them into a single Hash by merging them together. Perhaps reduce is a better term for what its doing?

@dblock
Copy link
Member

dblock commented Sep 15, 2015

@marshall-lee Does the as: syntax let you merge onto root at set of dynamic keys, doesn't look like that or am I missing something?

Can you comment on what you think about the key-less extension (expose with just a block)?

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

Successfully merging this pull request may close these issues.

None yet

3 participants