named_struct 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +62 -0
- data/Rakefile +2 -0
- data/lib/named_struct/config.rb +34 -0
- data/lib/named_struct/version.rb +3 -0
- data/lib/named_struct.rb +2 -0
- data/named_struct.gemspec +26 -0
- data/spec/named_struct/config_spec.rb +74 -0
- data/spec/spec_helper.rb +86 -0
- metadata +127 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d9f07fc08fe47b747577f6c1f2a080295b437cc6
|
4
|
+
data.tar.gz: 380ce889316a7387b33c340279f5d81b7f052d20
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4008bbc5004c1351c88911570f423f992e1ff640a43fed8dc5564e7d5f3e849177b539cb22c7d3ba6a5ce7cec0b24951207070fac141a921d09933d169f7c18b
|
7
|
+
data.tar.gz: 8b24d41151be98eae8f3d48024f518147406f77911853d55f9c1926927b8dbb7d7c7c9ad3190417c82d6c93afc9138d25a4b18ada0833533ae68efdc73c32652
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
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
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Max Shytikov
|
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,62 @@
|
|
1
|
+
# NamedStruct
|
2
|
+
|
3
|
+
Simple object to wrap your configuration, it can be instantiated only
|
4
|
+
with required named arguments.
|
5
|
+
|
6
|
+
Usually you do :
|
7
|
+
|
8
|
+
```
|
9
|
+
class MyConfig
|
10
|
+
att_reader :a, :b, :c
|
11
|
+
|
12
|
+
def initialize(a:, b:, c:)
|
13
|
+
@a = a
|
14
|
+
@b = b
|
15
|
+
@c = c
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
```
|
21
|
+
|
22
|
+
But now you can :
|
23
|
+
|
24
|
+
```
|
25
|
+
class MyConfig < NamedStruct::Config
|
26
|
+
attr_required :a, :b, :c
|
27
|
+
end
|
28
|
+
|
29
|
+
```
|
30
|
+
|
31
|
+
## Installation
|
32
|
+
|
33
|
+
Add this line to your application's Gemfile:
|
34
|
+
|
35
|
+
gem 'named_struct'
|
36
|
+
|
37
|
+
And then execute:
|
38
|
+
|
39
|
+
$ bundle
|
40
|
+
|
41
|
+
Or install it yourself as:
|
42
|
+
|
43
|
+
$ gem install named_struct
|
44
|
+
|
45
|
+
## Usage
|
46
|
+
|
47
|
+
```
|
48
|
+
class MyConfig < NamedStruct::Config
|
49
|
+
attr_required :a, :b, :c
|
50
|
+
end
|
51
|
+
|
52
|
+
my_config = MyConfig.new(a: 10, b: 20, c: 30)
|
53
|
+
|
54
|
+
class MySuperAlgorithm
|
55
|
+
def initialize(my_param, **config)
|
56
|
+
@my_param = my_param
|
57
|
+
@config = MyConfig.new(**config)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
MySuperAlgirithm.new("some param", a: 12, b: 10, c: 50)
|
62
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
module NamedStruct
|
2
|
+
class Config
|
3
|
+
class << self
|
4
|
+
|
5
|
+
attr_reader :members
|
6
|
+
def attr_required(*keys)
|
7
|
+
@members = keys
|
8
|
+
@members.freeze
|
9
|
+
attr_reader(*@members)
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
attr_required() # init this class with no required attributes
|
15
|
+
|
16
|
+
def initialize(*positional_args, **args)
|
17
|
+
if !positional_args.empty?
|
18
|
+
raise ArgumentError, "wrong arguments accepts only keyword arguments"
|
19
|
+
end
|
20
|
+
keys = args.keys
|
21
|
+
required_keys = self.class.members
|
22
|
+
if keys != required_keys
|
23
|
+
if keys.size > required_keys.size
|
24
|
+
raise(ArgumentError, "wrong number of arguments (%d for %d)"%
|
25
|
+
[keys.size, required_keys.size])
|
26
|
+
end
|
27
|
+
mising_keys = required_keys - keys
|
28
|
+
raise(ArgumentError, "missing keyword: %s"%mising_keys.join(', '))
|
29
|
+
end
|
30
|
+
args.each{|k, v| instance_variable_set("@#{k}".to_sym, v)}
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
data/lib/named_struct.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'named_struct/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "named_struct"
|
8
|
+
spec.version = NamedStruct::VERSION
|
9
|
+
spec.authors = ["Max Shytikov"]
|
10
|
+
spec.email = ["mshytikov@gmail.com"]
|
11
|
+
spec.summary = %q{Simple Struct with required keyword arguments}
|
12
|
+
spec.description = %q{Simple Struct with required keyword arguments}
|
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.6"
|
22
|
+
spec.add_development_dependency "rspec"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
spec.add_development_dependency 'simplecov'
|
25
|
+
spec.add_development_dependency 'simplecov-gem-adapter'
|
26
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe NamedStruct::Config do
|
4
|
+
describe ".new" do
|
5
|
+
context "without arguments" do
|
6
|
+
subject{ NamedStruct::Config.new}
|
7
|
+
it { is_expected.to be_kind_of(NamedStruct::Config) }
|
8
|
+
it { expect{ subject }.to_not raise_error }
|
9
|
+
end
|
10
|
+
|
11
|
+
context "with positional arguments" do
|
12
|
+
subject{ NamedStruct::Config.new(10, 20)}
|
13
|
+
let(:error_message) { "wrong arguments accepts only keyword arguments" }
|
14
|
+
it { expect{ subject }.to raise_error(ArgumentError, error_message)}
|
15
|
+
end
|
16
|
+
|
17
|
+
context "with keyword arguments" do
|
18
|
+
subject{ NamedStruct::Config.new(a: 10, b: 20)}
|
19
|
+
let(:error_message) { "wrong number of arguments (2 for 0)" }
|
20
|
+
it { expect{ subject }.to raise_error(ArgumentError, error_message)}
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "class MyConfig < NamedStruct::Config" do
|
25
|
+
class MyConfig < NamedStruct::Config
|
26
|
+
attr_required :a, :b
|
27
|
+
end
|
28
|
+
|
29
|
+
describe '.members' do
|
30
|
+
subject{ MyConfig.members }
|
31
|
+
it { is_expected.to eq([:a, :b]) }
|
32
|
+
it { is_expected.to be_frozen }
|
33
|
+
end
|
34
|
+
|
35
|
+
context "without arguments" do
|
36
|
+
subject{ MyConfig.new}
|
37
|
+
let(:error_message) { "missing keyword: a, b" }
|
38
|
+
it { expect{ subject }.to raise_error(ArgumentError, error_message)}
|
39
|
+
end
|
40
|
+
|
41
|
+
context "with positional arguments" do
|
42
|
+
subject{ MyConfig.new(10, 20)}
|
43
|
+
let(:error_message) { "wrong arguments accepts only keyword arguments" }
|
44
|
+
it { expect{ subject }.to raise_error(ArgumentError, error_message)}
|
45
|
+
end
|
46
|
+
|
47
|
+
context "with keyword arguments" do
|
48
|
+
context "with all required arguments" do
|
49
|
+
subject{ MyConfig.new(a: 10, b: 11)}
|
50
|
+
it { is_expected.to be_kind_of(MyConfig) }
|
51
|
+
it { expect{ subject }.to_not raise_error }
|
52
|
+
it { is_expected.to respond_to(:a) }
|
53
|
+
it { is_expected.to respond_to(:b) }
|
54
|
+
it { is_expected.to_not respond_to(:a=) }
|
55
|
+
it { is_expected.to_not respond_to(:b=) }
|
56
|
+
it { expect(subject.a).to eq(10) }
|
57
|
+
it { expect(subject.b).to eq(11) }
|
58
|
+
end
|
59
|
+
|
60
|
+
context "with missing required arguments" do
|
61
|
+
context ":a is missing" do
|
62
|
+
subject{ MyConfig.new(b: 10)}
|
63
|
+
let(:error_message) { "missing keyword: a" }
|
64
|
+
it { expect{ subject }.to raise_error(ArgumentError, error_message)}
|
65
|
+
end
|
66
|
+
context ":b is missing" do
|
67
|
+
subject{ MyConfig.new(a: 10)}
|
68
|
+
let(:error_message) { "missing keyword: b" }
|
69
|
+
it { expect{ subject }.to raise_error(ArgumentError, error_message)}
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
require 'simplecov-gem-adapter'
|
3
|
+
SimpleCov.start 'gem'
|
4
|
+
|
5
|
+
require 'named_struct'
|
6
|
+
|
7
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
8
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
9
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause this
|
10
|
+
# file to always be loaded, without a need to explicitly require it in any files.
|
11
|
+
#
|
12
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
13
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
14
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
15
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
16
|
+
# a separate helper file that requires the additional dependencies and performs
|
17
|
+
# the additional setup, and require it from the spec files that actually need it.
|
18
|
+
#
|
19
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
20
|
+
# users commonly want.
|
21
|
+
#
|
22
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
23
|
+
RSpec.configure do |config|
|
24
|
+
# rspec-expectations config goes here. You can use an alternate
|
25
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
26
|
+
# assertions if you prefer.
|
27
|
+
config.expect_with :rspec do |expectations|
|
28
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
29
|
+
# and `failure_message` of custom matchers include text for helper methods
|
30
|
+
# defined using `chain`, e.g.:
|
31
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
32
|
+
# # => "be bigger than 2 and smaller than 4"
|
33
|
+
# ...rather than:
|
34
|
+
# # => "be bigger than 2"
|
35
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
36
|
+
end
|
37
|
+
|
38
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
39
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
40
|
+
config.mock_with :rspec do |mocks|
|
41
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
42
|
+
# a real object. This is generally recommended, and will default to
|
43
|
+
# `true` in RSpec 4.
|
44
|
+
mocks.verify_partial_doubles = true
|
45
|
+
end
|
46
|
+
|
47
|
+
# These two settings work together to allow you to limit a spec run
|
48
|
+
# to individual examples or groups you care about by tagging them with
|
49
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
50
|
+
# get run.
|
51
|
+
config.filter_run :focus
|
52
|
+
config.run_all_when_everything_filtered = true
|
53
|
+
|
54
|
+
# Limits the available syntax to the non-monkey patched syntax that is recommended.
|
55
|
+
# For more details, see:
|
56
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
57
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
58
|
+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
59
|
+
config.disable_monkey_patching!
|
60
|
+
|
61
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
62
|
+
# be too noisy due to issues in dependencies.
|
63
|
+
config.warnings = true
|
64
|
+
|
65
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
66
|
+
# file, and it's useful to allow more verbose output when running an
|
67
|
+
# individual spec file.
|
68
|
+
if config.files_to_run.one?
|
69
|
+
# Use the documentation formatter for detailed output,
|
70
|
+
# unless a formatter has already been configured
|
71
|
+
# (e.g. via a command-line flag).
|
72
|
+
config.default_formatter = 'doc'
|
73
|
+
end
|
74
|
+
|
75
|
+
# Run specs in random order to surface order dependencies. If you find an
|
76
|
+
# order dependency and want to debug it, you can fix the order by providing
|
77
|
+
# the seed, which is printed after each run.
|
78
|
+
# --seed 1234
|
79
|
+
config.order = :random
|
80
|
+
|
81
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
82
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
83
|
+
# test failures related to randomization by passing the same `--seed` value
|
84
|
+
# as the one that triggered the failure.
|
85
|
+
Kernel.srand config.seed
|
86
|
+
end
|
metadata
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: named_struct
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Max Shytikov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-10-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: simplecov
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: simplecov-gem-adapter
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Simple Struct with required keyword arguments
|
84
|
+
email:
|
85
|
+
- mshytikov@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- Gemfile
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- lib/named_struct.rb
|
96
|
+
- lib/named_struct/config.rb
|
97
|
+
- lib/named_struct/version.rb
|
98
|
+
- named_struct.gemspec
|
99
|
+
- spec/named_struct/config_spec.rb
|
100
|
+
- spec/spec_helper.rb
|
101
|
+
homepage: ''
|
102
|
+
licenses:
|
103
|
+
- MIT
|
104
|
+
metadata: {}
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options: []
|
107
|
+
require_paths:
|
108
|
+
- lib
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
requirements: []
|
120
|
+
rubyforge_project:
|
121
|
+
rubygems_version: 2.2.2
|
122
|
+
signing_key:
|
123
|
+
specification_version: 4
|
124
|
+
summary: Simple Struct with required keyword arguments
|
125
|
+
test_files:
|
126
|
+
- spec/named_struct/config_spec.rb
|
127
|
+
- spec/spec_helper.rb
|