fried-schema 3.0.0 → 4.0.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: 46020d2292e48229329d1db62423cb1e84b2914d
4
- data.tar.gz: d7f87baa4d01f27aaf4178a7d7de13ce9f067f60
3
+ metadata.gz: dc8aae6f9e132e9b279cf743f3a52404e62ca7f1
4
+ data.tar.gz: 4c2fefef74fd3953b3b8fa21e30720d020e361ca
5
5
  SHA512:
6
- metadata.gz: 21026e6c724c72c2093a5865997764effddb846b84c73a159c9c3e9eeb6b71c17be19674ed39dd3673e4673347a38bb843fbe326f9944315ee86f9be221d2f3f
7
- data.tar.gz: 4c7a64f85cfe8ddfce9b182dad4735e70cfcb407f1e53f85edbec7ab08992355f0bf39cba1431a654f30858e628b0e291f8caed9620cbbb94dffddfcf6e8bcf5
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
@@ -1,5 +1,5 @@
1
1
  module Fried
2
2
  module Schema
3
- VERSION = "3.0.0"
3
+ VERSION = "4.0.0"
4
4
  end
5
5
  end
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: 3.0.0
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-01 00:00:00.000000000 Z
11
+ date: 2017-12-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler