as_readonly 1.0.0

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
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in as_readonly.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Robert Pankowecki
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,135 @@
1
+ # AsReadonly
2
+
3
+ Expose your collections as readonly objects without fear.
4
+
5
+
6
+ ## Example
7
+
8
+ ```ruby
9
+ ara = AsReadonly::Array.new([1,2])
10
+ ara[0] # => 1
11
+ ara.clear # raises NoMethodError
12
+ ```
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's `Gemfile`:
17
+
18
+ gem 'as_readonly'
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install as_readonly
27
+
28
+ ## Require
29
+
30
+ * `as_readonly/array`
31
+
32
+ AsReadonly::Array
33
+
34
+ * `as_readonly/hash`
35
+
36
+ AsReadonly::Hash
37
+
38
+ * `as_readonly/set`
39
+
40
+ AsReadonly::Set
41
+
42
+ * `as_readonly/collections`
43
+
44
+ as_readonly/array
45
+ as_readonly/hash
46
+ as_readonly/set`
47
+
48
+ * `as_readonly/core_ext/array`
49
+
50
+ array.as_readonly # => AsReadonly::Array.new(array)
51
+
52
+ * `as_readonly/core_ext/hash`
53
+
54
+ hash.as_readonly # => AsReadonly::Hash.new(hash)
55
+
56
+ * `as_readonly/core_ext/set`
57
+
58
+ set.as_readonly # => AsReadonly::Set.new(set)
59
+
60
+ * `as_readonly/core_ext/collections`
61
+
62
+ as_readonly/core_ext/array
63
+ as_readonly/core_ext/hash
64
+ as_readonly/core_ext/set
65
+
66
+ * `as_readonly/core_ext/module`
67
+
68
+ Module#attr_readonly
69
+
70
+ class MyClass
71
+ attr_readonly :array
72
+ def initialize
73
+ @array = []
74
+ end
75
+ end
76
+
77
+ MyClass.new.array.size => 0
78
+ MyClass.new.array.clear => NoMethodError exception
79
+
80
+ * `as_readonly/core_ext/all`
81
+
82
+ as_readonly/core_ext/collections
83
+ as_readonly/core_ext/module
84
+
85
+ * `as_readonly` - everything above
86
+
87
+
88
+ ## Usage
89
+
90
+ ```ruby
91
+ require 'as_readonly/array'
92
+
93
+ class MyClass
94
+ def initialize
95
+ @collection = []
96
+ end
97
+
98
+ def collection
99
+ @read_collection ||= AsReadonly::Array.new(@collection)
100
+ end
101
+ end
102
+ ```
103
+
104
+ ## Note
105
+
106
+ This gem does not prevent calling mutating methods on collection elements. Obviously.
107
+
108
+ ```ruby
109
+ class MyClass
110
+ def initialize
111
+ @collection = ["asd"]
112
+ end
113
+
114
+ def collection
115
+ @read_collection ||= AsReadonly::Array.new(@collection)
116
+ end
117
+ end
118
+
119
+ c = MyClass.new
120
+ c.collection[0].upcase
121
+ ```
122
+
123
+ ## Inspirations
124
+
125
+ * http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/144294
126
+ * http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/144375
127
+ * http://msdn.microsoft.com/en-us/library/e78dcd75.aspx
128
+
129
+ ## Contributing
130
+
131
+ 1. Fork it
132
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
133
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
134
+ 4. Push to the branch (`git push origin my-new-feature`)
135
+ 5. Create new Pull Request
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new(:test) do |test|
6
+ test.pattern = 'test/**/*_test.rb'
7
+ test.libs << 'lib' << 'test'
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/as_readonly/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Robert Pankowecki"]
6
+ gem.email = ["robert.pankowecki@gmail.com"]
7
+ gem.description = %q{Safely expose collections as readonly objects}
8
+ gem.summary = %q{Safley expose collections}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "as_readonly"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = AsReadonly::VERSION
17
+ end
@@ -0,0 +1,5 @@
1
+ module AsReadonly
2
+ end
3
+
4
+ require "as_readonly/version"
5
+ require "as_readonly/core_ext/all"
@@ -0,0 +1,22 @@
1
+ require 'delegate'
2
+
3
+ module AsReadonly
4
+ class Array < DelegateClass(::Array)
5
+ REMOVED = %w|
6
+ << []= clear concat delete delete_at delete_if
7
+ flatten! insert map! pop push reject! replace
8
+ reverse! shift slice! sort! uniq! unshift fill
9
+ |.each do |method|
10
+ undef_method(method)
11
+ end
12
+
13
+ def method_missing(*params)
14
+ raise NoMethodError
15
+ end
16
+
17
+ def respond_to?(method, include_private = false)
18
+ return false if REMOVED.include?(method.to_s)
19
+ super
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ require 'as_readonly/array'
2
+ require 'as_readonly/hash'
3
+ require 'as_readonly/set'
@@ -0,0 +1,2 @@
1
+ require 'as_readonly/core_ext/collections'
2
+ require 'as_readonly/core_ext/module'
@@ -0,0 +1,7 @@
1
+ require 'as_readonly/array'
2
+
3
+ class Array
4
+ def as_readonly
5
+ AsReadonly::Array.new(self)
6
+ end
7
+ end
@@ -0,0 +1,4 @@
1
+ require 'as_readonly/core_ext/array'
2
+ require 'as_readonly/core_ext/hash'
3
+ require 'as_readonly/core_ext/set'
4
+
@@ -0,0 +1,7 @@
1
+ require 'as_readonly/hash'
2
+
3
+ class Hash
4
+ def as_readonly
5
+ AsReadonly::Hash.new(self)
6
+ end
7
+ end
@@ -0,0 +1,15 @@
1
+ require 'as_readonly/collections'
2
+
3
+ class Module
4
+ def attr_readonly(*attributes)
5
+ attributes.each do |attr|
6
+ module_eval <<-"end_eval", __FILE__, __LINE__
7
+ def #{attr}
8
+ @_#{attr}_as_readonly ||= begin
9
+ @#{attr} ? @#{attr}.as_readonly : @#{attr}
10
+ end
11
+ end
12
+ end_eval
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,7 @@
1
+ require 'as_readonly/set'
2
+
3
+ class Set
4
+ def as_readonly
5
+ AsReadonly::Set.new(self)
6
+ end
7
+ end
@@ -0,0 +1,28 @@
1
+ require 'delegate'
2
+
3
+ module AsReadonly
4
+ # Be aware that #[] and #default are not blacklisted here
5
+ # but they can change the internal state of Hash if default
6
+ # proc is implemented to change the Hash itself:
7
+ #
8
+ # h = Hash.new {|h,k| h[k] = k*k }
9
+ class Hash < DelegateClass(::Hash)
10
+ REMOVED = %w|
11
+ []= clear default= default_proc=
12
+ delete delete_if replace keep_if
13
+ merge! update rehash reject!
14
+ select! shift store
15
+ |.each do |method|
16
+ undef_method(method)
17
+ end
18
+
19
+ def method_missing(*params)
20
+ raise NoMethodError
21
+ end
22
+
23
+ def respond_to?(method, include_private = false)
24
+ return false if REMOVED.include?(method.to_s)
25
+ super
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,22 @@
1
+ require 'set'
2
+ require 'delegate'
3
+
4
+ module AsReadonly
5
+ class Set < DelegateClass(::Set)
6
+ REMOVED = %w|
7
+ << add add? clear collect! map! delete delete?
8
+ delete_if flatten! keep_if merge reject! select!
9
+ |.each do |method|
10
+ undef_method(method)
11
+ end
12
+
13
+ def method_missing(*params)
14
+ raise NoMethodError
15
+ end
16
+
17
+ def respond_to?(method, include_private = false)
18
+ return false if REMOVED.include?(method.to_s)
19
+ super
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ module AsReadonly
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,18 @@
1
+ require 'test_helper'
2
+ require 'as_readonly/array'
3
+
4
+ class ArrayTest < MiniTest::Unit::TestCase
5
+ def setup
6
+ @array = AsReadonly::Array.new(@target = [])
7
+ end
8
+
9
+ def test_safe_method_delegated
10
+ assert_equal 0, @array.size
11
+ assert @array.respond_to?(:map)
12
+ end
13
+
14
+ def test_unsafe_method_removed
15
+ refute @array.respond_to?(:map!)
16
+ assert_raises(NoMethodError) { @array.map!{|x| x } }
17
+ end
18
+ end
@@ -0,0 +1,39 @@
1
+ require 'test_helper'
2
+ require 'as_readonly/core_ext/all'
3
+
4
+ class CoreExtTest < MiniTest::Unit::TestCase
5
+ def test_array_ext
6
+ assert_instance_of AsReadonly::Array, [].as_readonly
7
+ end
8
+
9
+ def test_hash_ext
10
+ assert_instance_of AsReadonly::Hash, {}.as_readonly
11
+ end
12
+
13
+ def test_set_ext
14
+ assert_instance_of AsReadonly::Set, Set.new.as_readonly
15
+ end
16
+
17
+ class TestClass
18
+ attr_readonly :hash, :array, :set
19
+ def initialize
20
+ @hash = {}
21
+ @array = []
22
+ @set = Set.new
23
+ end
24
+ end
25
+
26
+ def test_attr_readonly_value
27
+ tc = TestClass.new
28
+ assert_instance_of AsReadonly::Array, tc.array
29
+ assert_instance_of AsReadonly::Hash, tc.hash
30
+ assert_instance_of AsReadonly::Set, tc.set
31
+ end
32
+
33
+ def test_attr_readonly_cache
34
+ tc = TestClass.new
35
+ assert_equal tc.array.object_id, tc.array.object_id
36
+ refute tc.array.object_id == tc.instance_variable_get(:@array).object_id
37
+ end
38
+ end
39
+
@@ -0,0 +1,34 @@
1
+ require 'test_helper'
2
+ require 'as_readonly/hash'
3
+
4
+ class HashTest < MiniTest::Unit::TestCase
5
+ def setup
6
+ @hash = AsReadonly::Hash.new(@target = {})
7
+ end
8
+
9
+ def test_safe_method_delegated
10
+ assert_equal 0, @hash.size
11
+ assert @hash.respond_to?(:map)
12
+ end
13
+
14
+ def test_unsafe_method_removed
15
+ refute @hash.respond_to?(:clear)
16
+ assert_raises(NoMethodError) { @hash.clear }
17
+ end
18
+
19
+ def test_get
20
+ @target = Hash.new {|h,k| h[k] = k*k }
21
+ @hash = AsReadonly::Hash.new(@target)
22
+ assert_equal 0, @hash.size
23
+ @hash[10]
24
+ assert_equal 1, @hash.size
25
+ end
26
+
27
+ def test_default
28
+ @target = Hash.new {|h,k| h[k] = k*k }
29
+ @hash = AsReadonly::Hash.new(@target)
30
+ assert_equal 0, @hash.size
31
+ @hash.default(10)
32
+ assert_equal 1, @hash.size
33
+ end
34
+ end
@@ -0,0 +1,19 @@
1
+ require 'test_helper'
2
+ require 'as_readonly/set'
3
+
4
+ class SetTest < MiniTest::Unit::TestCase
5
+ def setup
6
+ @set = AsReadonly::Set.new(@target = Set.new)
7
+ end
8
+
9
+ def test_safe_method_delegated
10
+ assert_equal 0, @set.size
11
+ assert @set.respond_to?(:map)
12
+ end
13
+
14
+ def test_unsafe_method_removed
15
+ refute @set.respond_to?(:map!)
16
+ assert_raises(NoMethodError) { @set.map!{|x| x } }
17
+ end
18
+ end
19
+
@@ -0,0 +1,2 @@
1
+ require 'minitest/unit'
2
+ require 'minitest/autorun'
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: as_readonly
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Robert Pankowecki
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-15 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Safely expose collections as readonly objects
15
+ email:
16
+ - robert.pankowecki@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.md
25
+ - Rakefile
26
+ - as_readonly.gemspec
27
+ - lib/as_readonly.rb
28
+ - lib/as_readonly/array.rb
29
+ - lib/as_readonly/collections.rb
30
+ - lib/as_readonly/core_ext/all.rb
31
+ - lib/as_readonly/core_ext/array.rb
32
+ - lib/as_readonly/core_ext/collections.rb
33
+ - lib/as_readonly/core_ext/hash.rb
34
+ - lib/as_readonly/core_ext/module.rb
35
+ - lib/as_readonly/core_ext/set.rb
36
+ - lib/as_readonly/hash.rb
37
+ - lib/as_readonly/set.rb
38
+ - lib/as_readonly/version.rb
39
+ - test/array_test.rb
40
+ - test/core_ext_test.rb
41
+ - test/hash_test.rb
42
+ - test/set_test.rb
43
+ - test/test_helper.rb
44
+ homepage: ''
45
+ licenses: []
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ segments:
57
+ - 0
58
+ hash: -2462703183450842111
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ segments:
66
+ - 0
67
+ hash: -2462703183450842111
68
+ requirements: []
69
+ rubyforge_project:
70
+ rubygems_version: 1.8.23
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Safley expose collections
74
+ test_files:
75
+ - test/array_test.rb
76
+ - test/core_ext_test.rb
77
+ - test/hash_test.rb
78
+ - test/set_test.rb
79
+ - test/test_helper.rb