Skip to content

[LazyBlock] Allows using complex expressions with configuration#1931

Merged
dblock merged 6 commits into
ruby-grape:masterfrom
myxoh:lazy_block
Nov 14, 2019
Merged

[LazyBlock] Allows using complex expressions with configuration#1931
dblock merged 6 commits into
ruby-grape:masterfrom
myxoh:lazy_block

Conversation

@myxoh

@myxoh myxoh commented Nov 14, 2019

Copy link
Copy Markdown
Member

This PR is motivated by a bug I've introduced on our system.
The problem, when you are trying to waterfall a configuration, is that LazyValue doesn't play nicely inside an expression so this code

value = configuration[:attribute] || 'default_value' 

Is evaluated when the class is load as: configuration[:attribute] => LazyValue || 'default_value' => LazyValue
Which means that even when you mount the API with attribute: nil the value will equal nil rather than 'default_value' as a developer might expect.

To mitigate this, I've introduced a lazy block, so you are able to put that expression within a mounted block which will only be evaluated when the API is mounted, and furthermore, will prevent methods using the response of the expression to running before the API is mounted

Comment thread README.md Outdated
@dblock

dblock commented Nov 14, 2019

Copy link
Copy Markdown
Member

👍

@dblock dblock merged commit c3de184 into ruby-grape:master Nov 14, 2019
@dblock

dblock commented Nov 14, 2019

Copy link
Copy Markdown
Member

Merged

basjanssen pushed a commit to basjanssen/grape that referenced this pull request Feb 28, 2020
…-grape#1931)

* Introduces the concept of a LazyBlock which gets excecuted on mount

* Fixes LazyBlock within a namespace

* Adds more specs so that given can also be used for conditional lazy blocks

* Documents expressions

* Adds a changelog

* Update README.md
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.

2 participants