Writing dynamic exposures by overriding initialize
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 35/100
Research direction
Start in entity.rb at line 175 and inspect how exposures are appended to root_exposures. Reproduce the duplicate-exposure behavior described in the issue and review the reported test coverage. Done means repeated dynamic exposure initialization no longer degrades performance and regression coverage passes.
Written by the indexing model from the issue text.
Description
I've got a project that was still running 0.4.8 where, for example, we have Arrival and Departure types of Flight records, and the consumer wanted fields labelled as such. Rather than writing a bunch of boilerplate like:
expose :blah, as: :departure_blah, if: if: lambda {|o,_p| o.type == "Departure" }
I overrode the entity child class's initialize method:
def initialize(object, options = {})
if object
self.class.expose :blah, as: "#{type}_blah"
end
super(object, options)
end
This worked fine but after (finally) upgrading to 0.6.0 this started causing duplicate exposures to be added the root_exposures class variable, steadily degrading performance over time. Changing line 175 of entity.rb to root_exposures << exposure unless root_exposures.include?(exposure) fixes the issue, I've got test coverage written for that but following along the instructions for contributing I just get permission denied errors when attempting to push my branch.
Looking at that old code I realize that was an awful kludgey way of going about it.
- Dominant language
- Ruby
- Stars
- 729
- Forks
- 154
- PR merge metrics
- No merged PRs in 30d
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from ruby-grape/grape-entity
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
ruby-grape/grape-entity#386 · 7 comments · 1 reaction ·
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
ruby-grape/grape-entity#382 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
ruby-grape/grape-entity#380 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
ruby-grape/grape-entity#373 · 2 comments ·
-
Truncate Formatter Open
Difficulty 1/5 1-3 hours Newbie friendliness 45/100
ruby-grape/grape-entity#372 ·
All issues in ruby-grape/grape-entity
Similar issues
-
バグ
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
voxpupuli/puppet-epel#186 · 1 comment ·
-
external_created_at is no longer used for the message timestamp since the new message UI (v4.4.0) OpenBug Frontend
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
TheOdinProject/curriculum#31402 · 1 comment ·
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100