hstruct 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,6 +1,14 @@
1
+ ### v0.6.0
2
+
3
+ * Support for hashes with string keys. We came across this problem when
4
+ persisting a hash with symbol keys and were getting back string keys.
5
+ Thanks @bestie!
6
+
7
+ * removed `to_hash`
8
+
1
9
  ### v0.5.0
2
10
 
3
- * to_h deprecates to_hash method, should serve as a reminder about Ruby
11
+ * `to_h` deprecates `to_hash` method, should serve as a reminder about Ruby
4
12
  2.0. Thanks @bestie!
5
13
 
6
14
  ### v0.4.0
data/Gemfile CHANGED
@@ -2,3 +2,5 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in hstruct.gemspec
4
4
  gemspec
5
+
6
+ gem 'attr_extras'
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- [![travis](https://secure.travis-ci.org/cambridge-healthcare/hstruct.png)]
1
+ [![travis](https://secure.travis-ci.org/cambridge-healthcare/hstruct.png)](https://travis-ci.org/cambridge-healthcare/hstruct)
2
2
 
3
3
  Struct with the convenience of instantiating from a Hash.
4
4
 
@@ -10,14 +10,13 @@ a `to_h` method by default (no, you don't have to be running Ruby 2.0).
10
10
 
11
11
  ```ruby
12
12
  class ClassWithArgsHash
13
- attr_reader :foo, :bar, :baz, :qux, :quux
13
+ attr_reader :foo, :bar, :baz, :qux
14
14
 
15
15
  def initialize(args)
16
16
  @foo = args[:foo]
17
17
  @bar = args[:bar]
18
18
  @baz = args[:baz]
19
19
  @qux = args[:qux]
20
- @quux = args[:quux]
21
20
  end
22
21
  end
23
22
 
@@ -26,7 +25,7 @@ end
26
25
  And this is the HStruct equivalent:
27
26
 
28
27
  ```ruby
29
- MyHStruct = HStruct.new(:foo, :bar, :baz, :qux, :quux)
28
+ MyHStruct = HStruct.new(:foo, :bar, :baz, :qux)
30
29
  ```
31
30
 
32
31
  If you're thinking about setting the class instance variables
@@ -49,6 +49,12 @@ hash = {
49
49
  :qux => 4,
50
50
  }
51
51
 
52
+ #require 'attr_extras'
53
+ #class AttrExtrasClass
54
+ #attr_initialize [:foo, :bar, :baz, :qux]
55
+ #attr_reader :foo, :bar, :baz, :qux
56
+ #end
57
+
52
58
  # All benchmarks were run on:
53
59
  # * ruby 1.9.3p392 (2013-02-22 revision 39386) [x86_64-darwin12.3.0]
54
60
  # * Intel i7 2.2Ghz (MBP 8,2)
@@ -81,6 +87,11 @@ Benchmark.ips(runs) do |x|
81
87
  }
82
88
  end
83
89
 
90
+ # 200k/s
91
+ #x.report('Class AttrExtras') do
92
+ #AttrExtrasClass.new(hash)
93
+ #end
94
+
84
95
  # 385k/s
85
96
  x.report('Class Hash Args') do
86
97
  HashArgsClass.new(hash)
@@ -104,4 +115,5 @@ end
104
115
  # FastOpenStruct => 30k/s
105
116
  # Hashie::Dash => 35k/s
106
117
  # Hashie MI & MA => 100k/s
107
-
118
+ # AttrExtras => 200k/s
119
+ #
data/hstruct.gemspec CHANGED
@@ -4,9 +4,9 @@ $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.5.0"
8
- spec.authors = ["Gerhard Lazu"]
9
- spec.email = ["gerhard@lazu.co.uk"]
7
+ spec.version = "0.6.0"
8
+ spec.authors = ["Gerhard Lazu", "Stephen Best"]
9
+ spec.email = ["gerhard@lazu.co.uk", "bestie@gmail.com"]
10
10
  spec.description = %q{Struct with the convenience of instantiating from a Hash}
11
11
  spec.summary = %q{When you care about speed and convenience, this is what you've been looking for}
12
12
  spec.homepage = "https://github.com/cambridge-healthcare/hstruct"
data/lib/hstruct.rb CHANGED
@@ -1,10 +1,17 @@
1
1
  class HStruct < Struct
2
- def initialize(args = nil)
3
- super(*members.map { |m| args[m] }) if args
2
+ def initialize(args = {})
3
+ raise ArgumentError unless args.is_a?(Enumerable)
4
+
5
+ unless args.keys.first.is_a?(Symbol)
6
+ args = args.each_with_object({}) do |(k,v), result|
7
+ result[k.to_sym] = v
8
+ end
9
+ end
10
+
11
+ super(*members.map { |m| args[m] })
4
12
  end
5
13
 
6
14
  def to_h
7
15
  Hash[each_pair.to_a]
8
16
  end
9
- alias_method :to_hash, :to_h
10
17
  end
@@ -1,6 +1,7 @@
1
1
  require_relative '../test_helper'
2
2
 
3
3
  require 'hstruct'
4
+ require 'pry'
4
5
 
5
6
  Person = HStruct.new(:first_name, :last_name)
6
7
 
@@ -13,25 +14,51 @@ describe "Person defined from an HStruct" do
13
14
  Person.new.must_be_instance_of Person
14
15
  end
15
16
 
17
+ it "argument must be enumerable" do
18
+ not_enumerable = Object.new
19
+
20
+ ->{ Person.new(not_enumerable) }.must_raise ArgumentError
21
+ end
22
+
16
23
  describe "instantiated from a hash" do
17
- let(:person) { Person.new(:first_name => "Jimmy") }
24
+ describe "with Symbol keys" do
25
+ let(:person_hash) { {
26
+ :first_name => "Jimmy",
27
+ } }
28
+
29
+ let(:person) { Person.new(person_hash) }
18
30
 
19
- it "first name" do
20
- person.first_name.must_equal "Jimmy"
31
+ it "first name" do
32
+ person.first_name.must_equal "Jimmy"
33
+ end
34
+
35
+ it "last name" do
36
+ person.last_name.must_equal nil
37
+ end
21
38
  end
22
39
 
23
- it "last name" do
24
- person.last_name.must_equal nil
40
+ describe "with String keys" do
41
+ let(:person_hash) { {
42
+ "first_name" => "Alex",
43
+ } }
44
+
45
+ let(:person) { Person.new(person_hash) }
46
+
47
+ it "first name" do
48
+ person.first_name.must_equal "Alex"
49
+ end
50
+
51
+ it "last name" do
52
+ person.last_name.must_equal nil
53
+ end
25
54
  end
26
55
 
27
56
  describe "when converting back to a hash" do
57
+ let(:person) { Person.new( :first_name => "Jimmy" ) }
58
+
28
59
  it "includes empty values" do
29
60
  person.to_h.must_equal Hash[:first_name => "Jimmy", :last_name => nil]
30
61
  end
31
-
32
- it "to_hash alias" do
33
- person.to_h.must_equal person.to_hash
34
- end
35
62
  end
36
63
  end
37
64
  end
metadata CHANGED
@@ -1,15 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hstruct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Gerhard Lazu
9
+ - Stephen Best
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
- date: 2013-05-23 00:00:00.000000000 Z
13
+ date: 2013-06-11 00:00:00.000000000 Z
13
14
  dependencies:
14
15
  - !ruby/object:Gem::Dependency
15
16
  name: benchmark-ips
@@ -110,6 +111,7 @@ dependencies:
110
111
  description: Struct with the convenience of instantiating from a Hash
111
112
  email:
112
113
  - gerhard@lazu.co.uk
114
+ - bestie@gmail.com
113
115
  executables: []
114
116
  extensions: []
115
117
  extra_rdoc_files: []
@@ -155,4 +157,3 @@ summary: When you care about speed and convenience, this is what you've been loo
155
157
  test_files:
156
158
  - test/lib/hstruct_test.rb
157
159
  - test/test_helper.rb
158
- has_rdoc: