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

Pass options to getters and setters #194

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

Pass options to getters and setters #194

wants to merge 6 commits into from

Conversation

aldesantis
Copy link

@aldesantis aldesantis commented Aug 29, 2016

@apotonick as per our email conversation.

This PR allows to do the following:

class SongDecorator < Representable::Decorator
  property :title, exec_context: :decorator

  def title(user_options:, **)
    user_options[:upcase_title] ? represented.title.upcase : represented.title
  end
end

The same can be done with setters:

class SongDecorator < Representable::Decorator
  property :title, exec_context: :decorator

  def title=(input, user_options:, **)
    super(user_options[:upcase_title] ? input.upcase : input)
  end
end

In addition to accepting the user_options keyword argument, the methods can also accept options, if the full options hash is needed.

I am not sure if there's a better approach to it.

Also not sure if a test would be required or if the existing tests are fine. I've run into an issue writing a test for it because the test representer (#representer!) seems to disregard the exec_context option for properties and always call getters and setters on the decorated object instead.

@aldesantis
Copy link
Author

@apotonick bump?

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.

1 participant