restricted_struct 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 701f39867ce9703612900fe3fc7a02a5546860cd
4
+ data.tar.gz: ec33a9f1adf85537047831ad5f52c7d02be8bc1f
5
+ SHA512:
6
+ metadata.gz: 97fc5cf511d82388464d41532d3f41a063de18b89891c1c9ac5cbd728e2337e2dc6ce2825dbe156350a65c59ba66eee5dffdb1a2d627d13d195c9ba75e5a4b9c
7
+ data.tar.gz: 96fa0f60b0005d2ff5da42137de9bcb8973bafe88e117854209fc532c2db24adb29f89970101f3128bce0a1058062bc0094f8f2dc9b965f95cae0e4bf3d2205b
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,13 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - 2.1
7
+ - 2.2
8
+ - jruby-18mode
9
+ - jruby-19mode
10
+ - rbx
11
+
12
+ script: bundle exec rspec
13
+ bundler_args: --without development
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in restricted_struct.gemspec
4
+ gemspec
5
+
6
+ gem "rspec"
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Alexey Fedorov
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.
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # RestrictedStruct
2
+
3
+ [![Build Status](https://travis-ci.org/waterlink/restricted_struct.svg?branch=master)](https://travis-ci.org/waterlink/restricted_struct)
4
+
5
+ This gem allows to use ruby's Struct, but automatically hides all the attributes as private or protected, which provides higher level of encapsulation.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'restricted_struct'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install restricted_struct
22
+
23
+ ## Usage
24
+
25
+ Instead of doing this:
26
+
27
+ ```ruby
28
+ class Rectangle < Struct.new(:x1, :y1, :x2, :y2)
29
+ protected :x1, :x1=, :y1, :y1=, :x2, :x2=, :y2, :y2=
30
+
31
+ # ...
32
+ end
33
+ ```
34
+
35
+ one can do:
36
+
37
+ ```ruby
38
+ class Rectangle < RestrictedStruct.new(:protected, :x1, :y1, :x2, :y2)
39
+ # ...
40
+ end
41
+ ```
42
+
43
+ Available access levels: `:private` and `:protected`.
44
+
45
+ ## Contributing
46
+
47
+ 1. Fork it ( https://github.com/waterlink/restricted_struct/fork )
48
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
49
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
50
+ 4. Push to the branch (`git push origin my-new-feature`)
51
+ 5. Create a new Pull Request
52
+
53
+ ## Contributors
54
+
55
+ - [Alexey Fedorov](https://github.com/waterlink)
56
+ - [Anselm Helbig](https://github.com/anselm-helbig-wimdu)
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,29 @@
1
+ class RestrictedStruct < Struct
2
+ ACCESS_RESTRICTIONS = {
3
+ :private => lambda { |methods| lambda { |*| private *methods } },
4
+ :protected => lambda { |methods| lambda { |*| protected *methods } }
5
+ }
6
+ ACCESS_LEVELS = ACCESS_RESTRICTIONS.keys
7
+
8
+ class << self
9
+ def new(access_level, *properties)
10
+ super(*properties, &access_restriction(access_level, properties))
11
+ end
12
+
13
+ private
14
+
15
+ def access_restriction(access_level, properties)
16
+ builder_strategy(access_level).call(accessor_methods(properties))
17
+ end
18
+
19
+ def builder_strategy(access_level)
20
+ ACCESS_RESTRICTIONS.fetch(access_level) do
21
+ raise ArgumentError, "access_level can be only one of #{ACCESS_LEVELS}"
22
+ end
23
+ end
24
+
25
+ def accessor_methods(properties)
26
+ properties + properties.map { |name| :"#{name}=" }
27
+ end
28
+ end
29
+ end
@@ -0,0 +1 @@
1
+ RestrictedStruct::VERSION = "0.1.0"
@@ -0,0 +1,2 @@
1
+ require "restricted_struct/definition"
2
+ require "restricted_struct/version"
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'restricted_struct'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "restricted_struct"
8
+ spec.version = RestrictedStruct::VERSION
9
+ spec.authors = ["Alexey Fedorov", "Anselm Helbig"]
10
+ spec.email = ["alexey.fedorov@wimdu.com", "anselm.helbig@wimdu.com"]
11
+ spec.summary = %q{Create Struct-s with private or protected attributes}
12
+ spec.description = %q{This gem allows to use ruby's Struct, but automatically hides all the attributes as private or protected, which provides higher level of encapsulation.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
@@ -0,0 +1,26 @@
1
+ RSpec.describe RestrictedStruct do
2
+ it "validates access_level" do
3
+ expect {
4
+ described_class.new(:invalid, :some, :properties)
5
+ }.to raise_error(ArgumentError)
6
+ end
7
+
8
+ described_class::ACCESS_LEVELS.each do |access_level|
9
+ context "when creating #{access_level} struct" do
10
+ subject { described_class.new(access_level, :some, :properties) }
11
+
12
+ it "makes readers #{access_level}" do
13
+ expect {
14
+ subject.new("some", "value").some
15
+ }.to raise_error(NoMethodError, /#{access_level} method `some'/)
16
+ end
17
+
18
+ it "makes writers #{access_level}" do
19
+ expect {
20
+ subject.new("some", "value").properties = 42
21
+ }.to raise_error(NoMethodError, /#{access_level} method `properties='/)
22
+ end
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,87 @@
1
+ require "restricted_struct"
2
+
3
+ # This file was generated by the `rspec --init` command. Conventionally, all
4
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
5
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
6
+ # file to always be loaded, without a need to explicitly require it in any files.
7
+ #
8
+ # Given that it is always loaded, you are encouraged to keep this file as
9
+ # light-weight as possible. Requiring heavyweight dependencies from this file
10
+ # will add to the boot time of your test suite on EVERY test run, even for an
11
+ # individual file that may not need all of that loaded. Instead, consider making
12
+ # a separate helper file that requires the additional dependencies and performs
13
+ # the additional setup, and require it from the spec files that actually need it.
14
+ #
15
+ # The `.rspec` file also contains a few flags that are not defaults but that
16
+ # users commonly want.
17
+ #
18
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19
+ RSpec.configure do |config|
20
+ # rspec-expectations config goes here. You can use an alternate
21
+ # assertion/expectation library such as wrong or the stdlib/minitest
22
+ # assertions if you prefer.
23
+ config.expect_with :rspec do |expectations|
24
+ # This option will default to `true` in RSpec 4. It makes the `description`
25
+ # and `failure_message` of custom matchers include text for helper methods
26
+ # defined using `chain`, e.g.:
27
+ # be_bigger_than(2).and_smaller_than(4).description
28
+ # # => "be bigger than 2 and smaller than 4"
29
+ # ...rather than:
30
+ # # => "be bigger than 2"
31
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
32
+ end
33
+
34
+ # rspec-mocks config goes here. You can use an alternate test double
35
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
36
+ config.mock_with :rspec do |mocks|
37
+ # Prevents you from mocking or stubbing a method that does not exist on
38
+ # a real object. This is generally recommended, and will default to
39
+ # `true` in RSpec 4.
40
+ mocks.verify_partial_doubles = true
41
+ end
42
+
43
+ # These two settings work together to allow you to limit a spec run
44
+ # to individual examples or groups you care about by tagging them with
45
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
46
+ # get run.
47
+ config.filter_run :focus
48
+ config.run_all_when_everything_filtered = true
49
+
50
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
51
+ # For more details, see:
52
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
53
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
54
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
55
+ config.disable_monkey_patching!
56
+
57
+ # This setting enables warnings. It's recommended, but in some cases may
58
+ # be too noisy due to issues in dependencies.
59
+ config.warnings = true
60
+
61
+ # Many RSpec users commonly either run the entire suite or an individual
62
+ # file, and it's useful to allow more verbose output when running an
63
+ # individual spec file.
64
+ if config.files_to_run.one?
65
+ # Use the documentation formatter for detailed output,
66
+ # unless a formatter has already been configured
67
+ # (e.g. via a command-line flag).
68
+ config.default_formatter = 'doc'
69
+ end
70
+
71
+ # Print the 10 slowest examples and example groups at the
72
+ # end of the spec run, to help surface which specs are running
73
+ # particularly slow.
74
+ config.profile_examples = 10
75
+
76
+ # Run specs in random order to surface order dependencies. If you find an
77
+ # order dependency and want to debug it, you can fix the order by providing
78
+ # the seed, which is printed after each run.
79
+ # --seed 1234
80
+ config.order = :random
81
+
82
+ # Seed global randomization in this process using the `--seed` CLI option.
83
+ # Setting this allows you to use `--seed` to deterministically reproduce
84
+ # test failures related to randomization by passing the same `--seed` value
85
+ # as the one that triggered the failure.
86
+ Kernel.srand config.seed
87
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: restricted_struct
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexey Fedorov
8
+ - Anselm Helbig
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-01-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.7'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.7'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '10.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '10.0'
42
+ description: This gem allows to use ruby's Struct, but automatically hides all the
43
+ attributes as private or protected, which provides higher level of encapsulation.
44
+ email:
45
+ - alexey.fedorov@wimdu.com
46
+ - anselm.helbig@wimdu.com
47
+ executables: []
48
+ extensions: []
49
+ extra_rdoc_files: []
50
+ files:
51
+ - ".gitignore"
52
+ - ".rspec"
53
+ - ".travis.yml"
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - lib/restricted_struct.rb
59
+ - lib/restricted_struct/definition.rb
60
+ - lib/restricted_struct/version.rb
61
+ - restricted_struct.gemspec
62
+ - spec/restricted_struct_spec.rb
63
+ - spec/spec_helper.rb
64
+ homepage: ''
65
+ licenses:
66
+ - MIT
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.2.2
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: Create Struct-s with private or protected attributes
88
+ test_files:
89
+ - spec/restricted_struct_spec.rb
90
+ - spec/spec_helper.rb