immutable_struct 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.mdown
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Theo Hultberg
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.mdown ADDED
@@ -0,0 +1,20 @@
1
+ # ImmutableStruct
2
+
3
+ An immutable implementation of Ruby's `Struct` class. It works just as Struct, but there are no setters, and you can't use `[]=`.
4
+
5
+ class Spaceship < ImmutableStruct.new(:name, :max_speed)
6
+ end
7
+
8
+ ship = Spaceship.new('Enterprise', 'Warp 9')
9
+
10
+ puts ship.max_speed # => Warp 9
11
+
12
+ ship.max_speed = '299792458 m/s' # raises NoMethodError
13
+
14
+ In addition to the above, `ImmutableStruct` enhances the constructor to accept a hash (but as you can see above the `Struct` behaviour works too):
15
+
16
+ ship = Spaceship.new(:max_speed => '∞', :name => 'The TARDIS')
17
+
18
+ puts ship.max_speed # => ∞
19
+
20
+ I'm surprised every time I look at the RDoc for `Struct` that it doesn't do this.
data/Rakefile ADDED
@@ -0,0 +1,48 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+
5
+ begin
6
+ require 'lib/immutable_struct'
7
+ require 'jeweler'
8
+ Jeweler::Tasks.new do |gem|
9
+ gem.name = 'immutable_struct'
10
+ gem.summary = %Q{An immutable version of Ruby's Struct class}
11
+ gem.description = %Q{An immutable implementation of Ruby's `Struct` class. It works just as Struct, but there are no setters, and you can't use `[]=`. As a bonus feature the constructor of classes inheriting from ImmutableStruct can take named parameters in the form of a hash.}
12
+ gem.email = 'theo@iconara.net'
13
+ gem.homepage = 'http://github.com/iconara/immutable_struct'
14
+ gem.authors = ['Theo Hultberg']
15
+ gem.add_development_dependency 'rspec', '>= 1.2.9'
16
+ gem.add_development_dependency 'yard', '>= 0'
17
+ gem.version = ImmutableStruct::VERSION
18
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
19
+ end
20
+ Jeweler::GemcutterTasks.new
21
+ rescue LoadError
22
+ puts 'Jeweler (or a dependency) not available. Install it with: gem install jeweler'
23
+ end
24
+
25
+ require 'spec/rake/spectask'
26
+ Spec::Rake::SpecTask.new(:spec) do |spec|
27
+ spec.libs << 'lib' << 'spec'
28
+ spec.spec_files = FileList['spec/**/*_spec.rb']
29
+ end
30
+
31
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
32
+ spec.libs << 'lib' << 'spec'
33
+ spec.pattern = 'spec/**/*_spec.rb'
34
+ spec.rcov = true
35
+ end
36
+
37
+ task :spec => :check_dependencies
38
+
39
+ task :default => :spec
40
+
41
+ begin
42
+ require 'yard'
43
+ YARD::Rake::YardocTask.new
44
+ rescue LoadError
45
+ task :yardoc do
46
+ abort 'YARD is not available. In order to run yardoc, you must: sudo gem install yard'
47
+ end
48
+ end
@@ -0,0 +1,40 @@
1
+ class ImmutableStruct
2
+ VERSION = '1.0.1'
3
+
4
+ def self.new(*attrs)
5
+ struct = Struct.new(*attrs)
6
+ make_immutable!(struct)
7
+ optionalize_constructor!(struct)
8
+ struct
9
+ end
10
+
11
+ private
12
+
13
+ def self.make_immutable!(struct)
14
+ struct.send(:undef_method, "[]=".to_sym)
15
+ struct.members.each do |member|
16
+ struct.send(:undef_method, "#{member}=".to_sym)
17
+ end
18
+ end
19
+
20
+ def self.optionalize_constructor!(struct)
21
+ struct.class_eval do
22
+ alias_method :struct_initialize, :initialize
23
+
24
+ def initialize(*attrs)
25
+ if attrs && attrs.size == 1 && attrs.first.is_a?(Hash)
26
+ struct_initialize(*members.map { |m| attrs.first[m.to_sym] })
27
+ else
28
+ struct_initialize(*attrs)
29
+ end
30
+ end
31
+
32
+ def to_h
33
+ members.inject({}) do |h, m|
34
+ h[m.to_sym] = self[m]
35
+ h
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,47 @@
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+
4
+ describe ImmutableStruct do
5
+
6
+ class ImmutableItem < ImmutableStruct.new(:a, :b); end
7
+
8
+ it 'can be used as a superclass just like Struct' do
9
+ obj = ImmutableItem.new(1, 2)
10
+ obj.a.should == 1
11
+ end
12
+
13
+ it 'creates no setters' do
14
+ obj = ImmutableItem.new(1, 2)
15
+ running { obj.a = 3 }.should raise_error
16
+ end
17
+
18
+ it 'does not allow [] to be used to mutate the instance' do
19
+ obj = ImmutableItem.new(1, 2)
20
+ running { obj[:a] = 3 }.should raise_error
21
+ end
22
+
23
+ it 'creates a constructor that works like the one Struct creates' do
24
+ obj = ImmutableItem.new(1, 2)
25
+ obj.a.should == 1
26
+ obj.b.should == 2
27
+ obj = ImmutableItem.new(1)
28
+ obj.a.should == 1
29
+ obj.b.should == nil
30
+ end
31
+
32
+ it 'creates a constructor that takes a hash' do
33
+ obj = ImmutableItem.new(:a => 1, :b => 2)
34
+ obj.a.should == 1
35
+ obj.b.should == 2
36
+ obj = ImmutableItem.new(:b => 2)
37
+ obj.a.should == nil
38
+ obj.b.should == 2
39
+ end
40
+
41
+ it 'adds #to_h that returns a hash representation of the object' do
42
+ obj = ImmutableItem.new(:a => 1, :b => 2)
43
+ obj.to_h.should have_key(:a)
44
+ obj.to_h.should have_key(:b)
45
+ end
46
+
47
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,13 @@
1
+ $: << File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+
3
+ require 'spec'
4
+ require 'spec/autorun'
5
+
6
+ require 'immutable_struct'
7
+
8
+
9
+ alias :running :lambda
10
+
11
+ Spec::Runner.configure do |config|
12
+
13
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: immutable_struct
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Theo Hultberg
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-03-13 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 13
30
+ segments:
31
+ - 1
32
+ - 2
33
+ - 9
34
+ version: 1.2.9
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: yard
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ type: :development
50
+ version_requirements: *id002
51
+ description: An immutable implementation of Ruby's `Struct` class. It works just as Struct, but there are no setters, and you can't use `[]=`. As a bonus feature the constructor of classes inheriting from ImmutableStruct can take named parameters in the form of a hash.
52
+ email: theo@iconara.net
53
+ executables: []
54
+
55
+ extensions: []
56
+
57
+ extra_rdoc_files:
58
+ - LICENSE
59
+ - README.mdown
60
+ files:
61
+ - .document
62
+ - .gitignore
63
+ - LICENSE
64
+ - README.mdown
65
+ - Rakefile
66
+ - lib/immutable_struct.rb
67
+ - spec/immutable_struct_spec.rb
68
+ - spec/spec.opts
69
+ - spec/spec_helper.rb
70
+ has_rdoc: true
71
+ homepage: http://github.com/iconara/immutable_struct
72
+ licenses: []
73
+
74
+ post_install_message:
75
+ rdoc_options:
76
+ - --charset=UTF-8
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ hash: 3
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ hash: 3
94
+ segments:
95
+ - 0
96
+ version: "0"
97
+ requirements: []
98
+
99
+ rubyforge_project:
100
+ rubygems_version: 1.3.7
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: An immutable version of Ruby's Struct class
104
+ test_files:
105
+ - spec/immutable_struct_spec.rb
106
+ - spec/spec_helper.rb