parametric 0.2.10 → 0.2.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +4 -0
- data/bench/struct_bench.rb +53 -0
- data/lib/parametric.rb +2 -0
- data/lib/parametric/block_validator.rb +2 -0
- data/lib/parametric/context.rb +2 -0
- data/lib/parametric/default_types.rb +2 -0
- data/lib/parametric/dsl.rb +2 -0
- data/lib/parametric/field.rb +2 -0
- data/lib/parametric/field_dsl.rb +2 -0
- data/lib/parametric/policies.rb +2 -0
- data/lib/parametric/registry.rb +2 -0
- data/lib/parametric/results.rb +2 -0
- data/lib/parametric/schema.rb +2 -0
- data/lib/parametric/struct.rb +26 -20
- data/lib/parametric/version.rb +3 -1
- data/parametric.gemspec +0 -1
- data/spec/policies_spec.rb +1 -1
- data/spec/struct_spec.rb +0 -26
- metadata +4 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 763b926782a534a5421ff8252b8da1e372b12ef7de36c57f8439e58daea178ac
|
4
|
+
data.tar.gz: 7e07efa2345016bd28f947d9d527b0ab698796f11b6fa466059e4dcfd7ceaea3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8b7d4569723c047a599af8da6e6f83ed05ffb976b7557396881d5ee1188ae0db30715a002cafa1d116bdfc3368fde1a1006baa83a5288ecab3ec2e39c08ea040
|
7
|
+
data.tar.gz: 50f419b619fa561a0769b812864bd43c7233a637f81b033d51052eb7e85eebb92872bb9a7fc41cf28092be04a50be813644423f9b9a6f3784255a5c5e84b8a1d
|
data/Gemfile
CHANGED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'benchmark/ips'
|
2
|
+
require 'parametric/struct'
|
3
|
+
|
4
|
+
StructAccount = Struct.new(:id, :email, keyword_init: true)
|
5
|
+
StructFriend = Struct.new(:name, keyword_init: true)
|
6
|
+
StructUser = Struct.new(:name, :age, :friends, :account, keyword_init: true)
|
7
|
+
|
8
|
+
class ParametricAccount
|
9
|
+
include Parametric::Struct
|
10
|
+
schema do
|
11
|
+
field(:id).type(:integer).present
|
12
|
+
field(:email).type(:string)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class ParametricUser
|
17
|
+
include Parametric::Struct
|
18
|
+
schema do
|
19
|
+
field(:name).type(:string).present
|
20
|
+
field(:age).type(:integer).default(42)
|
21
|
+
field(:friends).type(:array).schema do
|
22
|
+
field(:name).type(:string).present
|
23
|
+
end
|
24
|
+
field(:account).type(:object).schema ParametricAccount
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
Benchmark.ips do |x|
|
29
|
+
x.report("Struct") {
|
30
|
+
StructUser.new(
|
31
|
+
name: 'Ismael',
|
32
|
+
age: 42,
|
33
|
+
friends: [
|
34
|
+
StructFriend.new(name: 'Joe'),
|
35
|
+
StructFriend.new(name: 'Joan'),
|
36
|
+
],
|
37
|
+
account: StructAccount.new(id: 123, email: 'my@account.com')
|
38
|
+
)
|
39
|
+
}
|
40
|
+
x.report("Parametric::Struct") {
|
41
|
+
ParametricUser.new!(
|
42
|
+
name: 'Ismael',
|
43
|
+
age: 42,
|
44
|
+
friends: [
|
45
|
+
{ name: 'Joe' },
|
46
|
+
{ name: 'Joan' }
|
47
|
+
],
|
48
|
+
account: { id: 123, email: 'my@account.com' }
|
49
|
+
)
|
50
|
+
}
|
51
|
+
x.compare!
|
52
|
+
end
|
53
|
+
|
data/lib/parametric.rb
CHANGED
data/lib/parametric/context.rb
CHANGED
data/lib/parametric/dsl.rb
CHANGED
data/lib/parametric/field.rb
CHANGED
data/lib/parametric/field_dsl.rb
CHANGED
data/lib/parametric/policies.rb
CHANGED
data/lib/parametric/registry.rb
CHANGED
data/lib/parametric/results.rb
CHANGED
data/lib/parametric/schema.rb
CHANGED
data/lib/parametric/struct.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'parametric/dsl'
|
2
4
|
|
3
5
|
module Parametric
|
@@ -56,10 +58,24 @@ module Parametric
|
|
56
58
|
|
57
59
|
# this hook is called after schema definition in DSL module
|
58
60
|
def parametric_after_define_schema(schema)
|
59
|
-
schema.fields.
|
60
|
-
|
61
|
-
|
61
|
+
schema.fields.values.each do |field|
|
62
|
+
if field.meta_data[:schema]
|
63
|
+
if field.meta_data[:schema].is_a?(Parametric::Schema)
|
64
|
+
klass = Class.new do
|
65
|
+
include Struct
|
66
|
+
end
|
67
|
+
klass.schema = field.meta_data[:schema]
|
68
|
+
self.const_set(__class_name(field.key), klass)
|
69
|
+
klass.parametric_after_define_schema(field.meta_data[:schema])
|
70
|
+
else
|
71
|
+
self.const_set(__class_name(field.key), field.meta_data[:schema])
|
72
|
+
end
|
62
73
|
end
|
74
|
+
self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
75
|
+
def #{field.key}
|
76
|
+
_graph[:#{field.key}]
|
77
|
+
end
|
78
|
+
RUBY
|
63
79
|
end
|
64
80
|
end
|
65
81
|
|
@@ -69,27 +85,11 @@ module Parametric
|
|
69
85
|
end
|
70
86
|
end
|
71
87
|
|
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
|
-
|
80
88
|
def wrap(key, value)
|
81
|
-
field = schema.fields[key]
|
82
|
-
return value unless field
|
83
|
-
|
84
89
|
case value
|
85
90
|
when Hash
|
86
91
|
# find constructor for field
|
87
|
-
cons =
|
88
|
-
if cons.kind_of?(Parametric::Schema)
|
89
|
-
klass = parametric_build_class_for_child(key, cons)
|
90
|
-
klass.parametric_after_define_schema(cons)
|
91
|
-
cons = klass
|
92
|
-
end
|
92
|
+
cons = self.const_get(__class_name(key))
|
93
93
|
cons ? cons.new(value) : value.freeze
|
94
94
|
when Array
|
95
95
|
value.map{|v| wrap(key, v) }.freeze
|
@@ -97,6 +97,12 @@ module Parametric
|
|
97
97
|
value.freeze
|
98
98
|
end
|
99
99
|
end
|
100
|
+
|
101
|
+
PLURAL_END = /s$/.freeze
|
102
|
+
|
103
|
+
def __class_name(key)
|
104
|
+
key.to_s.split('_').map(&:capitalize).join.sub(PLURAL_END, '')
|
105
|
+
end
|
100
106
|
end
|
101
107
|
end
|
102
108
|
end
|
data/lib/parametric/version.rb
CHANGED
data/parametric.gemspec
CHANGED
@@ -17,7 +17,6 @@ Gem::Specification.new do |spec|
|
|
17
17
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
18
|
spec.require_paths = ["lib"]
|
19
19
|
|
20
|
-
spec.add_development_dependency "bundler", "~> 1.5"
|
21
20
|
spec.add_development_dependency "rake"
|
22
21
|
spec.add_development_dependency "rspec", '3.4.0'
|
23
22
|
spec.add_development_dependency "byebug"
|
data/spec/policies_spec.rb
CHANGED
data/spec/struct_spec.rb
CHANGED
@@ -99,32 +99,6 @@ 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
|
-
|
128
102
|
it "wraps regular schemas in structs" do
|
129
103
|
friend_schema = Parametric::Schema.new do
|
130
104
|
field(:name)
|
metadata
CHANGED
@@ -1,29 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: parametric
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ismael Celis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-04-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: bundler
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - "~>"
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '1.5'
|
20
|
-
type: :development
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - "~>"
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '1.5'
|
27
13
|
- !ruby/object:Gem::Dependency
|
28
14
|
name: rake
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|
@@ -81,6 +67,7 @@ files:
|
|
81
67
|
- LICENSE.txt
|
82
68
|
- README.md
|
83
69
|
- Rakefile
|
70
|
+
- bench/struct_bench.rb
|
84
71
|
- bin/console
|
85
72
|
- lib/parametric.rb
|
86
73
|
- lib/parametric/block_validator.rb
|
@@ -125,8 +112,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
125
112
|
- !ruby/object:Gem::Version
|
126
113
|
version: '0'
|
127
114
|
requirements: []
|
128
|
-
|
129
|
-
rubygems_version: 2.7.7
|
115
|
+
rubygems_version: 3.1.2
|
130
116
|
signing_key:
|
131
117
|
specification_version: 4
|
132
118
|
summary: DSL for declaring allowed parameters with options, regexp patern and default
|