ofstruct 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +24 -4
- data/lib/ofstruct.rb +8 -1
- data/spec/lib/ofstruct_spec.rb +22 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cc0d0332d4b8b6ce6adc7df06f8d9aaf63c463bf
|
4
|
+
data.tar.gz: 67e0a85b29683cef3e2e495f71a37e05b05defb2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f2d567ec0621b81122b5924b8a49b9c9111a497a1d6fe53740f75e55ed46c008fc4952bad472ad16e7688fb37ada5b1871ed3eb765b25a4aff778a40e28d4444
|
7
|
+
data.tar.gz: 11f6e81aec9ff93a8f7da30942df17392204a3f3a9fc249946eda3465e8b1935b1e2ae902b62a3a97fcc276f0124a337ef3711b96c6bcfee008e71cfa6a0b88e
|
data/README.md
CHANGED
@@ -1,10 +1,30 @@
|
|
1
1
|
# OpenFastStruct
|
2
2
|
|
3
|
+
[![Code Climate](https://codeclimate.com/github/arturoherrero/ofstruct/badges/gpa.svg)](https://codeclimate.com/github/arturoherrero/ofstruct)
|
4
|
+
[![Build Status](https://travis-ci.org/arturoherrero/ofstruct.svg)](https://travis-ci.org/arturoherrero/ofstruct)
|
5
|
+
|
3
6
|
OpenStruct allows the creation of data objects with arbitrary attributes.
|
4
7
|
|
5
|
-
**OpenFastStruct** is a data structure, similar to an OpenStruct, that allows the
|
8
|
+
**OpenFastStruct** is a data structure, similar* to an OpenStruct, that allows the
|
6
9
|
definition of arbitrary attributes with their accompanying values. It benchmarks
|
7
|
-
~3x slower than a Hash, but it's ~4x faster than OpenStruct
|
10
|
+
~3x slower than a Hash, but **it's ~4x faster than OpenStruct**.
|
11
|
+
|
12
|
+
*An OpenFastStruct is not exactly like an OpenStruct, these are the main
|
13
|
+
differences between them:
|
14
|
+
- OpenFastStruct doesn't allow hash access like `person[:name]`.
|
15
|
+
- OpenFastStruct doesn't provide marshalling.
|
16
|
+
- OpenFastStruct allows infinite chaining of attributes <sup>[example](#black-hole-object)</sup>.
|
17
|
+
|
18
|
+
|
19
|
+
## Installation
|
20
|
+
|
21
|
+
Install the gem:
|
22
|
+
|
23
|
+
$ gem install ofstruct
|
24
|
+
|
25
|
+
Use the gem in a project managed with Bundler adding it into the Gemfile:
|
26
|
+
|
27
|
+
gem "ofstruct"
|
8
28
|
|
9
29
|
|
10
30
|
## Examples
|
@@ -72,7 +92,7 @@ puts person.address.number # -> 4
|
|
72
92
|
Probably you heard that you should never, ever use OpenStruct because the
|
73
93
|
performance penalty is prohibitive. You can use OpenFastStruct instead!
|
74
94
|
|
75
|
-
|
95
|
+
#### Comparation between Hash, OpenFastStruct and OpenStruct:
|
76
96
|
|
77
97
|
```
|
78
98
|
$ ruby benchmark/hash_ofstruct_ostruct.rb
|
@@ -91,7 +111,7 @@ Comparison:
|
|
91
111
|
OpenStruct: 45601.6 i/s - 10.69x slower
|
92
112
|
```
|
93
113
|
|
94
|
-
|
114
|
+
#### Comparation between RSpec Stubs, OpenFastStruct and OpenStruct:
|
95
115
|
|
96
116
|
```
|
97
117
|
$ ruby benchmark/double_ofstruct_ostruct.rb
|
data/lib/ofstruct.rb
CHANGED
@@ -17,7 +17,9 @@ class OpenFastStruct
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def to_h
|
20
|
-
@members
|
20
|
+
@members.inject({}) do |result, (k, v)|
|
21
|
+
result.tap { |result| result[k] = v.is_a?(self.class) ? v.to_h : v }
|
22
|
+
end
|
21
23
|
end
|
22
24
|
|
23
25
|
def inspect
|
@@ -29,6 +31,11 @@ class OpenFastStruct
|
|
29
31
|
nil
|
30
32
|
end
|
31
33
|
|
34
|
+
def ==(other)
|
35
|
+
return false unless other.is_a?(self.class)
|
36
|
+
self.to_h == other.to_h
|
37
|
+
end
|
38
|
+
|
32
39
|
private
|
33
40
|
|
34
41
|
def method_missing(name, *args)
|
data/spec/lib/ofstruct_spec.rb
CHANGED
@@ -14,15 +14,18 @@ RSpec.describe OpenFastStruct do
|
|
14
14
|
|
15
15
|
it "works recursive accessor" do
|
16
16
|
ofstruct.user.name = "John"
|
17
|
-
ofstruct.name = "OpenFastStruct"
|
18
|
-
expect(ofstruct.user).to be_a(described_class)
|
19
17
|
expect(ofstruct.user.name).to eq("John")
|
20
|
-
expect(ofstruct.name).to eq("OpenFastStruct")
|
21
18
|
end
|
22
19
|
|
23
20
|
it "works with unexisting values" do
|
24
21
|
expect(ofstruct.name).to be_a(described_class)
|
25
|
-
|
22
|
+
end
|
23
|
+
|
24
|
+
it "equals to other with same attributes" do
|
25
|
+
ofstruct.user.name = "John"
|
26
|
+
other_ofstruct = described_class.new
|
27
|
+
other_ofstruct.user.name = "John"
|
28
|
+
expect(ofstruct).to eq(other_ofstruct)
|
26
29
|
end
|
27
30
|
|
28
31
|
context "instantiated from arguments" do
|
@@ -59,7 +62,7 @@ RSpec.describe OpenFastStruct do
|
|
59
62
|
end
|
60
63
|
end
|
61
64
|
|
62
|
-
describe "#
|
65
|
+
describe "#each_pair" do
|
63
66
|
it "yields all keys with the values" do
|
64
67
|
expect(ofstruct.each_pair.to_a).to eq([[:name, "John"]])
|
65
68
|
end
|
@@ -92,6 +95,20 @@ RSpec.describe OpenFastStruct do
|
|
92
95
|
it "converts to a hash" do
|
93
96
|
expect(ofstruct.to_h).to eq(args)
|
94
97
|
end
|
98
|
+
|
99
|
+
it "converts to a hash with chained attributes" do
|
100
|
+
ofstruct.address.street = "Sunset Boulevar"
|
101
|
+
ofstruct.address.number = 4
|
102
|
+
expect(ofstruct.to_h).to eq(
|
103
|
+
{
|
104
|
+
:name => "John",
|
105
|
+
:address => {
|
106
|
+
:street => "Sunset Boulevar",
|
107
|
+
:number => 4,
|
108
|
+
}
|
109
|
+
}
|
110
|
+
)
|
111
|
+
end
|
95
112
|
end
|
96
113
|
|
97
114
|
describe "#inspect" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ofstruct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Arturo Herrero
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-03-
|
11
|
+
date: 2015-03-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: benchmark-ips
|