power_struct 0.1.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.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/Gemfile +4 -0
- data/README.md +53 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/power_struct.rb +48 -0
- data/lib/power_struct/version.rb +3 -0
- data/power_struct.gemspec +26 -0
- metadata +94 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ae3f76e1267fde36f0a4d2322e5ea71001dfaedc
|
4
|
+
data.tar.gz: e8fd39becc5ce8844be6a8b07e07c2cd97c42c4c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 92fd194d04cc88d8212fa0762e52a7ab0e116ce556574388ab09aab11377376787246860559300abd105c94e6660fada117a6d7e7f026994da951b4a9fc7ba9a
|
7
|
+
data.tar.gz: ee87b0920b5d8277968ea4522afdf08393193ca911752922f2b0cd978b10caf17abc9df3dfca9a5d03950d06da7d91e6c29cbe6e9023b273c843c3768c553da3
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# PowerStruct
|
2
|
+
|
3
|
+
Simple `Struct` replacement with mandatory keyword arguments.
|
4
|
+
|
5
|
+
Reasons to use `PowerStruct`:
|
6
|
+
|
7
|
+
* Argument order no longer matters
|
8
|
+
* Initialization arguments are mandatory by default
|
9
|
+
* Cheesy name
|
10
|
+
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Add this line to your application's Gemfile:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
gem 'power_struct'
|
18
|
+
```
|
19
|
+
|
20
|
+
And then execute:
|
21
|
+
|
22
|
+
$ bundle
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
|
26
|
+
$ gem install power_struct
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
require "power_struct"
|
32
|
+
|
33
|
+
MyStruct = PowerStruct.new(:attr1, attr2: :attr2_default)
|
34
|
+
|
35
|
+
my_struct = MyStruct.new(attr1: "attr1_value")
|
36
|
+
my_struct.attr1 # => "attr1_value"
|
37
|
+
my_struct.attr2 # => :attr2_default
|
38
|
+
my_struct.attr2 = 1
|
39
|
+
my_struct.attr2 # => 1
|
40
|
+
|
41
|
+
my_struct = MyStruct.new # => raises exception, attr1 is mandatory
|
42
|
+
```
|
43
|
+
|
44
|
+
## Development
|
45
|
+
|
46
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
47
|
+
|
48
|
+
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).
|
49
|
+
|
50
|
+
## Contributing
|
51
|
+
|
52
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/power_struct.
|
53
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "power_struct"
|
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/power_struct.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require "power_struct/version"
|
2
|
+
|
3
|
+
module PowerStruct
|
4
|
+
ArgumentError = Class.new(::ArgumentError)
|
5
|
+
|
6
|
+
def self.new(*mandatory_attributes, **defaults, &block)
|
7
|
+
raise ArgumentError, "pass attribute names as symbols" unless \
|
8
|
+
mandatory_attributes.all? { |attribute| attribute.is_a?(Symbol) }
|
9
|
+
|
10
|
+
Class.new(Base) do
|
11
|
+
class << self
|
12
|
+
attr_reader :mandatory_attributes, :attribute_defaults
|
13
|
+
end
|
14
|
+
@mandatory_attributes = mandatory_attributes
|
15
|
+
@attribute_defaults = defaults
|
16
|
+
|
17
|
+
def initialize(*flat_arguments, **arguments)
|
18
|
+
raise ArgumentError, "pass parameters as hash arguments" if flat_arguments.any?
|
19
|
+
unless (self.class.mandatory_attributes - arguments.keys).empty?
|
20
|
+
raise ArgumentError, "missing argument(s) " +
|
21
|
+
(self.class.mandatory_attributes - arguments.keys).map(&:inspect).join(", ")
|
22
|
+
end
|
23
|
+
unless (
|
24
|
+
arguments.keys - self.class.mandatory_attributes - self.class.attribute_defaults.keys
|
25
|
+
).none?
|
26
|
+
raise ArgumentError, "excess argument(s) " +
|
27
|
+
(arguments.keys - self.class.mandatory_attributes - self.class.attribute_defaults.keys)
|
28
|
+
.map(&:inspect).join(", ")
|
29
|
+
end
|
30
|
+
|
31
|
+
self.class.attribute_defaults.each do |attribute, default|
|
32
|
+
self.public_send("#{attribute}=", default)
|
33
|
+
end
|
34
|
+
|
35
|
+
arguments.each do |attribute, value|
|
36
|
+
self.public_send("#{attribute}=", value)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
attr_accessor(*mandatory_attributes, *defaults.keys)
|
41
|
+
|
42
|
+
class_eval(&block) if block
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
class Base
|
47
|
+
end
|
48
|
+
end
|
@@ -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 'power_struct/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "power_struct"
|
8
|
+
spec.version = PowerStruct::VERSION
|
9
|
+
spec.authors = ["Thomas Stratmann"]
|
10
|
+
spec.email = ["thomas.stratmann@9elements.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Simple Struct replacement with mandatory keyword arguments}
|
13
|
+
spec.description = %q{Simple Struct replacement with mandatory keyword arguments}
|
14
|
+
#spec.homepage = "TODO: Put your gem's website or public repo URL here."
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: power_struct
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Thomas Stratmann
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-08-04 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.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: Simple Struct replacement with mandatory keyword arguments
|
56
|
+
email:
|
57
|
+
- thomas.stratmann@9elements.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- README.md
|
65
|
+
- Rakefile
|
66
|
+
- bin/console
|
67
|
+
- bin/setup
|
68
|
+
- lib/power_struct.rb
|
69
|
+
- lib/power_struct/version.rb
|
70
|
+
- power_struct.gemspec
|
71
|
+
homepage:
|
72
|
+
licenses: []
|
73
|
+
metadata: {}
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options: []
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
requirements: []
|
89
|
+
rubyforge_project:
|
90
|
+
rubygems_version: 2.5.2
|
91
|
+
signing_key:
|
92
|
+
specification_version: 4
|
93
|
+
summary: Simple Struct replacement with mandatory keyword arguments
|
94
|
+
test_files: []
|