hstruct 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.
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/Guardfile +5 -0
- data/LICENSE.txt +22 -0
- data/README.md +82 -0
- data/Rakefile +1 -0
- data/benchmarks/hstruct.rb +111 -0
- data/hstruct.gemspec +26 -0
- data/lib/hstruct.rb +9 -0
- data/spec/lib/hstruct_spec.rb +26 -0
- data/spec/spec_helper.rb +17 -0
- metadata +158 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Gerhard Lazu
|
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,82 @@
|
|
1
|
+
Ruby Struct with the convenience of instantiating from a hash.
|
2
|
+
|
3
|
+
When you care about speed, this is the Ruby structure that you've been
|
4
|
+
looking for. HStructs are faster than any other gem out there, but still
|
5
|
+
only half as fast as when compared to a Class with hash arguments.
|
6
|
+
To make up for it, you will end up writing less code and you will get
|
7
|
+
a `to_hash` method by default (no, you don't have to be running Ruby 2.0).
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
class ClassWithArgsHash
|
11
|
+
attr_reader :foo, :bar, :baz, :qux, :quux
|
12
|
+
|
13
|
+
def initialize(args)
|
14
|
+
@foo = args[:foo]
|
15
|
+
@bar = args[:bar]
|
16
|
+
@baz = args[:baz]
|
17
|
+
@qux = args[:qux]
|
18
|
+
@quux = args[:quux]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
```
|
23
|
+
|
24
|
+
And this is the HStruct equivalent:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
MyHStruct = HStruct.new(:foo, :bar, :baz, :qux, :quux)
|
28
|
+
```
|
29
|
+
|
30
|
+
If you're thinking about setting the class instance variables
|
31
|
+
dynamically, the performance penalty might surprise you (run the
|
32
|
+
benchmarks to see what I mean).
|
33
|
+
|
34
|
+
## Installation
|
35
|
+
|
36
|
+
Add this line to your application's Gemfile:
|
37
|
+
|
38
|
+
gem 'hstruct'
|
39
|
+
|
40
|
+
And then execute:
|
41
|
+
|
42
|
+
$ bundle
|
43
|
+
|
44
|
+
Or install it yourself as:
|
45
|
+
|
46
|
+
$ gem install hstruct
|
47
|
+
|
48
|
+
## Usage
|
49
|
+
|
50
|
+
This is a surprisingly simple gem, just a few lines of code. Usage is
|
51
|
+
equally simple and straightforward. Here's an HStruct example with a
|
52
|
+
default value:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
HeartRate = HStruct.new(:patient_id, :bpm, :timestamp) do
|
56
|
+
def initialize(args)
|
57
|
+
super(args)
|
58
|
+
self[:timestamp] ||= Time.now.utc.to_i
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
[1] pry(main)> heart_rate = HeartRate.new(:patient_id => 1, :bpm => 88)
|
63
|
+
=> #<struct HeartRate patient_id=1, bpm=88, timestamp=1368786389>
|
64
|
+
[2] pry(main)> heart_rate.class
|
65
|
+
=> HeartRate
|
66
|
+
[3] pry(main)> heart_rate.patient_id
|
67
|
+
=> 1
|
68
|
+
[4] pry(main)> heart_rate.bpm
|
69
|
+
=> 88
|
70
|
+
[5] pry(main)> heart_rate.timestamp
|
71
|
+
=> 1368786389
|
72
|
+
[6] pry(main)> heart_rate.to_hash
|
73
|
+
=> {:patient_id=>1, :bpm=>88, :timestamp=>1368786389}
|
74
|
+
```
|
75
|
+
|
76
|
+
## Contributing
|
77
|
+
|
78
|
+
1. Fork it
|
79
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
80
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
81
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
82
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,111 @@
|
|
1
|
+
require 'benchmark/ips'
|
2
|
+
require 'pry'
|
3
|
+
|
4
|
+
# OpenStruct
|
5
|
+
require 'ostruct'
|
6
|
+
|
7
|
+
# Struct
|
8
|
+
PlainStruct = Struct.new(:foo, :bar, :baz, :qux, :quux)
|
9
|
+
|
10
|
+
# HStruct
|
11
|
+
require_relative '../lib/hstruct'
|
12
|
+
PlainHStruct = HStruct.new(:foo, :bar, :baz, :qux, :quux)
|
13
|
+
|
14
|
+
# Class
|
15
|
+
class SimpleClass
|
16
|
+
attr_reader :foo, :bar, :baz, :qux, :quux
|
17
|
+
|
18
|
+
def initialize(foo, bar, baz, qux, quux)
|
19
|
+
@foo = foo
|
20
|
+
@bar = bar
|
21
|
+
@baz = baz
|
22
|
+
@qux = qux
|
23
|
+
@quux = quux
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class CleverClass
|
28
|
+
attr_reader :foo, :bar, :baz, :qux, :quux
|
29
|
+
|
30
|
+
def initialize(args)
|
31
|
+
args.each { |k, v| instance_variable_set("@#{k}", v) }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class HashArgsClass
|
36
|
+
attr_reader :foo, :bar, :baz, :qux, :quux
|
37
|
+
|
38
|
+
def initialize(args)
|
39
|
+
@foo = args[:foo]
|
40
|
+
@bar = args[:bar]
|
41
|
+
@baz = args[:baz]
|
42
|
+
@qux = args[:qux]
|
43
|
+
@quux = args[:quux]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
hash = {
|
48
|
+
:foo => 1,
|
49
|
+
:bar => 2,
|
50
|
+
:baz => 3,
|
51
|
+
:qux => 4,
|
52
|
+
:quux => 5,
|
53
|
+
}
|
54
|
+
|
55
|
+
# All commented benchmarks were run on:
|
56
|
+
# * ruby 1.9.3p392 (2013-02-22 revision 39386) [x86_64-darwin12.3.0]
|
57
|
+
# * Intel i7 2.2Ghz (MBP 8,2)
|
58
|
+
|
59
|
+
runs = ENV.fetch('RUNS', 5).to_i
|
60
|
+
|
61
|
+
Benchmark.ips(runs) do |x|
|
62
|
+
# 10k/s
|
63
|
+
x.report('OpenStruct') do
|
64
|
+
OpenStruct.new(hash)
|
65
|
+
end
|
66
|
+
|
67
|
+
# 65k/s
|
68
|
+
x.report('Class Clever') do
|
69
|
+
CleverClass.new(hash)
|
70
|
+
end
|
71
|
+
|
72
|
+
# 170k/s
|
73
|
+
x.report('HStruct') do
|
74
|
+
PlainHStruct.new(hash)
|
75
|
+
end
|
76
|
+
|
77
|
+
# 325k/s
|
78
|
+
x.report('Hash') do
|
79
|
+
hash = {
|
80
|
+
:foo => 1,
|
81
|
+
:bar => 2,
|
82
|
+
:baz => 3,
|
83
|
+
:qux => 4,
|
84
|
+
:quux => 5,
|
85
|
+
}
|
86
|
+
end
|
87
|
+
|
88
|
+
# 350k/s
|
89
|
+
x.report('Class Hash Args') do
|
90
|
+
HashArgsClass.new(hash)
|
91
|
+
end
|
92
|
+
|
93
|
+
# 450k/s
|
94
|
+
x.report('Class Plain') do
|
95
|
+
SimpleClass.new(1, 2, 3, 4, 5)
|
96
|
+
end
|
97
|
+
|
98
|
+
# 600k/s
|
99
|
+
x.report('Struct') do
|
100
|
+
PlainStruct.new(1, 2, 3, 4, 5)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
### This is how some of the more popular gems compare to the above:
|
105
|
+
#
|
106
|
+
# Virtus => 12k/s (no coercions) & 17k/s (coercions)
|
107
|
+
# Hashr => 21k/s
|
108
|
+
# FastOpenStruct => 30k/s
|
109
|
+
# Hashie::Dash => 34k/s
|
110
|
+
# Hashie MI & MA => 98k/s
|
111
|
+
|
data/hstruct.gemspec
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
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "hstruct"
|
7
|
+
spec.version = "0.1.0"
|
8
|
+
spec.authors = ["Gerhard Lazu"]
|
9
|
+
spec.email = ["gerhard@lazu.co.uk"]
|
10
|
+
spec.description = %q{Ruby Struct with the convenience of instantiating from a hash.}
|
11
|
+
spec.summary = %q{When you care about speed, this is the Ruby structure that you've been looking for.}
|
12
|
+
spec.homepage = "https://github.com/cambridge-healthcare/hstruct"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files`.split($/)
|
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 "benchmark-ips"
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "guard-rspec"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
spec.add_development_dependency "rspec"
|
25
|
+
spec.add_development_dependency "pry"
|
26
|
+
end
|
data/lib/hstruct.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
|
3
|
+
require 'hstruct'
|
4
|
+
|
5
|
+
Person = HStruct.new(:first_name, :last_name)
|
6
|
+
|
7
|
+
describe HStruct do
|
8
|
+
let(:person) { }
|
9
|
+
|
10
|
+
it "is a Struct" do
|
11
|
+
Person.ancestors.should include Struct
|
12
|
+
end
|
13
|
+
|
14
|
+
it "can be instantiated from hash" do
|
15
|
+
person = Person.new(:first_name => "Jimmy")
|
16
|
+
person.first_name.should eql "Jimmy"
|
17
|
+
person.last_name.should be_nil
|
18
|
+
end
|
19
|
+
|
20
|
+
it "can be converted back to a hash" do
|
21
|
+
person = Person.new(:first_name => "Jimmy")
|
22
|
+
person.last_name = "Kiel"
|
23
|
+
person.to_hash.should eql Hash[:first_name => "Jimmy", :last_name => "Kiel"]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
9
|
+
config.run_all_when_everything_filtered = true
|
10
|
+
config.filter_run :focus
|
11
|
+
|
12
|
+
# Run specs in random order to surface order dependencies. If you find an
|
13
|
+
# order dependency and want to debug it, you can fix the order by providing
|
14
|
+
# the seed, which is printed after each run.
|
15
|
+
# --seed 1234
|
16
|
+
config.order = 'random'
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,158 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hstruct
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Gerhard Lazu
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-05-17 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: benchmark-ips
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: bundler
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '1.3'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '1.3'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: guard-rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rake
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rspec
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: pry
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
description: Ruby Struct with the convenience of instantiating from a hash.
|
111
|
+
email:
|
112
|
+
- gerhard@lazu.co.uk
|
113
|
+
executables: []
|
114
|
+
extensions: []
|
115
|
+
extra_rdoc_files: []
|
116
|
+
files:
|
117
|
+
- .gitignore
|
118
|
+
- .rspec
|
119
|
+
- Gemfile
|
120
|
+
- Guardfile
|
121
|
+
- LICENSE.txt
|
122
|
+
- README.md
|
123
|
+
- Rakefile
|
124
|
+
- benchmarks/hstruct.rb
|
125
|
+
- hstruct.gemspec
|
126
|
+
- lib/hstruct.rb
|
127
|
+
- spec/lib/hstruct_spec.rb
|
128
|
+
- spec/spec_helper.rb
|
129
|
+
homepage: https://github.com/cambridge-healthcare/hstruct
|
130
|
+
licenses:
|
131
|
+
- MIT
|
132
|
+
post_install_message:
|
133
|
+
rdoc_options: []
|
134
|
+
require_paths:
|
135
|
+
- lib
|
136
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
137
|
+
none: false
|
138
|
+
requirements:
|
139
|
+
- - ! '>='
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: '0'
|
142
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
143
|
+
none: false
|
144
|
+
requirements:
|
145
|
+
- - ! '>='
|
146
|
+
- !ruby/object:Gem::Version
|
147
|
+
version: '0'
|
148
|
+
requirements: []
|
149
|
+
rubyforge_project:
|
150
|
+
rubygems_version: 1.8.23
|
151
|
+
signing_key:
|
152
|
+
specification_version: 3
|
153
|
+
summary: When you care about speed, this is the Ruby structure that you've been looking
|
154
|
+
for.
|
155
|
+
test_files:
|
156
|
+
- spec/lib/hstruct_spec.rb
|
157
|
+
- spec/spec_helper.rb
|
158
|
+
has_rdoc:
|