hstruct 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml ADDED
@@ -0,0 +1 @@
1
+ script: "bundle exec ruby test/test_helper.rb"
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ### v0.5.0
2
+
3
+ * to_h deprecates to_hash method, should serve as a reminder about Ruby
4
+ 2.0. Thanks @bestie!
5
+
1
6
  ### v0.4.0
2
7
 
3
8
  * leaving empty values out when converting to a hash was too intrusive.
data/README.md CHANGED
@@ -1,10 +1,12 @@
1
+ [![travis](https://secure.travis-ci.org/cambridge-healthcare/hstruct.png)]
2
+
1
3
  Struct with the convenience of instantiating from a Hash.
2
4
 
3
5
  When you care about speed, this is the Ruby structure that you've been
4
6
  looking for. HStructs are faster than any other gem out there, but still
5
7
  only half as fast as when compared to a Class with hash arguments.
6
8
  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).
9
+ a `to_h` method by default (no, you don't have to be running Ruby 2.0).
8
10
 
9
11
  ```ruby
10
12
  class ClassWithArgsHash
@@ -69,7 +71,7 @@ end
69
71
  => 88
70
72
  [5] pry(main)> heart_rate.timestamp
71
73
  => 1368786389
72
- [6] pry(main)> heart_rate.to_hash
74
+ [6] pry(main)> heart_rate.to_h
73
75
  => {:patient_id=>1, :bpm=>88, :timestamp=>1368786389}
74
76
  ```
75
77
 
@@ -5,27 +5,26 @@ require 'pry'
5
5
  require 'ostruct'
6
6
 
7
7
  # Struct
8
- PlainStruct = Struct.new(:foo, :bar, :baz, :qux, :quux)
8
+ PlainStruct = Struct.new(:foo, :bar, :baz, :qux)
9
9
 
10
10
  # HStruct
11
11
  require_relative '../lib/hstruct'
12
- PlainHStruct = HStruct.new(:foo, :bar, :baz, :qux, :quux)
12
+ PlainHStruct = HStruct.new(:foo, :bar, :baz, :qux)
13
13
 
14
14
  # Class
15
15
  class SimpleClass
16
- attr_reader :foo, :bar, :baz, :qux, :quux
16
+ attr_reader :foo, :bar, :baz, :qux
17
17
 
18
- def initialize(foo, bar, baz, qux, quux)
18
+ def initialize(foo, bar, baz, qux)
19
19
  @foo = foo
20
20
  @bar = bar
21
21
  @baz = baz
22
22
  @qux = qux
23
- @quux = quux
24
23
  end
25
24
  end
26
25
 
27
26
  class CleverClass
28
- attr_reader :foo, :bar, :baz, :qux, :quux
27
+ attr_reader :foo, :bar, :baz, :qux
29
28
 
30
29
  def initialize(args)
31
30
  args.each { |k, v| instance_variable_set("@#{k}", v) }
@@ -33,14 +32,13 @@ class CleverClass
33
32
  end
34
33
 
35
34
  class HashArgsClass
36
- attr_reader :foo, :bar, :baz, :qux, :quux
35
+ attr_reader :foo, :bar, :baz, :qux
37
36
 
38
37
  def initialize(args)
39
38
  @foo = args[:foo]
40
39
  @bar = args[:bar]
41
40
  @baz = args[:baz]
42
41
  @qux = args[:qux]
43
- @quux = args[:quux]
44
42
  end
45
43
  end
46
44
 
@@ -49,63 +47,61 @@ hash = {
49
47
  :bar => 2,
50
48
  :baz => 3,
51
49
  :qux => 4,
52
- :quux => 5,
53
50
  }
54
51
 
55
- # All commented benchmarks were run on:
52
+ # All benchmarks were run on:
56
53
  # * ruby 1.9.3p392 (2013-02-22 revision 39386) [x86_64-darwin12.3.0]
57
54
  # * Intel i7 2.2Ghz (MBP 8,2)
58
55
 
59
56
  runs = ENV.fetch('RUNS', 5).to_i
60
57
 
61
58
  Benchmark.ips(runs) do |x|
62
- # 10k/s
59
+ # 14k/s
63
60
  x.report('OpenStruct') do
64
61
  OpenStruct.new(hash)
65
62
  end
66
63
 
67
- # 65k/s
64
+ # 80k/s
68
65
  x.report('Class Clever') do
69
66
  CleverClass.new(hash)
70
67
  end
71
68
 
72
- # 170k/s
69
+ # 185k/s
73
70
  x.report('HStruct') do
74
71
  PlainHStruct.new(hash)
75
72
  end
76
73
 
77
- # 325k/s
74
+ # 380k/s
78
75
  x.report('Hash') do
79
76
  hash = {
80
77
  :foo => 1,
81
78
  :bar => 2,
82
79
  :baz => 3,
83
80
  :qux => 4,
84
- :quux => 5,
85
81
  }
86
82
  end
87
83
 
88
- # 350k/s
84
+ # 385k/s
89
85
  x.report('Class Hash Args') do
90
86
  HashArgsClass.new(hash)
91
87
  end
92
88
 
93
- # 450k/s
89
+ # 470k/s
94
90
  x.report('Class Plain') do
95
- SimpleClass.new(1, 2, 3, 4, 5)
91
+ SimpleClass.new(1, 2, 3, 4)
96
92
  end
97
93
 
98
- # 600k/s
94
+ # 620k/s
99
95
  x.report('Struct') do
100
- PlainStruct.new(1, 2, 3, 4, 5)
96
+ PlainStruct.new(1, 2, 3, 4)
101
97
  end
102
98
  end
103
99
 
104
100
  ### This is how some of the more popular gems compare to the above:
105
101
  #
106
102
  # Virtus => 12k/s (no coercions) & 17k/s (coercions)
107
- # Hashr => 21k/s
103
+ # Hashr => 20k/s
108
104
  # FastOpenStruct => 30k/s
109
- # Hashie::Dash => 34k/s
110
- # Hashie MI & MA => 98k/s
105
+ # Hashie::Dash => 35k/s
106
+ # Hashie MI & MA => 100k/s
111
107
 
data/hstruct.gemspec CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "hstruct"
7
- spec.version = "0.4.0"
7
+ spec.version = "0.5.0"
8
8
  spec.authors = ["Gerhard Lazu"]
9
9
  spec.email = ["gerhard@lazu.co.uk"]
10
10
  spec.description = %q{Struct with the convenience of instantiating from a Hash}
@@ -19,7 +19,8 @@ Gem::Specification.new do |spec|
19
19
 
20
20
  spec.add_development_dependency "benchmark-ips", "~> 1.2"
21
21
  spec.add_development_dependency "bundler", "~> 1.3"
22
- spec.add_development_dependency "rake", "~> 10.0"
23
- spec.add_development_dependency "minitest", "~> 5.0"
22
+ spec.add_development_dependency "minitest", "~> 4.7"
23
+ spec.add_development_dependency "minitest-reporters", "~> 0.14.19"
24
24
  spec.add_development_dependency "pry"
25
+ spec.add_development_dependency "rake", "~> 10.0"
25
26
  end
data/lib/hstruct.rb CHANGED
@@ -3,7 +3,8 @@ class HStruct < Struct
3
3
  super(*members.map { |m| args[m] }) if args
4
4
  end
5
5
 
6
- def to_hash
6
+ def to_h
7
7
  Hash[each_pair.to_a]
8
8
  end
9
+ alias_method :to_hash, :to_h
9
10
  end
@@ -26,7 +26,11 @@ describe "Person defined from an HStruct" do
26
26
 
27
27
  describe "when converting back to a hash" do
28
28
  it "includes empty values" do
29
- person.to_hash.must_equal Hash[:first_name => "Jimmy", :last_name => nil]
29
+ person.to_h.must_equal Hash[:first_name => "Jimmy", :last_name => nil]
30
+ end
31
+
32
+ it "to_hash alias" do
33
+ person.to_h.must_equal person.to_hash
30
34
  end
31
35
  end
32
36
  end
data/test/test_helper.rb CHANGED
@@ -1,3 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
1
3
  require 'minitest/autorun'
4
+ require "minitest/reporters"
5
+ MiniTest::Reporters.use! MiniTest::Reporters::SpecReporter.new
2
6
 
3
- Dir.glob('./test/**/*_spec.rb', &method(:require)) if __FILE__ == $0
7
+ Dir.glob('./test/**/*_test.rb', &method(:require)) if __FILE__ == $0
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hstruct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-22 00:00:00.000000000 Z
12
+ date: 2013-05-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: benchmark-ips
@@ -44,13 +44,13 @@ dependencies:
44
44
  - !ruby/object:Gem::Version
45
45
  version: '1.3'
46
46
  - !ruby/object:Gem::Dependency
47
- name: rake
47
+ name: minitest
48
48
  requirement: !ruby/object:Gem::Requirement
49
49
  none: false
50
50
  requirements:
51
51
  - - ~>
52
52
  - !ruby/object:Gem::Version
53
- version: '10.0'
53
+ version: '4.7'
54
54
  type: :development
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
@@ -58,15 +58,15 @@ dependencies:
58
58
  requirements:
59
59
  - - ~>
60
60
  - !ruby/object:Gem::Version
61
- version: '10.0'
61
+ version: '4.7'
62
62
  - !ruby/object:Gem::Dependency
63
- name: minitest
63
+ name: minitest-reporters
64
64
  requirement: !ruby/object:Gem::Requirement
65
65
  none: false
66
66
  requirements:
67
67
  - - ~>
68
68
  - !ruby/object:Gem::Version
69
- version: '5.0'
69
+ version: 0.14.19
70
70
  type: :development
71
71
  prerelease: false
72
72
  version_requirements: !ruby/object:Gem::Requirement
@@ -74,7 +74,7 @@ dependencies:
74
74
  requirements:
75
75
  - - ~>
76
76
  - !ruby/object:Gem::Version
77
- version: '5.0'
77
+ version: 0.14.19
78
78
  - !ruby/object:Gem::Dependency
79
79
  name: pry
80
80
  requirement: !ruby/object:Gem::Requirement
@@ -91,6 +91,22 @@ dependencies:
91
91
  - - ! '>='
92
92
  - !ruby/object:Gem::Version
93
93
  version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: rake
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: '10.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: '10.0'
94
110
  description: Struct with the convenience of instantiating from a Hash
95
111
  email:
96
112
  - gerhard@lazu.co.uk
@@ -99,7 +115,7 @@ extensions: []
99
115
  extra_rdoc_files: []
100
116
  files:
101
117
  - .gitignore
102
- - .rspec
118
+ - .travis.yml
103
119
  - CHANGELOG.md
104
120
  - Gemfile
105
121
  - LICENSE.txt
@@ -108,7 +124,7 @@ files:
108
124
  - benchmarks/hstruct.rb
109
125
  - hstruct.gemspec
110
126
  - lib/hstruct.rb
111
- - test/lib/hstruct_spec.rb
127
+ - test/lib/hstruct_test.rb
112
128
  - test/test_helper.rb
113
129
  homepage: https://github.com/cambridge-healthcare/hstruct
114
130
  licenses:
@@ -137,6 +153,6 @@ specification_version: 3
137
153
  summary: When you care about speed and convenience, this is what you've been looking
138
154
  for
139
155
  test_files:
140
- - test/lib/hstruct_spec.rb
156
+ - test/lib/hstruct_test.rb
141
157
  - test/test_helper.rb
142
158
  has_rdoc:
data/.rspec DELETED
@@ -1,2 +0,0 @@
1
- --color
2
- --format doc