fried-schema 2.0.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b3cfb4822db15c6d31a1e5778169260f3211cb46
4
- data.tar.gz: d8bf67c8722815aa065b41199260a02823a7d7ab
3
+ metadata.gz: 46020d2292e48229329d1db62423cb1e84b2914d
4
+ data.tar.gz: d7f87baa4d01f27aaf4178a7d7de13ce9f067f60
5
5
  SHA512:
6
- metadata.gz: aec8560a85fc82ff3e63ee7ea844f33a97e96609c63b5c8d7048f48647fd3e5f4cf19cc1b8250dfe0fb0f8cf58e8cf49dadfa0fe0010af4f6d388390a7ece47f
7
- data.tar.gz: e1bcb3622ebaf51ffeabcfc43d0ab28a972b1ae4c8a683cbc71af20514399e96c8036d52bea0b4c9f130d38e8096e07d81938dae82d31d0f46ed2e16606d3ad7
6
+ metadata.gz: 21026e6c724c72c2093a5865997764effddb846b84c73a159c9c3e9eeb6b71c17be19674ed39dd3673e4673347a38bb843fbe326f9944315ee86f9be221d2f3f
7
+ data.tar.gz: 4c7a64f85cfe8ddfce9b182dad4735e70cfcb407f1e53f85edbec7ab08992355f0bf39cba1431a654f30858e628b0e291f8caed9620cbbb94dffddfcf6e8bcf5
@@ -0,0 +1,78 @@
1
+ require "fried/core"
2
+ require "fried/typings"
3
+ require "fried/schema/definition"
4
+ require "fried/schema/get_attribute"
5
+ require "fried/schema/struct"
6
+
7
+ module Fried::Schema
8
+ # Compares two {Struct} objects, used for sorting and equality
9
+ class Compare
10
+ include ::Fried::Typings
11
+
12
+ attr_accessor :get_attribute
13
+
14
+ def initialize
15
+ self.get_attribute = GetAttribute.new
16
+ end
17
+
18
+ def self.build
19
+ new.tap do |instance|
20
+ instance.get_attribute = GetAttribute.build
21
+ end
22
+ end
23
+
24
+ def self.call(schema, struct, other)
25
+ instance = build
26
+ instance.(schema, struct, other)
27
+ end
28
+
29
+ # @param schema [Definition]
30
+ # @param struct [Struct]
31
+ # @param other [Struct]
32
+ # @return [Integer, nil] like `<=>` sign
33
+ def call(schema, struct, other)
34
+ return nil unless struct.class == other.class
35
+ return nil unless Is[Struct].valid?(struct)
36
+ return nil unless Is[Struct].valid?(other)
37
+
38
+ compare(schema, struct, other)
39
+ end
40
+
41
+ private
42
+
43
+ def compare(schema, struct, other)
44
+ comparison = 0
45
+
46
+ each_values(schema, struct, other) do |struct_value, other_value|
47
+ next unless struct_value.respond_to?(:"<=>")
48
+ next unless other_value.respond_to?(:"<=>")
49
+ comparison = struct_value <=> other_value
50
+ break if comparison != 0
51
+ end
52
+
53
+ comparison
54
+ end
55
+
56
+ def same_schema?(schema, struct, other)
57
+ schema.
58
+ each_attribute.
59
+ all? do |attr|
60
+ struct.respond_to?(attr.reader) && other.respond_to?(attr.reader)
61
+ end
62
+ end
63
+
64
+ def each_values(schema, struct, other, &block)
65
+ schema.
66
+ each_attribute.
67
+ lazy.
68
+ map { |attr| attr_values_pair(attr, struct, other) }.
69
+ each do |struct_value, other_value|
70
+ block.call(struct_value, other_value)
71
+ end
72
+ end
73
+
74
+ def attr_values_pair(attr, struct, other)
75
+ [get_attribute.(struct, attr), get_attribute.(other, attr)]
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,29 @@
1
+ require "fried/core"
2
+ require "fried/schema/get_definition"
3
+ require "fried/schema/definition"
4
+
5
+ module Fried::Schema
6
+ # Creates schema definition if missing
7
+ class CreateDefinitionIfMissing
8
+ attr_accessor :get_definition
9
+
10
+ def self.build
11
+ new.tap do |instance|
12
+ instance.get_definition = GetDefinition.build
13
+ end
14
+ end
15
+
16
+ def self.call(obj)
17
+ instance = build
18
+ instance.(obj)
19
+ end
20
+
21
+ # @param obj [Class] a {Struct} or {DataEntity} class
22
+ # @return [Definition]
23
+ def call(obj)
24
+ schema = get_definition.(obj)
25
+
26
+ schema || obj.instance_variable_set(:@__fried_schema__, Definition.new)
27
+ end
28
+ end
29
+ end
@@ -1,4 +1,5 @@
1
1
  require "fried/core"
2
+ require "fried/schema/get_definition"
2
3
  require "fried/schema/struct"
