Skip to content

Loading…

Add method to simplest the NoFreeze system by adding a class method #4

Closed
wants to merge 1 commit into from

3 participants

@shingara

If you want not freeze some namespace you can do

IceNine.no_freeze([RSpec, Mocha])

fixed #2

@mbj
Collaborator
mbj commented

I really like to get the problem solved, but I do not like we are dynamically modifying global state. Also when this state is the class hierarchy of IceNine.

What about a factory that returns an IceNine / Adamantium module that behaves like we want?

module MyApp
  Immutable = Adamantium.build(:no_freeze => [...], :memoizer => :freeze, ...)

  class DomainEntity
    include Immutable
  end
end
@shingara

I test the code with const_set and seems not work in regard of my spec

@dkubb dkubb commented on an outdated diff
lib/ice_nine.rb
@@ -42,4 +42,12 @@ def self.deep_freeze(object)
end
end
+ def self.no_freeze(mods)
@dkubb Owner
dkubb added a note

You could write this as def self.no_freeze(*mods), then you wouldn't have to use Array(mods) below.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@dkubb
Owner

@mbj that's an interesting case. I do lean toward figuring out how to do what you suggest, it's much nicer for testing and keeps things out of global state. Maybe we should all discuss how that would work and if it makes sense here or in Adamantium.

@shingara

I rebase my pull request with all of comment do on previous pull request

@mbj
Collaborator

Closing this PR for no activity.

@mbj mbj closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
View
10 README.md
@@ -22,6 +22,16 @@ object = Object.new
object.deep_freeze
```
+If you want avoid deep_freeze some object in Namespace, you can use
+`IceNine.no_freeze` class method.
+
+By example if you want avoid not freeze the namespace RSpec and Mocha,
+you can do
+
+```ruby
+IceNine.no_freeze([RSpec, Mocha])
+```
+
## Installation
With Rubygems:
View
7 lib/ice_nine.rb
@@ -42,4 +42,11 @@ def self.deep_freeze(object)
end
end
+ def self.no_freeze(*mods)
+ mods.each do |mod|
+ klass = Class.new(IceNine::Freezer::NoFreeze)
+ IceNine::Freezer.const_set(mod.to_s, klass)
+ end
+ end
+
end # module IceNine
View
20 spec/integration/ice_nine/class_methods/deep_freeze_spec.rb
@@ -229,3 +229,23 @@
end
end
end
+
+describe IceNine, 'no_freeze' do
+ module Mocha;
+ class Object; end
+ end
+
+ let(:object) { IceNine }
+ let(:value) { Mocha }
+
+ subject { object.no_freeze(value) }
+
+ it 'avoid freeze class in this namespace' do
+ subject
+ IceNine.deep_freeze(Mocha::Object.new).should_not be_frozen
+ end
+
+ after do
+ IceNine::Freezer.send(:remove_const, :Mocha)
+ end
+end
View
48 spec/unit/ice_nine/class_methods/deep_freeze_spec.rb
@@ -45,3 +45,51 @@
end
end
end
+
+describe IceNine, '.no_freeze' do
+ module Mocha; end
+ module Foo; end
+
+ let(:object) { IceNine }
+ let(:value) { Mocha }
+
+ subject { object.no_freeze(*value) }
+
+ context "with one constant args pass" do
+ before do
+ IceNine::Freezer.constants.should_not include(:Mocha)
+ end
+
+ it 'mark namespace like NoFreeze' do
+ subject
+ IceNine::Freezer.constants.should include(:Mocha)
+ end
+
+ after do
+ IceNine::Freezer.send(:remove_const, :Mocha)
+ end
+ end
+
+ context "with several namespace pass in args" do
+
+ let(:value) { [Mocha, Foo] }
+
+ before do
+ IceNine::Freezer.constants.should_not include(:Mocha)
+ IceNine::Freezer.constants.should_not include(:Foo)
+ end
+
+ it 'mark namespace like NoFreeze' do
+ subject
+ IceNine::Freezer.constants.should include(:Mocha)
+ IceNine::Freezer.constants.should include(:Foo)
+ end
+
+ after do
+ IceNine::Freezer.send(:remove_const, :Mocha)
+ IceNine::Freezer.send(:remove_const, :Foo)
+ end
+
+ end
+
+end
Something went wrong with that request. Please try again.