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

Attribute Aliases #43

Closed
wants to merge 5 commits into from
Closed

Attribute Aliases #43

wants to merge 5 commits into from

Conversation

iamvery
Copy link
Contributor

@iamvery iamvery commented Apr 18, 2019

Problem

Let's say you want to adjust the trajectory of a value object, but there are existing external dependencies on its interface. How can you begin the process of renaming without breaking compatibility?

Solution

Provide a mechanism for aliasing attributes. This allows you to take the interface of a data object in a different direction without breaking existing references to it. Additionally, aliases might provide a hook which is invoked when they are referenced so that you may log usage and iterate towards their deprecation and eventual removal.

@@ -81,8 +81,25 @@ def self.new(*attributes,&block)
end
end

@@aliases = {}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've never really used a class variable in Ruby before, but I think it's appropriate here. It's roughly equivalent (in my mind) to module attributes from Elixir (for lack of a better example).

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please double check if it's thread safe in your use case.

@@aliases = {}

def self.alias_attribute(aliaz, attr, &block)
@@aliases.store(aliaz, [attr, block])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably "primitive obsession", but I had a hard time convincing myself that we really needed a custom type for this data... A likely causing factor of primitive obsession 😅 💦

@@aliases.store(aliaz, [attr, block])
end

def method_missing(m, *_args, &_block)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The build failed initially with the signature method_missing(m, *_, &_). I assume it's because Ruby doesn't like the underscores in this case, but I didn't dig all the way down on the cause of failure.

@@aliases.store(aliaz, [attr, block])
end

def method_missing(m, *_args, &_block)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I override respond_to? also?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually consider def respond_to_missing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the correction 👍

private

def map_aliases(attrs)
attrs.transform_keys { |k|
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh oh, looks like I didn't realize how new Hash#transform_keys is.

@iamvery
Copy link
Contributor Author

iamvery commented Apr 18, 2019

Closing this out after having some internal discussion. The pain that pushed me to come up with this solution seems more localized than I had originally thought.

@iamvery iamvery closed this Apr 18, 2019
@iamvery iamvery deleted the aliases branch April 18, 2019 16:07
@iamvery
Copy link
Contributor Author

iamvery commented Apr 19, 2019

@januszm Thanks for the review! We decided this isn't a desirable feature for a library that's meant to stay lean.

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