ofstruct 0.3.0 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/ofstruct.rb +26 -8
- metadata +48 -9
- data/spec/lib/ofstruct_spec.rb +0 -151
- data/spec/spec_helper.rb +0 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d651a8d4353c6ce27531f7da77ae5fc04cc77562779587957b5b79f4c1138ead
|
4
|
+
data.tar.gz: c4680069d8296d8117d7f07d1dbcefc6c21db09555d6bf7199a888c8d901b9cb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4c340dc7e9e8485888a4ee55cf7060d27b7b8a97d12acdd886a87ab6c7d1c2dd8e7dd91e5f70536200dd5e89b860e8d1e5c6e6b17780f41f468de20c09937efa
|
7
|
+
data.tar.gz: 55a42e5035ff0262ccfb8c8650911b31afdeccc2ed6fb9b4c429e2ca99417945c3f61f122598b29ca06acd002f9abad6e1be40b5f35bc09bd965540608ffe403
|
data/lib/ofstruct.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class OpenFastStruct
|
2
4
|
def initialize(args = {})
|
3
5
|
@members = {}
|
@@ -14,24 +16,33 @@ class OpenFastStruct
|
|
14
16
|
|
15
17
|
def update(args)
|
16
18
|
ensure_hash!(args)
|
17
|
-
args.each { |
|
19
|
+
args.each { |key, value| assign(key, value) }
|
18
20
|
end
|
19
21
|
|
20
22
|
def to_h
|
21
|
-
@members.merge(@members)
|
23
|
+
@members.merge(@members) do |_, value|
|
24
|
+
case value
|
25
|
+
when Array
|
26
|
+
value.map(&:to_h)
|
27
|
+
when self.class
|
28
|
+
value.to_h
|
29
|
+
else
|
30
|
+
value
|
31
|
+
end
|
32
|
+
end
|
22
33
|
end
|
23
34
|
|
24
35
|
def inspect
|
25
|
-
"#<#{self.class}
|
36
|
+
"#<#{self.class}#{@members.map { |key, value| " :#{key}=#{value.inspect}" }.join}>"
|
26
37
|
end
|
27
|
-
alias
|
38
|
+
alias to_s inspect
|
28
39
|
|
29
40
|
def to_ary
|
30
41
|
nil
|
31
42
|
end
|
32
43
|
|
33
44
|
def ==(other)
|
34
|
-
other.is_a?(self.class) &&
|
45
|
+
other.is_a?(self.class) && to_h == other.to_h
|
35
46
|
end
|
36
47
|
|
37
48
|
private
|
@@ -51,10 +62,17 @@ class OpenFastStruct
|
|
51
62
|
end
|
52
63
|
|
53
64
|
def assign(key, value)
|
54
|
-
|
55
|
-
|
65
|
+
@members[key.to_sym] = process(value)
|
66
|
+
end
|
67
|
+
|
68
|
+
def process(data)
|
69
|
+
case data
|
70
|
+
when Hash
|
71
|
+
self.class.new(data)
|
72
|
+
when Array
|
73
|
+
data.map { |element| process(element) }
|
56
74
|
else
|
57
|
-
|
75
|
+
data
|
58
76
|
end
|
59
77
|
end
|
60
78
|
end
|
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.3.
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Arturo Herrero
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-01-
|
11
|
+
date: 2023-01-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: benchmark-ips
|
@@ -66,6 +66,48 @@ dependencies:
|
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '3.12'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.44'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.44'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop-rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0.6'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0.6'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rubocop-rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '2.18'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '2.18'
|
69
111
|
description: OpenFastStruct is a data structure, similar to an OpenStruct but faster.
|
70
112
|
email: arturo.herrero@gmail.com
|
71
113
|
executables: []
|
@@ -75,12 +117,11 @@ files:
|
|
75
117
|
- LICENSE
|
76
118
|
- README.md
|
77
119
|
- lib/ofstruct.rb
|
78
|
-
- spec/lib/ofstruct_spec.rb
|
79
|
-
- spec/spec_helper.rb
|
80
120
|
homepage: https://github.com/arturoherrero/ofstruct
|
81
121
|
licenses:
|
82
122
|
- MIT
|
83
|
-
metadata:
|
123
|
+
metadata:
|
124
|
+
rubygems_mfa_required: 'true'
|
84
125
|
post_install_message:
|
85
126
|
rdoc_options: []
|
86
127
|
require_paths:
|
@@ -89,7 +130,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
89
130
|
requirements:
|
90
131
|
- - ">="
|
91
132
|
- !ruby/object:Gem::Version
|
92
|
-
version:
|
133
|
+
version: '3.0'
|
93
134
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
135
|
requirements:
|
95
136
|
- - ">="
|
@@ -100,6 +141,4 @@ rubygems_version: 3.4.1
|
|
100
141
|
signing_key:
|
101
142
|
specification_version: 4
|
102
143
|
summary: OpenFastStruct
|
103
|
-
test_files:
|
104
|
-
- spec/lib/ofstruct_spec.rb
|
105
|
-
- spec/spec_helper.rb
|
144
|
+
test_files: []
|
data/spec/lib/ofstruct_spec.rb
DELETED
@@ -1,151 +0,0 @@
|
|
1
|
-
require "ofstruct"
|
2
|
-
|
3
|
-
RSpec.describe OpenFastStruct do
|
4
|
-
subject(:ofstruct) { described_class.new }
|
5
|
-
|
6
|
-
it "can be instantiated with no arguments" do
|
7
|
-
expect(ofstruct).to be_a(described_class)
|
8
|
-
end
|
9
|
-
|
10
|
-
it "works accessor" do
|
11
|
-
ofstruct.name = "John"
|
12
|
-
expect(ofstruct.name).to eq("John")
|
13
|
-
end
|
14
|
-
|
15
|
-
it "works recursive accessor" do
|
16
|
-
ofstruct.user.name = "John"
|
17
|
-
expect(ofstruct.user.name).to eq("John")
|
18
|
-
end
|
19
|
-
|
20
|
-
it "works with unexisting values" do
|
21
|
-
expect(ofstruct.name).to be_a(described_class)
|
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)
|
29
|
-
end
|
30
|
-
|
31
|
-
context "instantiated from arguments" do
|
32
|
-
subject(:ofstruct) { described_class.new(args) }
|
33
|
-
|
34
|
-
context "without hash" do
|
35
|
-
let(:args) { double }
|
36
|
-
|
37
|
-
it "raises an exception" do
|
38
|
-
expect { ofstruct }.to raise_error(ArgumentError)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
context "with hash" do
|
43
|
-
let(:symbol_args) { { :name => "John" } }
|
44
|
-
let(:string_args) { { "name" => "John" } }
|
45
|
-
let(:nested_args) { { person: { name: "John" } } }
|
46
|
-
|
47
|
-
context "with a nested hash" do
|
48
|
-
let(:args) { nested_args }
|
49
|
-
|
50
|
-
it "works with reader" do
|
51
|
-
expect(ofstruct.person.name).to eq("John")
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
context "with symbol keys" do
|
56
|
-
let(:args) { symbol_args }
|
57
|
-
|
58
|
-
it "works with reader" do
|
59
|
-
expect(ofstruct.name).to eq("John")
|
60
|
-
end
|
61
|
-
|
62
|
-
it "works accessor" do
|
63
|
-
ofstruct.name = "John Smith"
|
64
|
-
expect(ofstruct.name).to eq("John Smith")
|
65
|
-
end
|
66
|
-
|
67
|
-
describe "#delete_field" do
|
68
|
-
it "removes the named key" do
|
69
|
-
ofstruct.delete_field(:name)
|
70
|
-
expect(ofstruct.name).to be_a(described_class)
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
describe "#each_pair" do
|
75
|
-
it "yields all keys with the values" do
|
76
|
-
expect(ofstruct.each_pair.to_a).to eq([[:name, "John"]])
|
77
|
-
end
|
78
|
-
|
79
|
-
it "returns an enumerator" do
|
80
|
-
expect(ofstruct.each_pair).to be_a(Enumerator)
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
describe "#update" do
|
85
|
-
it "overwrite previous keys" do
|
86
|
-
ofstruct.update(:name => "John Smith")
|
87
|
-
expect(ofstruct.name).to eq("John Smith")
|
88
|
-
end
|
89
|
-
|
90
|
-
it "adds new keys" do
|
91
|
-
ofstruct.update(:surname => "Smith")
|
92
|
-
expect(ofstruct.name).to eq("John")
|
93
|
-
expect(ofstruct.surname).to eq("Smith")
|
94
|
-
end
|
95
|
-
|
96
|
-
context "without hash" do
|
97
|
-
it "raises an exception" do
|
98
|
-
expect { ofstruct.update(double) }.to raise_error(ArgumentError)
|
99
|
-
end
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
describe "#to_h" do
|
104
|
-
it "converts to a hash" do
|
105
|
-
expect(ofstruct.to_h).to eq(args)
|
106
|
-
end
|
107
|
-
|
108
|
-
it "converts to a hash with chained attributes" do
|
109
|
-
ofstruct.address.street = "Sunset Boulevar"
|
110
|
-
ofstruct.address.number = 4
|
111
|
-
expect(ofstruct.to_h).to eq(
|
112
|
-
{
|
113
|
-
:name => "John",
|
114
|
-
:address => {
|
115
|
-
:street => "Sunset Boulevar",
|
116
|
-
:number => 4,
|
117
|
-
}
|
118
|
-
}
|
119
|
-
)
|
120
|
-
end
|
121
|
-
end
|
122
|
-
|
123
|
-
describe "#inspect" do
|
124
|
-
it "returns the human-readable representation" do
|
125
|
-
expect(ofstruct.inspect).to eq('#<OpenFastStruct :name="John">')
|
126
|
-
end
|
127
|
-
end
|
128
|
-
end
|
129
|
-
|
130
|
-
context "with string keys" do
|
131
|
-
let(:args) { string_args }
|
132
|
-
|
133
|
-
it "works with reader" do
|
134
|
-
expect(ofstruct.name).to eq("John")
|
135
|
-
end
|
136
|
-
|
137
|
-
describe "#to_h" do
|
138
|
-
it "converts to a hash with keys as symbols" do
|
139
|
-
expect(ofstruct.to_h).to eq(symbol_args)
|
140
|
-
end
|
141
|
-
end
|
142
|
-
|
143
|
-
describe "#inspect" do
|
144
|
-
it "returns the human-readable representation" do
|
145
|
-
expect(ofstruct.inspect).to eq('#<OpenFastStruct :name="John">')
|
146
|
-
end
|
147
|
-
end
|
148
|
-
end
|
149
|
-
end
|
150
|
-
end
|
151
|
-
end
|
data/spec/spec_helper.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
RSpec.configure do |config|
|
2
|
-
config.expect_with :rspec do |expectations|
|
3
|
-
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
4
|
-
end
|
5
|
-
|
6
|
-
config.mock_with :rspec do |mocks|
|
7
|
-
mocks.verify_partial_doubles = true
|
8
|
-
end
|
9
|
-
|
10
|
-
config.disable_monkey_patching!
|
11
|
-
|
12
|
-
if config.files_to_run.one?
|
13
|
-
config.default_formatter = 'doc'
|
14
|
-
end
|
15
|
-
|
16
|
-
config.order = :random
|
17
|
-
Kernel.srand config.seed
|
18
|
-
end
|