hash_initialized_struct 1.0.0 → 1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 28eb0a537f9077e0834c71e8bc6ca52d2038c67e
4
- data.tar.gz: f55ba4273ae166f51743a855fda5f9dcec7fece1
3
+ metadata.gz: cc7ed250f43a1ae28eae74e01ceebf6d8af96784
4
+ data.tar.gz: ea0e9580302506002ff65c74085428b7893706b8
5
5
  SHA512:
6
- metadata.gz: 3d4498296eb6deaa2e57b3bf64629657a24d9db6d108549804012e6e4128f68922cf0ff7b76e72d3124c847d9c37a7fcd725489e4dd9dd22dfcdcf1f9bc5f3f9
7
- data.tar.gz: 1d98179af8ca82f5d29c54929f66c4f9110a2079e488f415854aaec6d70720325dfad205a08cfb7e838e35486141cc7a66ad1d9a7fa184bde79a555a7f1f2420
6
+ metadata.gz: 925de0d5261c4c00838b63800280cc9a89189ce108363dc8de75f717a39a602d8333708857ac4b9540b12c2250fd80849c9c4be08730d40a4f47bc0d3662b9f3
7
+ data.tar.gz: e28821abc99474aca7408ca85f5b3060370d5cbac0ed3d513585487a4d5bfb99d561925f3305cdf74dc1b4d87e56d5005807804aab2d63a9f8f918e7e487c8da
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.0
6
+ - 2.2.0
7
+ script:
8
+ - bundle exec rake spec SPEC_OPTS="--format d"
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Hash-Initialized Struct
1
+ # Hash-Initialized Struct [![Build Status](https://travis-ci.org/damncabbage/hash_initialized_struct.svg)](https://travis-ci.org/damncabbage/hash_initialized_struct)
2
2
 
3
3
  Halfway between Struct and OpenStruct. (Or: Struct, except it takes a Hash on object initialization.)
4
4
 
@@ -1,3 +1,3 @@
1
1
  class HashInitializedStruct
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -46,6 +46,12 @@ class HashInitializedStruct
46
46
  instance_variable_set("@#{attr}", value)
47
47
  end
48
48
  end
49
+
50
+ def to_h
51
+ # [k,k,...] => [[k,v], [k,v], ...] => {k => v, k => v, ...}
52
+ Hash[ self.class::STRUCT_ATTRS.map {|key| [key, self.public_send(key)]} ]
53
+ end
54
+ alias :to_hash :to_h
49
55
  end
50
56
  end
51
57
  end
@@ -35,6 +35,17 @@ describe "HashInitializedStruct" do
35
35
  }.to raise_error(ArgumentError, "Missing keys: :y")
36
36
  end
37
37
 
38
+ it "provides to_h" do
39
+ point = klass.new(x: 1, y: 2)
40
+ expect(point.to_h).to match({x: 1, y: 2})
41
+ end
42
+
43
+ it "provides to_hash for implicit conversions (Ruby 2+)" do
44
+ skip "Ignoring Ruby 1.9.x..." unless Kernel.respond_to?(:Hash)
45
+ point = klass.new(x: 1, y: 2)
46
+ expect(point.to_h).to eq Hash(point) # to_h == to_hash
47
+ end
48
+
38
49
  it "allows for overriding the constructor to add additional checks" do
39
50
  # Could do this with an anonymous class and define_method, but we're trying to emulate
40
51
  # how it might actually be used.
metadata CHANGED
@@ -1,55 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash_initialized_struct
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rob Howard
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-04 00:00:00.000000000 Z
11
+ date: 2015-04-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.6'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.6'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ~>
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
33
  version: '10.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ~>
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ~>
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
47
  version: '3.2'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ~>
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.2'
55
55
  description: |-
@@ -61,7 +61,8 @@ executables: []
61
61
  extensions: []
62
62
  extra_rdoc_files: []
63
63
  files:
64
- - .gitignore
64
+ - ".gitignore"
65
+ - ".travis.yml"
65
66
  - Gemfile
66
67
  - LICENSE.txt
67
68
  - README.md
@@ -80,17 +81,17 @@ require_paths:
80
81
  - lib
81
82
  required_ruby_version: !ruby/object:Gem::Requirement
82
83
  requirements:
83
- - - '>='
84
+ - - ">="
84
85
  - !ruby/object:Gem::Version
85
86
  version: '0'
86
87
  required_rubygems_version: !ruby/object:Gem::Requirement
87
88
  requirements:
88
- - - '>='
89
+ - - ">="
89
90
  - !ruby/object:Gem::Version
90
91
  version: '0'
91
92
  requirements: []
92
93
  rubyforge_project:
93
- rubygems_version: 2.0.14
94
+ rubygems_version: 2.2.2
94
95
  signing_key:
95
96
  specification_version: 4
96
97
  summary: Struct, except it takes a Hash on object initialization.