solid-struct 0.0.2

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,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 522f1dbc238d5a05b441c2b629bdc0fa77f0121c
4
+ data.tar.gz: 39d3df78bd5f6f42f0399e3cf15226da38dc15da
5
+ SHA512:
6
+ metadata.gz: 7cfae66fedc07c2aa086201b6ab99ddf7466c5026d42127a3efd20a1459f7e20158fddacf098bfb2f6e5975ac81375c38a7bc6fb6eb53ad8c8b274ea0fec4636
7
+ data.tar.gz: fb8efc37c401c7ff7f967af5ce9a6f346067dc7ae5915522e98bdddbf59246f590be82f559eb33b308dc1f90220ce5162986492a708900dc092e7619905bbc4d
@@ -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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in solid-struct.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 chemica
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 all
13
+ 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 THE
21
+ SOFTWARE.
22
+
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Ben Dunkley
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,103 @@
1
+ # Solid Struct
2
+
3
+ A Ruby gem providing a Struct variant that allows initialisation with named parameters.
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'solid-struct'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install solid-struct
21
+
22
+ and require it in your Ruby code:
23
+
24
+ ````ruby
25
+ require 'solid_struct'
26
+ ````
27
+
28
+
29
+ ## Usage
30
+
31
+ ```ruby
32
+ Klass = SolidStruct.new(:param1, :param2, :param3)
33
+ instance = Klass.new(param1: 1, param2: "foo", param3: "bar")
34
+ instance.param1
35
+ #=> 1
36
+ instance.param2
37
+ #=> "foo"
38
+ instance.param3
39
+ #=> "bar"
40
+ ```
41
+
42
+ This allows for more readable code, especially when the struct definition is separated from the use. Named parameters intrinsically convey more meaning and reduce the need for comments.
43
+
44
+ Using named arguments allows you to initialise parameters out of order, of course:
45
+
46
+ ```ruby
47
+ Klass = SolidStruct.new(:param1, :param2, :param3)
48
+ instance = Klass.new(param2: "foo", param1: 1, param3: "bar")
49
+ instance.param1
50
+ #=> 1
51
+ instance.param2
52
+ #=> "foo"
53
+ instance.param3
54
+ #=> "bar"
55
+ ```
56
+
57
+ You can still use positional arguments if you choose to ignore the benefits of the gem:
58
+
59
+
60
+ ```ruby
61
+ Klass = SolidStruct.new(:param1, :param2, :param3)
62
+ instance = Klass.new(1, "foo", "bar")
63
+ instance.param1
64
+ #=> 1
65
+ instance.param2
66
+ #=> "foo"
67
+ instance.param3
68
+ #=> "bar"
69
+ ```
70
+
71
+ Or you can define and initialise in one simple step:
72
+
73
+
74
+ ```ruby
75
+ instance = SolidStruct.build(param1: 1, :param2: "foo", :param3: "bar")
76
+ instance.param1
77
+ #=> 1
78
+ instance.param2
79
+ #=> "foo"
80
+ instance.param3
81
+ #=> "bar"
82
+
83
+ # Unlike an OpenStruct undefined arguments are not allowed:
84
+
85
+ instance.pancake
86
+ #=>NoMethodError: undefined method `pancake' for #<struct param1=1, param2="foo", param3="bar">
87
+ ```
88
+
89
+
90
+ ## Credits
91
+
92
+ Partially based on Stack Overflow answers by clacke and Matt S:
93
+
94
+ http://stackoverflow.com/questions/5407940/named-parameters-in-ruby-structs
95
+
96
+
97
+ ## Contributing
98
+
99
+ 1. Fork it ( https://github.com/[my-github-username]/named-struct/fork )
100
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
101
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
102
+ 4. Push to the branch (`git push origin my-new-feature`)
103
+ 5. Create a new Pull Request
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << 'test'
7
+ t.pattern = "test/*_test.rb"
8
+ end
9
+
@@ -0,0 +1,27 @@
1
+ # Allow structs to be initialised using a hash
2
+ module SolidStruct
3
+ VERSION = "0.0.2"
4
+
5
+ class NamedStruct < Struct
6
+ # Override the initialize to handle hashes of named parameters
7
+ def initialize *args
8
+ opts = args.last.is_a?(Hash) ? args.pop : Hash.new
9
+ super *args
10
+ opts.each_pair do |k, v|
11
+ self.send "#{k}=", v
12
+ end
13
+ end
14
+ end
15
+
16
+ def self.new(*args)
17
+ NamedStruct.new(*args)
18
+ end
19
+
20
+ def self.build(hash)
21
+ raise ArgumentError.new("Arguments must be defined as a hash") unless hash.is_a? Hash
22
+
23
+ klass = NamedStruct.new(*hash.keys)
24
+ klass.new(hash)
25
+ end
26
+
27
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'solid_struct'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "solid-struct"
8
+ spec.version = SolidStruct::VERSION
9
+ spec.authors = ["Benjamin Dunkley"]
10
+ spec.email = ["ben@chemica.co.uk"]
11
+ spec.summary = %q{Structs with named parameters for initialisation and syntactic sugar}
12
+ spec.description = %q{SolidStruct is a Struct variant that allows the use of named parameters (via a hash) for initialisation. Syntactic sugar is provided for simpler struct definitions.}
13
+ spec.homepage = "https://github.com/chemica/solid-struct"
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
+ spec.add_development_dependency "minitest", "~> 5.7.0"
24
+ end
@@ -0,0 +1,36 @@
1
+ require 'test_helper'
2
+
3
+ describe SolidStruct do
4
+ it "allows named initialisation arguments" do
5
+ Klass1 = SolidStruct.new(:param1, :param2)
6
+ instance = Klass1.new(param1: "one", param2: "two")
7
+ instance.param1.must_equal "one"
8
+ instance.param2.must_equal "two"
9
+ end
10
+
11
+ it "allows positional initialisation arguments" do
12
+ Klass2 = SolidStruct.new(:param1, :param2)
13
+ instance = Klass2.new("one", "two")
14
+ instance.param1.must_equal "one"
15
+ instance.param2.must_equal "two"
16
+ end
17
+
18
+ it "allows named arguments in a different order" do
19
+ Klass3 = SolidStruct.new(:param1, :param2)
20
+ instance = Klass3.new(param2: "two", param1: "one")
21
+ instance.param1.must_equal "one"
22
+ instance.param2.must_equal "two"
23
+ end
24
+
25
+ it "builds an instance using a hash" do
26
+ instance = SolidStruct.build(param1: "one", param2: "two")
27
+ instance.param1.must_equal "one"
28
+ instance.param2.must_equal "two"
29
+ end
30
+
31
+ it "throws an exception if building using a non-hash" do
32
+ assert_raises ArgumentError do
33
+ SolidStruct.build(["I'm an array, fool!"])
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,4 @@
1
+ require 'solid_struct'
2
+ require 'minitest/autorun'
3
+ require 'minitest/pride'
4
+ require 'minitest/spec'
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: solid-struct
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Benjamin Dunkley
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-19 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
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: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 5.7.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 5.7.0
55
+ description: SolidStruct is a Struct variant that allows the use of named parameters
56
+ (via a hash) for initialisation. Syntactic sugar is provided for simpler struct
57
+ definitions.
58
+ email:
59
+ - ben@chemica.co.uk
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - Gemfile
66
+ - LICENSE
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - lib/solid_struct.rb
71
+ - solid-struct.gemspec
72
+ - test/solid_struct_test.rb
73
+ - test/test_helper.rb
74
+ homepage: https://github.com/chemica/solid-struct
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.4.8
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Structs with named parameters for initialisation and syntactic sugar
98
+ test_files:
99
+ - test/solid_struct_test.rb
100
+ - test/test_helper.rb