super_struct 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f1671cda7646a760788b60b4e57f2c019d3a2c39
4
+ data.tar.gz: b5550c15630c199d189985a6e627918bc7ab2dc7
5
+ SHA512:
6
+ metadata.gz: 5d817cb4f2f7d83894ebce53bdf7dd166de9e6bb59e4e9b87bb5b5631aa286ae62fd565bc53e1ac5eacdef595ed98f4637c5b8f7a2d1fd649b1aa3f5ba5760da
7
+ data.tar.gz: 667da4f7025e13ba9837a09a6ad7d6f7dfe257aac49181458d5f7c4faa6d0c08ab738ec2c882944966e26b529732e889cd8d9d09b71da83d47035c9d1579a1f4
data/.gitignore ADDED
@@ -0,0 +1,15 @@
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
15
+ *.swp
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --require spec_helper
2
+ --format documentation
3
+ --color
4
+ --seed 99513377
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.2.0
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.5
6
+ - 2.2.0
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+ group :development, :test do
4
+ gem 'byebug', '~> 3.5.1', platforms: [ :ruby_20, :ruby_21 ]
5
+ gem 'debugger', platforms: [ :ruby_19 ]
6
+ end
data/Guardfile ADDED
@@ -0,0 +1,9 @@
1
+ require 'guard/rspec/dsl'
2
+ dsl = Guard::RSpec::Dsl.new(self)
3
+ rspec = dsl.rspec
4
+ ruby = dsl.ruby
5
+
6
+ guard :rspec, cmd: 'bundle exec rspec', spec_paths: %w( spec ) do
7
+ watch(%r{^lib/(.+)\.rb$}) { |m| "#{rspec.spec_dir}/#{m[1]}_spec.rb" }
8
+ watch(%r{^spec/.+\.rb$})
9
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Bram Swenson
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,54 @@
1
+ # SuperStruct
2
+
3
+ [![Build Status](https://travis-ci.org/bramswenson/super_struct.svg?branch=master)](https://travis-ci.org/bramswenson/super_struct)
4
+
5
+ Simple extensions to `Struct` to make it more compatable with `Hash` without the performance penalties of `OpenStruct`
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'super_struct'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install super_struct
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ require 'super_struct'
27
+
28
+ attributes = { name: 'John Doe' }
29
+ class Customer < SuperStruct.new(attributes)
30
+ def has_attribute?(attribute)
31
+ members.include?(attribute.to_sym)
32
+ end
33
+ end
34
+
35
+ john_doe = Customer.new(attributes)
36
+ => #<struct Customer name="John Doe">
37
+ john_doe.name
38
+ => 'John Doe'
39
+ ```
40
+
41
+ ## Testing
42
+
43
+ ```ruby
44
+ bundle install --path .bundle/bundle
45
+ bundle exec rake spec
46
+ ```
47
+
48
+ ## Contributing
49
+
50
+ 1. Fork it ( https://github.com/[my-github-username]/super_struct/fork )
51
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
52
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
53
+ 4. Push to the branch (`git push origin my-new-feature`)
54
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,105 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
8
+ task :benchmark do
9
+ require 'super_struct'
10
+ require 'hashie'
11
+ require 'ostruct'
12
+ require 'benchmark'
13
+ require 'benchmark/ips'
14
+
15
+ class PORO
16
+ attr_reader :foo, :bar, :baz
17
+
18
+ def initialize attrs={}
19
+ attrs.each do |attr, value|
20
+ instance_variable_set "@#{attr}", value
21
+ end
22
+ end
23
+ end
24
+
25
+ attrs = { foo: 1, bar: true, baz: 'quux' }
26
+ hashie = Hashie::Mash.new(attrs)
27
+ struct_klass = Struct.new(*attrs.keys)
28
+ struct = struct_klass.new(*attrs.values)
29
+ ostruct = OpenStruct.new(attrs)
30
+ poro = PORO.new(attrs)
31
+ super_struct_klass = SuperStruct.new(attrs)
32
+ super_struct = super_struct_klass.new(attrs)
33
+
34
+ Benchmark.ips do |x|
35
+ x.report 'Hashie alloc' do
36
+ Hashie::Mash.new(attrs)
37
+ end
38
+
39
+ x.report 'Hash alloc' do
40
+ Hash.new(attrs)
41
+ end
42
+
43
+ x.report 'OpenStruct alloc' do
44
+ OpenStruct.new(attrs)
45
+ end
46
+
47
+ x.report 'PORO alloc' do
48
+ PORO.new(attrs)
49
+ end
50
+
51
+ x.report 'Struct alloc' do
52
+ struct_klass.new(*attrs.values)
53
+ end
54
+
55
+ x.report 'SuperStruct hash alloc' do
56
+ super_struct_klass.new(attrs)
57
+ end
58
+
59
+ x.report 'SuperStruct array alloc' do
60
+ super_struct_klass.new(*attrs.sort.map(&:last))
61
+ end
62
+
63
+ x.compare!
64
+ end
65
+
66
+ Benchmark.ips do |x|
67
+ x.report 'Hashie access' do
68
+ hashie.foo
69
+ hashie.bar
70
+ hashie.baz
71
+ end
72
+
73
+ x.report 'Hash access' do
74
+ attrs[:foo]
75
+ attrs[:bar]
76
+ attrs[:baz]
77
+ end
78
+
79
+ x.report 'OStruct access' do
80
+ ostruct.foo
81
+ ostruct.bar
82
+ ostruct.baz
83
+ end
84
+
85
+ x.report 'PORO access' do
86
+ poro.foo
87
+ poro.bar
88
+ poro.baz
89
+ end
90
+
91
+ x.report 'Struct access' do
92
+ struct.foo
93
+ struct.bar
94
+ struct.baz
95
+ end
96
+
97
+ x.report 'SuperStruct access' do
98
+ super_struct.foo
99
+ super_struct.bar
100
+ super_struct.baz
101
+ end
102
+
103
+ x.compare!
104
+ end
105
+ end
@@ -0,0 +1,3 @@
1
+ module SuperStruct
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,36 @@
1
+ require "super_struct/version"
2
+
3
+ module SuperStruct
4
+ module InstanceMethods
5
+ def initialize(*input)
6
+ if input.first.respond_to?(:has_key?)
7
+ keys, values = input.first.sort.transpose
8
+ unless keys.map(&:to_sym) == members
9
+ raise ArgumentError.new("#{self.class.name} expects a hash with keys #{members.join(', ')}")
10
+ end
11
+ super(*values)
12
+ else
13
+ super(*input)
14
+ end
15
+ end
16
+
17
+ def attributes
18
+ members.inject({}) do |attributes, member|
19
+ attributes[member] = send(member)
20
+ attributes
21
+ end
22
+ end
23
+ end
24
+
25
+ def self.new(array_or_hash, &block)
26
+ keys = if array_or_hash.respond_to?(:has_key?)
27
+ array_or_hash.keys.sort.map(&:to_sym)
28
+ else
29
+ array_or_hash.sort.map(&:to_sym)
30
+ end
31
+
32
+ Struct.new(*keys, &block).tap do |struct|
33
+ struct.send(:include, InstanceMethods)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'super_struct'
3
+ require 'pry'
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ describe SuperStruct do
4
+ let(:constructor_input) { { foo: :bar, fiz: :bang } }
5
+ let(:klass) { SuperStruct.new(constructor_input) }
6
+ let(:input) { constructor_input }
7
+ subject { klass.new(input) }
8
+
9
+ describe '::new' do
10
+ subject { klass }
11
+
12
+ it 'creates a new Class' do
13
+ expect(subject.class).to eq Class
14
+ end
15
+
16
+ it 'extends the new Class with the SuperStruct methods' do
17
+ expect(subject.new).to respond_to :attributes
18
+ end
19
+
20
+ context 'given a block upon construction' do
21
+ let(:klass) do
22
+ SuperStruct.new(constructor_input) do
23
+ def has_attribute?(attribute)
24
+ members.include?(attribute.to_sym)
25
+ end
26
+ end
27
+ end
28
+
29
+ it 'extends the Class with the block' do
30
+ expect(klass.new).to have_attribute :foo
31
+ end
32
+ end
33
+ end
34
+
35
+ describe '#new' do
36
+ context 'given multiple arguments as input' do
37
+ subject { klass.new(:bar, :bang) }
38
+
39
+ it 'accepts an array as input' do
40
+ expect(subject.foo).to eq :bang
41
+ end
42
+ end
43
+ end
44
+
45
+ describe '#members' do
46
+ it 'is the keys given to the constructor, sorted' do
47
+ expect(subject.members).to eq input.keys.sort
48
+ end
49
+ end
50
+
51
+ describe '#attributes' do
52
+ it 'is a hash of the attributes and their values' do
53
+ expect(subject.attributes).to eq constructor_input
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'super_struct/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'super_struct'
8
+ spec.version = SuperStruct::VERSION
9
+ spec.authors = ['Bram Swenson']
10
+ spec.email = ['bram@craniumisajar.com']
11
+ spec.summary = %q{Extended struct constructors}
12
+ spec.description = %q{Extended struct constructors}
13
+ spec.homepage = 'http://github.com/bramswenson/super_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 'rspec', '~> 3.1.0'
24
+ spec.add_development_dependency 'guard', '~> 2.11.1'
25
+ spec.add_development_dependency 'guard-rspec', '~> 4.5.0'
26
+ spec.add_development_dependency 'pry', '~> 0.10.1'
27
+ spec.add_development_dependency 'hashie', '~> 3.4.0'
28
+ spec.add_development_dependency 'benchmark-ips', '~> 2.1.1'
29
+ end
metadata ADDED
@@ -0,0 +1,172 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: super_struct
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Bram Swenson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-07 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.1.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.1.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 2.11.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 2.11.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard-rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 4.5.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 4.5.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.10.1
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.10.1
97
+ - !ruby/object:Gem::Dependency
98
+ name: hashie
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 3.4.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 3.4.0
111
+ - !ruby/object:Gem::Dependency
112
+ name: benchmark-ips
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 2.1.1
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 2.1.1
125
+ description: Extended struct constructors
126
+ email:
127
+ - bram@craniumisajar.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".rspec"
134
+ - ".ruby-version"
135
+ - ".travis.yml"
136
+ - Gemfile
137
+ - Guardfile
138
+ - LICENSE.txt
139
+ - README.md
140
+ - Rakefile
141
+ - lib/super_struct.rb
142
+ - lib/super_struct/version.rb
143
+ - spec/spec_helper.rb
144
+ - spec/super_struct_spec.rb
145
+ - super_struct.gemspec
146
+ homepage: http://github.com/bramswenson/super_struct
147
+ licenses:
148
+ - MIT
149
+ metadata: {}
150
+ post_install_message:
151
+ rdoc_options: []
152
+ require_paths:
153
+ - lib
154
+ required_ruby_version: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ required_rubygems_version: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - ">="
162
+ - !ruby/object:Gem::Version
163
+ version: '0'
164
+ requirements: []
165
+ rubyforge_project:
166
+ rubygems_version: 2.4.5
167
+ signing_key:
168
+ specification_version: 4
169
+ summary: Extended struct constructors
170
+ test_files:
171
+ - spec/spec_helper.rb
172
+ - spec/super_struct_spec.rb