fried-schema 3.0.0 → 4.0.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 +6 -0
- data/lib/fried/schema/attributes_to_hash.rb +17 -2
- data/lib/fried/schema/version.rb +1 -1
- 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: dc8aae6f9e132e9b279cf743f3a52404e62ca7f1
|
4
|
+
data.tar.gz: 4c2fefef74fd3953b3b8fa21e30720d020e361ca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2b7dc2bc98206df8251ceb5ee009536cc0822fe405d6a557b81f4352c57fbe92d2d440c561024e7d962e423e27f198a3c7d5cfd50d088c012ba327b2475498ff
|
7
|
+
data.tar.gz: 2dc03177a3e812c239f3a3d21360b3d62888f1a5fe5c376fb6fd6da7255c0a01498958a12942795dfe921d6313dac82123ab305a0e8eae62ba3d83a78acf60ea
|
data/README.md
CHANGED
@@ -57,6 +57,9 @@ person.age # => nil
|
|
57
57
|
person.name = 123 # raises TypeError
|
58
58
|
```
|
59
59
|
|
60
|
+
`Struct`s are `Comparable`. Compare them with `<=>` which will just run
|
61
|
+
`<=>` on each attribute and stop if any is different from 0
|
62
|
+
|
60
63
|
#### DataEntity
|
61
64
|
|
62
65
|
Has all the same functionality as `Fried::Schema::Struct`, in addition to
|
@@ -79,6 +82,9 @@ person.age # => 123
|
|
79
82
|
person.to_h # => { name: "John", born_at: 2017-11-24 00:55:50 -0800, age: 123 }
|
80
83
|
```
|
81
84
|
|
85
|
+
`to_h` works with nested DataEntities. So if a field is of a `DataEntity`
|
86
|
+
type, `to_h` will be called on it.
|
87
|
+
|
82
88
|
### Fried::Typings integration
|
83
89
|
|
84
90
|
The gem integrates with [fried-typings][fried-typings-link], you can use
|
@@ -1,9 +1,15 @@
|
|
1
1
|
require "fried/core"
|
2
|
+
require "fried/schema/data_entity"
|
2
3
|
require "fried/schema/get_attribute"
|
4
|
+
require "fried/schema/get_definition"
|
5
|
+
require "fried/typings"
|
3
6
|
|
4
7
|
module Fried::Schema
|
5
|
-
# Converts all attributes into a {Hash} of name => value
|
8
|
+
# Converts all attributes into a {Hash} of name => value. It calls {#to_h} on
|
9
|
+
# each value that is a {DataEntity}
|
6
10
|
class AttributesToHash
|
11
|
+
include ::Fried::Typings
|
12
|
+
|
7
13
|
attr_accessor :get_attribute
|
8
14
|
|
9
15
|
def initialize
|
@@ -28,9 +34,18 @@ module Fried::Schema
|
|
28
34
|
def call(schema, obj)
|
29
35
|
schema.each_attribute.inject({}) do |hash, attribute|
|
30
36
|
value = get_attribute.(obj, attribute)
|
31
|
-
hash[attribute.name] = value
|
37
|
+
hash[attribute.name] = value_to_h(value)
|
32
38
|
hash
|
33
39
|
end
|
34
40
|
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def value_to_h(value)
|
45
|
+
return value unless Is[DataEntity].valid?(value)
|
46
|
+
|
47
|
+
schema = GetDefinition.(value.class)
|
48
|
+
call(schema, value)
|
49
|
+
end
|
35
50
|
end
|
36
51
|
end
|
data/lib/fried/schema/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fried-schema
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 4.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Fire-Dragon-DoL
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-12-
|
11
|
+
date: 2017-12-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|