iso-init 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ # encoding: utf-8
2
+ source 'https://rubygems.org'
3
+
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Ryan Biesemeyer
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,72 @@
1
+ # Iso::Init
2
+
3
+ This is a gem to help Isolate object initialization.
4
+
5
+ What's wrong with the following?
6
+
7
+ ```ruby
8
+ SomeKlass = Class.new(Struct.new(options))
9
+ options = {foo: bar}
10
+ object = SomeKlass.new(options)
11
+ object.options
12
+ # => { :foo => :bar }
13
+ options[:baz] = bingo # DANGER, WILL ROBINSON
14
+ object.options
15
+ # => { :foo => :bar, :baz : bingo }
16
+ ```
17
+
18
+ That's right. You passed an object by reference, so when that object changed,
19
+ all references to it also changed, which was not what you intended to do.
20
+
21
+ `IsoInit` fixes that.
22
+
23
+ ## Installation
24
+
25
+ Add this line to your application's Gemfile:
26
+
27
+ gem 'iso-init'
28
+
29
+ And then execute:
30
+
31
+ $ bundle
32
+
33
+ Or install it yourself as:
34
+
35
+ $ gem install iso-init
36
+
37
+ ## Usage
38
+
39
+ ```ruby
40
+ SomeKlass = Class.new(Struct.new(options)) { include IsoInit }
41
+ options = {foo: bar}
42
+ object = SomeKlass.new(options)
43
+ object.options
44
+ # => { :foo => :bar }
45
+ options[:baz] = bingo # DANGER, WILL ROBINSON ???
46
+ object.options
47
+ # => { :foo => :bar }
48
+ # Sweet! It didn't change!
49
+ object.options.frozen?
50
+ # => true
51
+ # ok. that's what I was hoping for. yay.
52
+ ```
53
+
54
+ If an object is immutable, it won't be dup'd (e.g., `true`, `nil`, `1`, `3.1415926`), but if it is mutable, `initialize` will be called with a frozen duplicate.
55
+
56
+ Sweeeeet.
57
+
58
+ ## Contributing
59
+
60
+ 1. Fork it
61
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
62
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
63
+ 4. Push to the branch (`git push origin my-new-feature`)
64
+ 5. Create new Pull Request
65
+
66
+
67
+ ## License
68
+
69
+ Dual-licensed under [MIT][] and [WTFPL][].
70
+
71
+ [MIT]: http://en.wikipedia.org/wiki/MIT_License
72
+ [WTFPL]: http://en.wikipedia.org/wiki/WTFPL
@@ -0,0 +1,9 @@
1
+ # encoding: utf-8
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rspec/core'
5
+ require 'rspec/core/rake_task'
6
+ RSpec::Core::RakeTask.new(:spec) do |spec|
7
+ spec.pattern = FileList['spec/**/*_spec.rb']
8
+ spec.verbose = true
9
+ end
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'iso-init/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'iso-init'
8
+ spec.version = IsoInit::VERSION
9
+ spec.authors = ['Ryan Biesemeyer']
10
+ spec.email = ['ryan@yaauie.com']
11
+ spec.description = 'Attempts to isolate initialize arguments'
12
+ spec.summary = <<-EOSUMMARY
13
+ Where possible, runs initialize with frozen duplicates of the
14
+ original arguments.
15
+ EOSUMMARY
16
+ spec.homepage = 'https://github.com/yaauie/iso-init'
17
+ spec.license = 'MIT'
18
+
19
+ spec.files = `git ls-files`.split($/)
20
+ spec.executables = spec.files.grep(/^bin\//) { |f| File.basename(f) }
21
+ spec.test_files = spec.files.grep(/^(test|spec|features)\//)
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.3'
25
+ spec.add_development_dependency 'rake'
26
+ spec.add_development_dependency 'rspec'
27
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+ require 'iso-init/version'
3
+
4
+ module IsoInit
5
+ def self.included(base)
6
+ base.extend(Isolator)
7
+ end
8
+
9
+ module Isolator
10
+ def new(*args, &block)
11
+ super(*args.map(&(Isolator.method(:isolate))), &block)
12
+ end
13
+
14
+ def self.isolate(arg)
15
+ arg.dup.freeze rescue arg
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,4 @@
1
+ # encoding: utf-8
2
+ module IsoInit
3
+ VERSION = '0.0.1'
4
+ end
@@ -0,0 +1,33 @@
1
+ # encoding: utf-8
2
+ require 'iso-init'
3
+ require_relative 'spec_helper.rb'
4
+
5
+ describe IsoInit do
6
+
7
+ let(:isolated_class_args) do
8
+ {
9
+ string: 'a string',
10
+ integer: 1,
11
+ nil_class: nil,
12
+ false_class: false,
13
+ true_class: true,
14
+ float: 1.1,
15
+ array: [1,2,3],
16
+ hash: { a: 1 },
17
+ }
18
+ end
19
+ let(:isolated_class) do
20
+ Class.new(Struct.new(*isolated_class_args.keys)) do
21
+ include IsoInit
22
+ end
23
+ end
24
+ let(:isolated_instance) { isolated_class.new(*isolated_class_args.values) }
25
+
26
+ it 'should isolate anything that can be dup\'d' do
27
+ isolated_class_args.keys.each do |key|
28
+ isolated_instance.public_send(key).should(
29
+ be_a_frozen_duplicate_of isolated_class_args[key]
30
+ ) if dupable?(isolated_class_args[key])
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+ RSpec::Matchers.define :be_a_frozen_duplicate_of do |expected|
3
+ match do |actual|
4
+ next false unless actual == expected
5
+ next false unless actual.frozen?
6
+ next false if actual.equal?(expected) # refer to same object?
7
+ true
8
+ end
9
+ failure_message_for_should do |actual|
10
+ "expected that #{actual} would be a frozen duplicate of #{expected}"
11
+ end
12
+ end
13
+
14
+ module Helpers
15
+ def dupable?(obj)
16
+ obj.dup
17
+ true
18
+ rescue
19
+ false
20
+ end
21
+ end
22
+
23
+ RSpec.configure do |c|
24
+ c.include Helpers
25
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: iso-init
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ryan Biesemeyer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Attempts to isolate initialize arguments
63
+ email:
64
+ - ryan@yaauie.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - iso-init.gemspec
75
+ - lib/iso-init.rb
76
+ - lib/iso-init/version.rb
77
+ - spec/iso_init_spec.rb
78
+ - spec/spec_helper.rb
79
+ homepage: https://github.com/yaauie/iso-init
80
+ licenses:
81
+ - MIT
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 1.8.24
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Where possible, runs initialize with frozen duplicates of the original arguments.
104
+ test_files:
105
+ - spec/iso_init_spec.rb
106
+ - spec/spec_helper.rb
107
+ has_rdoc: