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
add deep_freeze refinement to support #260
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
module Pakyow | ||
module Support | ||
module DeepFreeze | ||
refine Object.singleton_class do | ||
def unfreezable_variables | ||
@unfreezable_variables ||= [] | ||
end | ||
|
||
private | ||
|
||
def unfreezable(*ivars) | ||
ivars = ivars.map { |ivar| "@#{ivar}".to_sym } | ||
unfreezable_variables.concat(ivars) | ||
unfreezable_variables.uniq! | ||
end | ||
end | ||
|
||
refine Object do | ||
def deep_freeze | ||
return self if frozen? | ||
|
||
self.freeze | ||
|
||
freezable_variables.each do |name| | ||
instance_variable_get(name).deep_freeze | ||
end | ||
|
||
self | ||
end | ||
|
||
private | ||
|
||
def freezable_variables | ||
instance_variables - self.class.unfreezable_variables | ||
end | ||
end | ||
|
||
refine Array do | ||
def deep_freeze | ||
return self if frozen? | ||
|
||
self.freeze | ||
|
||
each(&:deep_freeze) | ||
|
||
self | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
require "pakyow/support/deep_freeze" | ||
|
||
using Pakyow::Support::DeepFreeze | ||
|
||
class MyObject | ||
unfreezable :fire | ||
|
||
attr_reader :fire, :water | ||
|
||
def initialize | ||
@fire = 'fire' | ||
@water = 'water' | ||
end | ||
end | ||
|
||
RSpec.describe Pakyow::Support::DeepFreeze do | ||
let :obj_with_unfreezable { | ||
MyObject.new | ||
} | ||
|
||
describe "deep_freeze" do | ||
it "returns the object" do | ||
obj = Object.new | ||
|
||
expect(obj.deep_freeze.object_id).to be(obj.object_id) | ||
end | ||
|
||
it "deep freezes an Object without ivars" do | ||
obj = Object.new | ||
obj.deep_freeze | ||
|
||
expect(obj).to be_frozen | ||
end | ||
|
||
it "deep freezes an Object with ivars" do | ||
ivar_value = Object.new | ||
obj = Object.new | ||
obj.instance_variable_set(:@ivar_name, ivar_value) | ||
|
||
obj.deep_freeze | ||
|
||
expect(obj).to be_frozen | ||
expect(ivar_value).to be_frozen | ||
end | ||
|
||
it "deep freezes ivars" do | ||
objects = Array.new(5) { Object.new } | ||
objects.reduce do |parent, child| | ||
parent.instance_variable_set(:@child, child) | ||
end | ||
|
||
objects.first.deep_freeze | ||
|
||
objects.each do |obj| | ||
expect(obj).to be_frozen | ||
end | ||
end | ||
|
||
it "deep freezes ivars without loop" do | ||
objects = Array.new(5, Object.new) | ||
objects.reduce do |parent, child| | ||
parent.instance_variable_set(:@child, child) | ||
end | ||
|
||
objects.first.deep_freeze | ||
|
||
objects.each do |obj| | ||
expect(obj).to be_frozen | ||
end | ||
end | ||
|
||
it "deep freezes array items" do | ||
objects = Array.new(5) { Object.new } | ||
|
||
objects.deep_freeze | ||
|
||
objects.each do |obj| | ||
expect(obj).to be_frozen | ||
end | ||
|
||
expect(objects).to be_frozen | ||
end | ||
|
||
it "doesn't freeze unfreezeable" do | ||
obj_with_unfreezable.deep_freeze | ||
|
||
expect(obj_with_unfreezable).to be_frozen | ||
expect(obj_with_unfreezable.water).to be_frozen | ||
expect(obj_with_unfreezable.fire).not_to be_frozen | ||
end | ||
|
||
it "returns an array of unfreezeable ivars" do | ||
vars = MyObject.unfreezable_variables | ||
|
||
expect(vars).to be_instance_of(Array) | ||
end | ||
end | ||
end | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe we also need to refine
Hash
to freeze values. Let me know if I'm missing something.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right you are. I just pushed an addition that refines Hash. I'm freezing keys and values. Do you think that is the right behavior? Or should it only be values?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, keys/values make sense.