rom 3.3.1 → 3.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/lib/rom/pipeline.rb +2 -0
- data/lib/rom/types.rb +30 -0
- data/lib/rom/version.rb +1 -1
- data/spec/unit/rom/relation/composite_spec.rb +17 -0
- data/spec/unit/rom/relation/schema_spec.rb +85 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 351889d2541ad18d05c029e555b635919c68c50c
|
4
|
+
data.tar.gz: c66be9ee892b0ee4c4f2c7ec6505739d9a811df1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 451aa0e25642c399e67eda6cddd8db5c67b801bbea371f291094913d693e9f226ed5defe783df59dfebebf4b1fd67a3577096df13c91a63faec8a2260bf0eeed
|
7
|
+
data.tar.gz: 41db751db49169072c52b16ffd07f4cbd694463dd2a64b484553d1a9542e47db81492b17d0e1914e489c4cf02dfa8f49ffd4bad905f271b4fc45177c760b7580
|
data/CHANGELOG.md
CHANGED
data/lib/rom/pipeline.rb
CHANGED
data/lib/rom/types.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'dry-types'
|
2
|
+
require 'json'
|
2
3
|
|
3
4
|
module ROM
|
4
5
|
module Types
|
@@ -6,6 +7,10 @@ module ROM
|
|
6
7
|
|
7
8
|
def self.included(other)
|
8
9
|
other.extend(Methods)
|
10
|
+
other::Coercible.extend(CoercibleMethods)
|
11
|
+
other::Coercible.const_set('JSON', other::Coercible.JSON)
|
12
|
+
other::Coercible.const_set('JSONHash', other::Coercible.JSONHash)
|
13
|
+
other::Coercible.const_set('HashJSON', other::Coercible.HashJSON)
|
9
14
|
super
|
10
15
|
end
|
11
16
|
|
@@ -22,5 +27,30 @@ module ROM
|
|
22
27
|
type.meta(foreign_key: true, target: relation)
|
23
28
|
end
|
24
29
|
end
|
30
|
+
|
31
|
+
module CoercibleMethods
|
32
|
+
def JSONHash(symbol_keys: false, type: Types::Hash)
|
33
|
+
Types.Constructor(type) do |value|
|
34
|
+
next Hash[value] if value.respond_to?(:to_hash)
|
35
|
+
|
36
|
+
begin
|
37
|
+
::JSON.parse(value.to_s, symbolize_names: symbol_keys)
|
38
|
+
rescue ::JSON::ParserError
|
39
|
+
value
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def HashJSON(type: Types::String)
|
45
|
+
Types.Constructor(type) do |value|
|
46
|
+
next value unless value.respond_to?(:to_hash)
|
47
|
+
::JSON.dump(value)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def JSON(symbol_keys: false)
|
52
|
+
self.HashJSON.meta(read: self.JSONHash(symbol_keys: symbol_keys))
|
53
|
+
end
|
54
|
+
end
|
25
55
|
end
|
26
56
|
end
|
data/lib/rom/version.rb
CHANGED
@@ -6,6 +6,11 @@ RSpec.describe ROM::Relation::Composite do
|
|
6
6
|
|
7
7
|
let(:users_relation) do
|
8
8
|
Class.new(ROM::Memory::Relation) do
|
9
|
+
schema do
|
10
|
+
attribute :name, ROM::Types::String
|
11
|
+
attribute :email, ROM::Types::String
|
12
|
+
end
|
13
|
+
|
9
14
|
def by_name(name)
|
10
15
|
restrict(name: name)
|
11
16
|
end
|
@@ -13,6 +18,10 @@ RSpec.describe ROM::Relation::Composite do
|
|
13
18
|
def sorted(other)
|
14
19
|
other.source.order(:name)
|
15
20
|
end
|
21
|
+
|
22
|
+
def select(key)
|
23
|
+
project(key)
|
24
|
+
end
|
16
25
|
end.new(users_dataset)
|
17
26
|
end
|
18
27
|
|
@@ -86,4 +95,12 @@ RSpec.describe ROM::Relation::Composite do
|
|
86
95
|
expect(relation.first).to eql('JOE')
|
87
96
|
end
|
88
97
|
end
|
98
|
+
|
99
|
+
describe '#__send__' do
|
100
|
+
it 'proxies Kernel methods' do
|
101
|
+
relation = (users_relation >> name_list >> upcaser).__send__(:select, :name)
|
102
|
+
|
103
|
+
expect(relation.call).to match_array(%w(JANE JOE))
|
104
|
+
end
|
105
|
+
end
|
89
106
|
end
|
@@ -81,6 +81,91 @@ RSpec.describe ROM::Relation, '.schema' do
|
|
81
81
|
expect(schema.foreign_key(:users)).to be(schema[:author_id])
|
82
82
|
end
|
83
83
|
|
84
|
+
it 'allows JSON read/write coersion', aggregate_failures: true do
|
85
|
+
class Test::Posts < ROM::Relation[:memory]
|
86
|
+
schema do
|
87
|
+
attribute :payload, Types::Coercible::JSON
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
schema = Test::Posts.schema
|
92
|
+
json_payload = '{"foo":"bar"}'
|
93
|
+
hash_payload = { "foo" => "bar" }
|
94
|
+
|
95
|
+
expect(schema[:payload][hash_payload]).to eq(json_payload)
|
96
|
+
expect(schema[:payload].meta[:read][json_payload]).to eq(hash_payload)
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'allows JSON/Hash read/write coersion using symbols as keys', aggregate_failures: true do
|
100
|
+
class Test::Posts < ROM::Relation[:memory]
|
101
|
+
schema do
|
102
|
+
attribute :payload, Types::Coercible::JSON(symbol_keys: true)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
schema = Test::Posts.schema
|
107
|
+
json_payload = '{"foo":"bar"}'
|
108
|
+
hash_payload = { foo: "bar" }
|
109
|
+
|
110
|
+
expect(schema[:payload][hash_payload]).to eq(json_payload)
|
111
|
+
expect(schema[:payload].meta[:read][json_payload]).to eq(hash_payload)
|
112
|
+
end
|
113
|
+
|
114
|
+
it 'allows JSON to Hash coersion only' do
|
115
|
+
class Test::Posts < ROM::Relation[:memory]
|
116
|
+
schema do
|
117
|
+
attribute :payload, Types::Coercible::JSONHash
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
schema = Test::Posts.schema
|
122
|
+
json_payload = '{"foo":"bar"}'
|
123
|
+
hash_payload = { "foo" => "bar" }
|
124
|
+
|
125
|
+
expect(schema[:payload][json_payload]).to eq(hash_payload)
|
126
|
+
end
|
127
|
+
|
128
|
+
it 'returns original payload in JSON to Hash coersion when json is invalid' do
|
129
|
+
class Test::Posts < ROM::Relation[:memory]
|
130
|
+
schema do
|
131
|
+
attribute :payload, Types::Coercible::JSONHash
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
schema = Test::Posts.schema
|
136
|
+
json_payload = 'invalid: json'
|
137
|
+
|
138
|
+
expect(schema[:payload][json_payload]).to eq(json_payload)
|
139
|
+
end
|
140
|
+
|
141
|
+
it 'allows JSON to Hash coersion only using symbols as keys' do
|
142
|
+
class Test::Posts < ROM::Relation[:memory]
|
143
|
+
schema do
|
144
|
+
attribute :payload, Types::Coercible::JSONHash(symbol_keys: true)
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
schema = Test::Posts.schema
|
149
|
+
json_payload = '{"foo":"bar"}'
|
150
|
+
hash_payload = { foo: "bar" }
|
151
|
+
|
152
|
+
expect(schema[:payload][json_payload]).to eq(hash_payload)
|
153
|
+
end
|
154
|
+
|
155
|
+
it 'allows Hash to JSON coersion only' do
|
156
|
+
class Test::Posts < ROM::Relation[:memory]
|
157
|
+
schema do
|
158
|
+
attribute :payload, Types::Coercible::HashJSON
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
schema = Test::Posts.schema
|
163
|
+
json_payload = '{"foo":"bar"}'
|
164
|
+
hash_payload = { "foo" => "bar" }
|
165
|
+
|
166
|
+
expect(schema[:payload][hash_payload]).to eq(json_payload)
|
167
|
+
end
|
168
|
+
|
84
169
|
it 'sets register_as and dataset' do
|
85
170
|
class Test::Users < ROM::Relation[:memory]
|
86
171
|
schema(:users) do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rom
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.3.
|
4
|
+
version: 3.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Piotr Solnica
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-10-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: concurrent-ruby
|
@@ -401,7 +401,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
401
401
|
version: '0'
|
402
402
|
requirements: []
|
403
403
|
rubyforge_project:
|
404
|
-
rubygems_version: 2.6.
|
404
|
+
rubygems_version: 2.6.13
|
405
405
|
signing_key:
|
406
406
|
specification_version: 4
|
407
407
|
summary: Ruby Object Mapper
|