superstructure 0.0.1

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: 7c025de98f670afc858e54a8b6f7be0b0a91a193
4
+ data.tar.gz: 7bf2df28edf8683689a8b5cc52a08a39c243752d
5
+ SHA512:
6
+ metadata.gz: b54485982cd3042f02ca18cf6d81e9eaccb6d9a37e610ee124bd66e31cc680f2257aa3298589b571ce8efbd413d9e9af1571d2d343e374522411c89ebb52e576
7
+ data.tar.gz: 1e935204cc0f528a86dddc15da0ae668d0e33d81e1125f986801cce33ad648973eed473a6075a540f213ca861bafd757e66a827b58a0f8fef3a7ff45cca1c0b9
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,3 @@
1
+ --color
2
+ --require spec_helper
3
+ --require superstructure
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in superstructure.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Dan Finnie
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,31 @@
1
+ # Superstructure
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'superstructure'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install superstructure
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/superstructure/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,6 @@
1
+ module Superstructure
2
+ end
3
+
4
+ require "superstructure/version"
5
+ require "superstructure/value_obj"
6
+ require "superstructure/argument_error"
@@ -0,0 +1,16 @@
1
+ module Superstructure
2
+ ArgumentError = ValueObj.new(
3
+ :extra_params,
4
+ :missing_params,
5
+ :shadowed_params,
6
+ superclass: ::ArgumentError
7
+ ) do
8
+ def message
9
+ [
10
+ extra_params.any? ? "Received unexpected options: #{extra_params.inspect}" : nil,
11
+ missing_params.any? ? "Expected but did not receive: #{missing_params.inspect}" : nil,
12
+ shadowed_params.any? ? "Received a symbol and string version of: #{shadowed_params.inspect}" : nil
13
+ ].compact.join("\n")
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,67 @@
1
+ module Superstructure
2
+ class ValueObj
3
+ class << self
4
+ def new *arguments, superclass: Object, &blk
5
+ Class.new(superclass) do
6
+ attr_reader :to_hash
7
+
8
+ define_method(:initialize) do |opts={}|
9
+ attributes = {}
10
+ missing_params = []
11
+ shadowed_params = []
12
+ used_params = []
13
+
14
+ arguments.each do |argument|
15
+ if opts.has_key?(argument) && opts.has_key?(argument.to_s)
16
+ shadowed_params << argument
17
+ used_params << argument << argument.to_s
18
+
19
+ elsif opts.has_key?(argument)
20
+ attributes[argument] = opts[argument]
21
+ used_params << argument
22
+ elsif opts.has_key?(argument.to_s)
23
+ attributes[argument] = opts[argument.to_s]
24
+ used_params << argument.to_s
25
+ else
26
+ missing_params << argument
27
+ end
28
+ end
29
+
30
+ extra_params = opts.keys - used_params
31
+
32
+ if missing_params.any? || shadowed_params.any? || extra_params.any?
33
+ raise ArgumentError.new(
34
+ missing_params: missing_params,
35
+ shadowed_params: shadowed_params,
36
+ extra_params: extra_params
37
+ )
38
+ end
39
+
40
+ @to_hash = attributes
41
+ end
42
+
43
+ def inspect
44
+ opts = to_hash.map do |k, v|
45
+ "#{k}=#{v.inspect}"
46
+ end.join(", ")
47
+ "#<value_obj #{self.class} #{opts}>"
48
+ end
49
+
50
+ def ==(o)
51
+ self.class == o.class && to_hash == o.to_hash
52
+ end
53
+
54
+ alias :eql? :==
55
+
56
+ arguments.each do |argument|
57
+ define_method(argument) do
58
+ to_hash[argument]
59
+ end
60
+ end
61
+
62
+ class_eval(&blk) if blk
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,3 @@
1
+ module Superstructure
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,35 @@
1
+ RSpec.describe Superstructure::ArgumentError do
2
+ let(:argument_error) do
3
+ described_class.new(
4
+ extra_params: extra_params,
5
+ missing_params: missing_params,
6
+ shadowed_params: shadowed_params
7
+ )
8
+ end
9
+
10
+ describe "#message" do
11
+ subject { argument_error.message }
12
+
13
+ context "when there is only one type of error" do
14
+ let(:extra_params) { ["alpha", :beta] }
15
+ let(:missing_params) { [] }
16
+ let(:shadowed_params) { [] }
17
+
18
+ it "has a description of the error" do
19
+ expect(subject).to eq 'Received unexpected options: ["alpha", :beta]'
20
+ end
21
+ end
22
+
23
+ context "when there are multiple errors" do
24
+ let(:extra_params) { [:elephant] }
25
+ let(:missing_params) { [:mongoose] }
26
+ let(:shadowed_params) { [:snake] }
27
+
28
+ it "has a description of the all of the errors" do
29
+ expect(subject).to include 'Received unexpected options: [:elephant]'
30
+ expect(subject).to include 'Expected but did not receive: [:mongoose]'
31
+ expect(subject).to include 'Received a symbol and string version of: [:snake]'
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,21 @@
1
+ RSpec.configure do |config|
2
+ config.expect_with :rspec do |expectations|
3
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
4
+ end
5
+
6
+ config.mock_with :rspec do |mocks|
7
+ mocks.verify_partial_doubles = true
8
+ end
9
+
10
+ config.filter_run :focus
11
+ config.run_all_when_everything_filtered = true
12
+
13
+ config.disable_monkey_patching!
14
+
15
+ if config.files_to_run.one?
16
+ config.default_formatter = 'doc'
17
+ end
18
+
19
+ config.order = :random
20
+ Kernel.srand config.seed
21
+ end
@@ -0,0 +1,149 @@
1
+ RSpec.describe Superstructure::ValueObj do
2
+ FooBar = Superstructure::ValueObj.new :foo, :bar
3
+ Empty = Superstructure::ValueObj.new
4
+
5
+ it "accepts parameters from symbol arguments and exposes them as methods" do
6
+ foobar = FooBar.new(foo: 1, bar: 2)
7
+ expect(foobar.foo).to eq 1
8
+ expect(foobar.bar).to eq 2
9
+ end
10
+
11
+ it "accepts parameters from string arguments" do
12
+ foobar = FooBar.new("foo" => "foo", "bar" => :bar)
13
+ expect(foobar.foo).to eq "foo"
14
+ expect(foobar.bar).to eq :bar
15
+ end
16
+
17
+ it "accepts parameters from a mix of string and symbol arguments" do
18
+ foobar = FooBar.new("foo" => "foo", bar: :bar)
19
+ expect(foobar.foo).to eq "foo"
20
+ expect(foobar.bar).to eq :bar
21
+ end
22
+
23
+ it "exposes options in the to_hash method" do
24
+ foobar = FooBar.new(foo: "foo?", bar: "bar?")
25
+ expect(foobar.to_hash).to eq({
26
+ foo: "foo?",
27
+ bar: "bar?"
28
+ })
29
+ end
30
+
31
+ it "exposes the parameters as readers" do
32
+ expect(FooBar.instance_methods).to include :foo, :bar
33
+ end
34
+
35
+ it "does not expose writers" do
36
+ foobar = FooBar.new(foo: :fruu, bar: :bruu)
37
+ expect(foobar).not_to respond_to :foo=
38
+ expect(foobar).not_to respond_to :bar=
39
+ end
40
+
41
+ describe "#inspect" do
42
+ it "basic stuff" do
43
+ foobar = FooBar.new(foo: 1, bar: 2)
44
+ expect(foobar.inspect).to eq '#<value_obj FooBar foo=1, bar=2>'
45
+ end
46
+
47
+ it "handles complicated data structures" do
48
+ foobar = FooBar.new(foo: [:a, :b], bar: 2)
49
+ expect(foobar.inspect).to eq '#<value_obj FooBar foo=[:a, :b], bar=2>'
50
+ end
51
+ end
52
+
53
+ it "can inherit from other classes" do
54
+ superclass = Class.new
55
+ subclass = Superstructure::ValueObj.new(:alpha, superclass: superclass)
56
+ instance = subclass.new(alpha: "alpha")
57
+ expect(instance).to be_kind_of superclass
58
+ end
59
+
60
+ it "can be passed additional methods" do
61
+ klass = Superstructure::ValueObj.new(:alpha) do
62
+ def hello
63
+ "bonjour"
64
+ end
65
+ end
66
+
67
+ instance = klass.new(alpha: "Alpharetta")
68
+ expect(instance.hello).to eq "bonjour"
69
+ end
70
+
71
+ it "does not mutate the input" do
72
+ opts = { foo: 42, bar: 24 }
73
+
74
+ FooBar.new(opts)
75
+
76
+ expect(opts[:foo]).to eq 42
77
+ expect(opts[:bar]).to eq 24
78
+ end
79
+
80
+ describe "equality" do
81
+ shared_examples_for "equality" do |operator|
82
+ it "is equal if all arguments are equal" do
83
+ alpha = FooBar.new(foo: 1, bar: 2)
84
+ beta = FooBar.new("foo" => 1, "bar" => 2)
85
+ expect(alpha.public_send(operator, beta)).to be_truthy
86
+ end
87
+
88
+ it "is not equal if any argument doesn't equal" do
89
+ alpha = FooBar.new(foo: 1, bar: 2000)
90
+ beta = FooBar.new("foo" => 1, "bar" => 2)
91
+ expect(alpha.public_send(operator, beta)).to be_falsey
92
+ end
93
+
94
+ it "is equal only to other instances of the same class" do
95
+ klass = Superstructure::ValueObj.new(:foo, :bar)
96
+ opts = { foo: 42, bar: 24 }
97
+
98
+ foobar = FooBar.new(opts)
99
+ barfoo = klass.new(opts)
100
+
101
+ expect(foobar.public_send(operator, barfoo)).to be_falsey
102
+ end
103
+ end
104
+
105
+ describe "==" do
106
+ it_behaves_like "equality", :==
107
+ end
108
+
109
+ describe "eql?" do
110
+ it_behaves_like "equality", :eql?
111
+ end
112
+ end
113
+
114
+ describe "error handling" do
115
+ it "is an error to not pass all of the parameters" do
116
+ expect { FooBar.new(foo: 1) }.to raise_error(Superstructure::ArgumentError) do |error|
117
+ expect(error.missing_params).to eq [:bar]
118
+ expect(error.extra_params).to be_empty
119
+ expect(error.shadowed_params).to be_empty
120
+ end
121
+ end
122
+
123
+ it "it an error to pass an extra parameter" do
124
+ expect { Empty.new(baz: 3, lolcat: 4) }.to raise_error(Superstructure::ArgumentError) do |error|
125
+ expect(error.missing_params).to be_empty
126
+ expect(error.extra_params).to match [:baz, :lolcat]
127
+ expect(error.shadowed_params).to be_empty
128
+ end
129
+ end
130
+
131
+ it "is an error to pass a symbol and string version of the same paramter" do
132
+ expect { FooBar.new(foo: 1, "foo" => 2, bar: 3) }.to raise_error(Superstructure::ArgumentError) do |error|
133
+ expect(error.missing_params).to be_empty
134
+ expect(error.extra_params).to be_empty
135
+ expect(error.shadowed_params).to eq [:foo]
136
+ end
137
+ end
138
+
139
+ it "does not create symbols for erroneous keys (as this could be a memory leak)" do
140
+ expect do
141
+ begin
142
+ Empty.new("extra_symbol_1" => 1)
143
+ rescue Superstructure::ArgumentError => e
144
+ # noop
145
+ end
146
+ end.not_to change { Symbol.all_symbols.size }
147
+ end
148
+ end
149
+ end
@@ -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 'superstructure/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "superstructure"
8
+ spec.version = Superstructure::VERSION
9
+ spec.authors = ["Dan Finnie"]
10
+ spec.email = ["dan@danfinnie.com"]
11
+ spec.summary = "Immutable, keyword-argument based value objects for Ruby"
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.7"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "rspec", "~> 3.2.0"
23
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: superstructure
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dan Finnie
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-13 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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.2.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.2.0
55
+ description:
56
+ email:
57
+ - dan@danfinnie.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/superstructure.rb
69
+ - lib/superstructure/argument_error.rb
70
+ - lib/superstructure/value_obj.rb
71
+ - lib/superstructure/version.rb
72
+ - spec/argument_error_spec.rb
73
+ - spec/spec_helper.rb
74
+ - spec/value_obj_spec.rb
75
+ - superstructure.gemspec
76
+ homepage: ''
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.0.14
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Immutable, keyword-argument based value objects for Ruby
100
+ test_files:
101
+ - spec/argument_error_spec.rb
102
+ - spec/spec_helper.rb
103
+ - spec/value_obj_spec.rb
104
+ has_rdoc: