parametric 0.2.4 → 0.2.5

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
  SHA256:
3
- metadata.gz: 311f5350815420ada31b971cbb09b920cdbadcd7c67f988dde5f6d23f9ddeaa5
4
- data.tar.gz: 7e07b16bcbca0f77f6258754d7f735e4c588a81de5cef6fb418bcd34a9cf6883
3
+ metadata.gz: ab73665f1c6b574e0710689244bf7aad39d37fbd2e6465c8f2185f9bd9421090
4
+ data.tar.gz: 829999fb2f7ef34d136b24fc3fd7f7260785bca8cb8fc6dabcae65e3c1f1e14a
5
5
  SHA512:
6
- metadata.gz: b83121daea8fb5665b7aee7e6ebcbe87409dee8756d5a62b9a63ab8becb03aeed7abb3202f52f8e14dfc3fe648d5fde97f80e1a51843524e16c828dffa1c442c
7
- data.tar.gz: fa56771695aabacd98928f8f2e35774e2ddeef2d6655aea3c509fe8d5c8eab7952c44cb388d5e6a1945be5313869b21ef7f2c5854c87affe274c86ea604b39c6
6
+ metadata.gz: 428e567cf0a782f7a424280b79ea10320c7326a95011d4591350b3d96024c67f5e07616fd62103b23f7a98b9e56e970275c668bb285f8192ac7a05950bfd027c
7
+ data.tar.gz: 9fd12296245cdcee7a29a7882ae8da866d64ffde1fa065c25136a4a661b30cf97f351bda588b4058b7707c9b2e7688bd0e2b356f2c3438daf8f27f3ecf87a16e
@@ -57,10 +57,10 @@ module Parametric
57
57
  return current_schema unless new_schema
58
58
 
59
59
  @schemas[key] = current_schema ? current_schema.merge(new_schema) : new_schema
60
- after_define_schema(@schemas[key])
60
+ parametric_after_define_schema(@schemas[key])
61
61
  end
62
62
 
63
- def after_define_schema(sc)
63
+ def parametric_after_define_schema(sc)
64
64
  # noop hook
65
65
  end
66
66
  end
@@ -55,7 +55,7 @@ module Parametric
55
55
  end
56
56
 
57
57
  # this hook is called after schema definition in DSL module
58
- def after_define_schema(schema)
58
+ def parametric_after_define_schema(schema)
59
59
  schema.fields.keys.each do |key|
60
60
  define_method key do
61
61
  _graph[key]
@@ -69,6 +69,14 @@ module Parametric
69
69
  end
70
70
  end
71
71
 
72
+ def parametric_build_class_for_child(key, child_schema)
73
+ klass = Class.new do
74
+ include Struct
75
+ end
76
+ klass.schema = child_schema
77
+ klass
78
+ end
79
+
72
80
  def wrap(key, value)
73
81
  field = schema.fields[key]
74
82
  return value unless field
@@ -78,11 +86,8 @@ module Parametric
78
86
  # find constructor for field
79
87
  cons = field.meta_data[:schema]
80
88
  if cons.kind_of?(Parametric::Schema)
81
- klass = Class.new do
82
- include Struct
83
- end
84
- klass.schema = cons
85
- klass.after_define_schema(cons)
89
+ klass = parametric_build_class_for_child(key, cons)
90
+ klass.parametric_after_define_schema(cons)
86
91
  cons = klass
87
92
  end
88
93
  cons ? cons.new(value) : value.freeze
@@ -1,3 +1,3 @@
1
1
  module Parametric
2
- VERSION = "0.2.4"
2
+ VERSION = "0.2.5"
3
3
  end
@@ -99,6 +99,32 @@ describe Parametric::Struct do
99
99
  expect(instance.friends.first.age).to eq 10
100
100
  end
101
101
 
102
+ it 'wraps nested schemas in custom class' do
103
+ klass = Class.new do
104
+ include Parametric::Struct
105
+
106
+ def self.parametric_build_class_for_child(key, child_schema)
107
+ Class.new do
108
+ include Parametric::Struct
109
+ schema child_schema
110
+ def salutation
111
+ "my age is #{age}"
112
+ end
113
+ end
114
+ end
115
+
116
+ schema do
117
+ field(:name).type(:string).present
118
+ field(:friends).type(:array).schema do
119
+ field(:age).type(:integer)
120
+ end
121
+ end
122
+ end
123
+
124
+ user = klass.new(name: 'Ismael', friends: [{age: 43}])
125
+ expect(user.friends.first.salutation).to eq 'my age is 43'
126
+ end
127
+
102
128
  it "wraps regular schemas in structs" do
103
129
  friend_schema = Parametric::Schema.new do
104
130
  field(:name)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parametric
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ismael Celis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-19 00:00:00.000000000 Z
11
+ date: 2018-11-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler