readonly 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.codeclimate.yml +30 -0
- data/.gitignore +16 -0
- data/.rspec +2 -0
- data/.rubocop.yml +1156 -0
- data/.travis.yml +9 -0
- data/.yardopts +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +90 -0
- data/Rakefile +16 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/readonly.rb +20 -0
- data/lib/readonly/delegate_safe_methods.rb +13 -0
- data/lib/readonly/hash.rb +34 -0
- data/lib/readonly/proxy.rb +19 -0
- data/lib/readonly/version.rb +3 -0
- data/readonly.gemspec +30 -0
- metadata +150 -0
data/.travis.yml
ADDED
data/.yardopts
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Konstantin Gredeskoul
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# ReadOnly — Wrap Any Class in a Readonly Proxy
|
2
|
+
|
3
|
+
This gem is meant as a fail-safe to guard certain data against
|
4
|
+
modification, and to enforce immutability to a certain extent.
|
5
|
+
|
6
|
+
Of course nothing can be completely immutable in ruby, but by using this
|
7
|
+
gem, and at the very least, the unintentional mutation of important
|
8
|
+
objects can be prevented.
|
9
|
+
|
10
|
+
## Usage
|
11
|
+
|
12
|
+
The gem works for any class by asking the user to define which of the methods
|
13
|
+
on the original objects can be safely delegated to the object itself,
|
14
|
+
and which of the methods (the mutable ones) should not be.
|
15
|
+
|
16
|
+
When wrapping a class, one must do three things in the following order:
|
17
|
+
|
18
|
+
1. include module `ReadOnly::Proxy`
|
19
|
+
2. call class method `safe_methods` and pass an array of symbols
|
20
|
+
representing safe methods of the class that can be delegated
|
21
|
+
3. include module `ReadOnly::DelegateSafeMethods`
|
22
|
+
|
23
|
+
### Readonly Hash
|
24
|
+
|
25
|
+
Gem comes with a built-in example: `ReadOnly::Hash`. It's an example of
|
26
|
+
how any class (a `Hash` in this case) can be wrapped by this library to
|
27
|
+
create a immutable version.
|
28
|
+
|
29
|
+
Below we demonstrate our attempts to write to this class: note that the
|
30
|
+
first level key assignment raises `NoMethodError` because `[]=` method
|
31
|
+
was not declared as a safe method, and is not delegated.
|
32
|
+
|
33
|
+
Second level key returns a regular hash, which can be modified of
|
34
|
+
course, but in this case the returned object is a deep copy of the
|
35
|
+
original, and while it can be changed, this does not affect the original
|
36
|
+
object.
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
require 'readonly/hash'
|
40
|
+
|
41
|
+
@writable_hash = { user: { name: 'Konstantin', phone: '555-222-5555' } }
|
42
|
+
@readonly_hash = ReadOnly::Hash.new @writable_hash
|
43
|
+
|
44
|
+
@readonly_hash[:user][:name]
|
45
|
+
#=> Konstantin
|
46
|
+
@readonly_hash[:user][:name] = 'John'
|
47
|
+
#=> John
|
48
|
+
@readonly_hash[:user][:name]
|
49
|
+
#=> Konstantin
|
50
|
+
|
51
|
+
# Top level actually raises NoMethodError
|
52
|
+
@readonly_hash[:user] = [ 1,2,3 ]
|
53
|
+
#=> NoMethodError: undefined method `[]=' for {:user=>{:name=>"Konstantin", :phone=>"555-222-5555"}}:ReadOnly::Hash
|
54
|
+
```
|
55
|
+
|
56
|
+
## Installation
|
57
|
+
|
58
|
+
Add this line to your application's Gemfile:
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
gem 'readonly'
|
62
|
+
```
|
63
|
+
|
64
|
+
And then execute:
|
65
|
+
|
66
|
+
$ bundle
|
67
|
+
|
68
|
+
Or install it yourself as:
|
69
|
+
|
70
|
+
$ gem install readonly
|
71
|
+
|
72
|
+
## Usage
|
73
|
+
|
74
|
+
TODO: Write usage instructions here
|
75
|
+
|
76
|
+
## Development
|
77
|
+
|
78
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
79
|
+
|
80
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
81
|
+
|
82
|
+
## Contributing
|
83
|
+
|
84
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/kigster/readonly.
|
85
|
+
|
86
|
+
|
87
|
+
## License
|
88
|
+
|
89
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
90
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
|
4
|
+
require 'yard'
|
5
|
+
|
6
|
+
|
7
|
+
YARD::Rake::YardocTask.new(:doc) do |t|
|
8
|
+
t.files = %w(lib/**/*.rb exe/*.rb - README.md LICENSE.txt)
|
9
|
+
t.options.unshift('--title', '"ReadOnly — Fast and Scalable "write-time" Simple Feed for Social Networks, with a Redis-based default backend implementation."')
|
10
|
+
t.after = ->() { exec('open doc/index.html') } if RUBY_PLATFORM =~ /darwin/
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
RSpec::Core::RakeTask.new(:spec)
|
15
|
+
|
16
|
+
task :default => :spec
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "readonly"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/readonly.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'readonly/version'
|
2
|
+
|
3
|
+
module ReadOnly
|
4
|
+
end
|
5
|
+
|
6
|
+
Object.class_eval do
|
7
|
+
def deep_dup
|
8
|
+
if respond_to?(:each_with_object)
|
9
|
+
each_with_object(dup) do |(key, value), hash|
|
10
|
+
hash[key.deep_dup] = value.deep_dup
|
11
|
+
end
|
12
|
+
else
|
13
|
+
is_a?(Symbol) ? self : dup
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
require 'readonly/proxy'
|
19
|
+
require 'readonly/delegate_safe_methods'
|
20
|
+
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module ReadOnly
|
2
|
+
module DelegateSafeMethods
|
3
|
+
def self.included(klass)
|
4
|
+
klass.class_eval do
|
5
|
+
proxied_methods.each do |method|
|
6
|
+
define_method(method) do |*args, &block|
|
7
|
+
@delegate.deep_dup.send(method, *args, &block)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'readonly'
|
2
|
+
|
3
|
+
module ReadOnly
|
4
|
+
class Hash
|
5
|
+
include ReadOnly::Proxy
|
6
|
+
|
7
|
+
proxied_methods \
|
8
|
+
:[],
|
9
|
+
:each,
|
10
|
+
:each_key,
|
11
|
+
:each_pair,
|
12
|
+
:each_value,
|
13
|
+
:eql?,
|
14
|
+
:has_key?,
|
15
|
+
:has_value?,
|
16
|
+
:inspect,
|
17
|
+
:key,
|
18
|
+
:keys,
|
19
|
+
:length,
|
20
|
+
:select,
|
21
|
+
:size,
|
22
|
+
:to_a,
|
23
|
+
:to_h,
|
24
|
+
:to_hash,
|
25
|
+
:to_s,
|
26
|
+
:value,
|
27
|
+
:value?,
|
28
|
+
:values,
|
29
|
+
:values,
|
30
|
+
:values_at
|
31
|
+
|
32
|
+
include ReadOnly::DelegateSafeMethods
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module ReadOnly
|
2
|
+
module Proxy
|
3
|
+
def self.included(klass)
|
4
|
+
klass.instance_eval do
|
5
|
+
@proxied_methods = []
|
6
|
+
class << self
|
7
|
+
def proxied_methods(*args)
|
8
|
+
@proxied_methods = args if args && !args.empty?
|
9
|
+
@proxied_methods
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end # klass.instance_eval
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize(writable_object)
|
16
|
+
@delegate = writable_object
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/readonly.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'readonly/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'readonly'
|
8
|
+
spec.version = ReadOnly::VERSION
|
9
|
+
spec.authors = ['Konstantin Gredeskoul']
|
10
|
+
spec.email = ['kig@reinvent.one']
|
11
|
+
|
12
|
+
spec.summary = %q{This gem offers a proxy class that can be used to create a read-only wrappers around any other class, by declaring which methods can be delegated to the wrapped class.}
|
13
|
+
spec.description = %q{This gem offers a proxy class that can be used to create a read-only wrappers around any other class, by declaring which methods can be delegated to the wrapped class.}
|
14
|
+
spec.homepage = 'https://github.com/kigster/readonly'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ['lib']
|
23
|
+
|
24
|
+
spec.add_development_dependency 'yard'
|
25
|
+
spec.add_development_dependency 'simplecov', '~> 0.12'
|
26
|
+
spec.add_development_dependency 'codeclimate-test-reporter', '~> 1.0'
|
27
|
+
spec.add_development_dependency 'bundler', '~> 1.13'
|
28
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
29
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: readonly
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Konstantin Gredeskoul
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-11-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: yard
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: simplecov
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.12'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.12'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: codeclimate-test-reporter
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.13'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.13'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.0'
|
97
|
+
description: This gem offers a proxy class that can be used to create a read-only
|
98
|
+
wrappers around any other class, by declaring which methods can be delegated to
|
99
|
+
the wrapped class.
|
100
|
+
email:
|
101
|
+
- kig@reinvent.one
|
102
|
+
executables: []
|
103
|
+
extensions: []
|
104
|
+
extra_rdoc_files: []
|
105
|
+
files:
|
106
|
+
- ".codeclimate.yml"
|
107
|
+
- ".gitignore"
|
108
|
+
- ".rspec"
|
109
|
+
- ".rubocop.yml"
|
110
|
+
- ".travis.yml"
|
111
|
+
- ".yardopts"
|
112
|
+
- Gemfile
|
113
|
+
- LICENSE.txt
|
114
|
+
- README.md
|
115
|
+
- Rakefile
|
116
|
+
- bin/console
|
117
|
+
- bin/setup
|
118
|
+
- lib/readonly.rb
|
119
|
+
- lib/readonly/delegate_safe_methods.rb
|
120
|
+
- lib/readonly/hash.rb
|
121
|
+
- lib/readonly/proxy.rb
|
122
|
+
- lib/readonly/version.rb
|
123
|
+
- readonly.gemspec
|
124
|
+
homepage: https://github.com/kigster/readonly
|
125
|
+
licenses:
|
126
|
+
- MIT
|
127
|
+
metadata: {}
|
128
|
+
post_install_message:
|
129
|
+
rdoc_options: []
|
130
|
+
require_paths:
|
131
|
+
- lib
|
132
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
133
|
+
requirements:
|
134
|
+
- - ">="
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: '0'
|
137
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
138
|
+
requirements:
|
139
|
+
- - ">="
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: '0'
|
142
|
+
requirements: []
|
143
|
+
rubyforge_project:
|
144
|
+
rubygems_version: 2.6.6
|
145
|
+
signing_key:
|
146
|
+
specification_version: 4
|
147
|
+
summary: This gem offers a proxy class that can be used to create a read-only wrappers
|
148
|
+
around any other class, by declaring which methods can be delegated to the wrapped
|
149
|
+
class.
|
150
|
+
test_files: []
|