3
4
  require "fried/schema/attributes_to_hash"
4
5
  require "fried/schema/hash_to_attributes"
@@ -10,8 +11,8 @@ module Fried::Schema
10
11
  module ClassMethods
11
12
  def build(attributes = {})
12
13
  new.tap do |instance|
13
- schema = self.instance_variable_get(:@__fried_schema__)
14
- ::Fried::Schema::HashToAttributes.(schema, attributes, instance)
14
+ schema = GetDefinition.(self)
15
+ HashToAttributes.(schema, attributes, instance)
15
16
  end
16
17
  end
17
18
  end
@@ -19,8 +20,8 @@ module Fried::Schema
19
20
  # @return [Hash{Symbol => Object}] a hash containing the values of all
20
21
  # attribute name => value
21
22
  def to_h
22
- schema = self.class.instance_variable_get(:@__fried_schema__)
23
- ::Fried::Schema::AttributesToHash.(schema, self)
23
+ schema = GetDefinition.(self.class)
24
+ AttributesToHash.(schema, self)
24
25
  end
25
26
 
26
27
  def self.included(klass)
@@ -0,0 +1,21 @@
1
+ require "fried/core"
2
+
3
+ module Fried::Schema
4
+ # Get schema definition from {Struct}
5
+ class GetDefinition
6
+ def self.build
7
+ new
8
+ end
9
+
10
+ def self.call(obj)
11
+ instance = build
12
+ instance.(obj)
13
+ end
14
+
15
+ # @param obj [Class] a {Struct} or {DataEntity} class
16
+ # @return [Definition]
17
+ def call(obj)
18
+ obj.instance_variable_get(:@__fried_schema__)
19
+ end
20
+ end
21
+ end
@@ -2,7 +2,10 @@ require "fried/core"
2
2
  require "fried/typings"
3
3
  require "fried/schema/attribute/definition"
4
4
  require "fried/schema/attribute/define_methods"
5
+ require "fried/schema/compare"
6
+ require "fried/schema/create_definition_if_missing"
5
7
  require "fried/schema/definition"
8
+ require "fried/schema/get_definition"
6
9
  require "fried/schema/set_defaults"
7
10
 
8
11
  module Fried::Schema
@@ -12,8 +15,8 @@ module Fried::Schema
12
15
  module Struct
13
16
  module Initializer
14
17
  def initialize
15
- schema = self.class.instance_variable_get(:@__fried_schema__)
16
- ::Fried::Schema::SetDefaults.(schema, self)
18
+ schema = GetDefinition.(self.class)
19
+ SetDefaults.(schema, self)
17
20
  end
18
21
  end
19
22
 
@@ -30,20 +33,29 @@ module Fried::Schema
30
33
  # object initialization
31
34
  # @return [Symbol] reader method name
32
35
  def attribute(name, type, default: Noop)
33
- @__fried_schema__ ||= ::Fried::Schema::Definition.new
34
- definer = ::Fried::Schema::Attribute::Definition
36
+ schema = CreateDefinitionIfMissing.(self)
37
+ definer = Attribute::Definition
35
38
  attribute_definition = definer.new(name, type, default)
36
- @__fried_schema__.add_attribute(attribute_definition)
37
- ::Fried::Schema::Attribute::DefineMethods.(attribute_definition, self)
39
+ schema.add_attribute(attribute_definition)
40
+ Attribute::DefineMethods.(attribute_definition, self)
38
41
  end
39
42
  end
40
43
 
41
44
  def self.included(klass)
45
+ CreateDefinitionIfMissing.(klass)
46
+
42
47
  klass.instance_eval do
43
48
  include Initializer
44
49
  extend ClassMethods
45
50
  include ::Fried::Typings
51
+ include Comparable
46
52
  end
47
53
  end
54
+
55
+ def <=>(other)
56
+ schema = GetDefinition.(self.class)
57
+
58
+ Compare.(schema, self, other)
59
+ end
48
60
  end
49
61
  end
@@ -1,5 +1,5 @@
1
1
  module Fried
2
2
  module Schema
3
- VERSION = "2.0.0"
3
+ VERSION = "3.0.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fried-schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fire-Dragon-DoL
@@ -160,9 +160,12 @@ files:
160
160
  - lib/fried/schema/attribute/define_writer.rb
161
161
  - lib/fried/schema/attribute/definition.rb
162
162
  - lib/fried/schema/attributes_to_hash.rb
163
+ - lib/fried/schema/compare.rb
164
+ - lib/fried/schema/create_definition_if_missing.rb
163
165
  - lib/fried/schema/data_entity.rb
164
166
  - lib/fried/schema/definition.rb
165
167
  - lib/fried/schema/get_attribute.rb
168
+ - lib/fried/schema/get_definition.rb
166
169
  - lib/fried/schema/hash_to_attributes.rb
167
170
  - lib/fried/schema/set_attribute.rb
168
171
  - lib/fried/schema/set_attribute_without_checks.rb