Separate condition evaluations for same attribute

Open
#52 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
ruby
Domain
api

Research direction

Start by tracing how conditional exposures are registered and stored in the exposures hash. Review the specs that access exposure keys directly, then determine how multiple definitions for the same attribute can be evaluated without breaking those expectations. Done means both :short and :full conditions select the appropriate value while existing specs remain valid.

Written by the indexing model from the issue text.

Description

feature question

Consider the following:

with_options if: { kind: :short } do
  expose :name do |obj, opts|
    "#{obj.first_name[0]}. #{obj.last_name}"
  end
end

with_options if: { kind: :full } do
  expose :name do |obj, opts|
    "#{obj.first_name} #{obj.last_name}"
  end
end

Based on the above, I would expect that if :kind is :short then the value of :name will be the first initial and last name, and if :kind is :full then the value will be the first and last name.

However, since these two exposures use the same attribute name, it's not possible to evaluated them both. The :name attribute only retains the conditional options that are defined last because they replace any previously defined options for the :name key in the the exposures hash.

This means that (for the code above) if :kind is :short then the :name attribute will not be exposed at all.

My first idea was to append some sort of unique identifier to each key in the exposures hash, but this produced a bunch of errors in the specs because they access the keys in the exposures hash directly to test values, and do not know beforehand what unique ID will be added.

Any thoughts?

Dominant language
Ruby
Stars
729
Forks
154
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ruby-grape/grape-entity

All issues in ruby-grape/grape-entity

Similar issues

More Ruby issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.