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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8f629915e6b2fee5ae5fcb9c63e340cd19880a72a3b53e06e33d3daff39ffa6a
4
- data.tar.gz: c3fd0ec0a51e954fccd021c62098192b52913d44e1f0c35299cc77c56e428f57
3
+ metadata.gz: 763b926782a534a5421ff8252b8da1e372b12ef7de36c57f8439e58daea178ac
4
+ data.tar.gz: 7e07efa2345016bd28f947d9d527b0ab698796f11b6fa466059e4dcfd7ceaea3
5
5
  SHA512:
6
- metadata.gz: 9b46933db3b2bddd50ea66ec5e03faeb637248382e9a6fab81b2c539e201f148dc88ddc49e77f467974813a0c30a9c5196d48c3eef69f324ac47b15f39cd3d9e
7
- data.tar.gz: 14ce2da62e976fa4c234d25087cfa9abeff2b4d4a077dbeab2a85654cbcc641f33fadcd9f42c76f02cf21321468d971e754e9ed4ff76ee0c314c1382d0201770
6
+ metadata.gz: 8b7d4569723c047a599af8da6e6f83ed05ffb976b7557396881d5ee1188ae0db30715a002cafa1d116bdfc3368fde1a1006baa83a5288ecab3ec2e39c08ea040
7
+ data.tar.gz: 50f419b619fa561a0769b812864bd43c7233a637f81b033d51052eb7e85eebb92872bb9a7fc41cf28092be04a50be813644423f9b9a6f3784255a5c5e84b8a1d
data/Gemfile CHANGED
@@ -2,3 +2,7 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in parametric.gemspec
4
4
  gemspec
5
+
6
+ group :development do
7
+ gem 'benchmark-ips'
8
+ end
@@ -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
+
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "parametric/version"
2
4
  require "parametric/registry"
3
5
  require "parametric/field"
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Parametric
2
4
  class BlockValidator
3
5
  def self.build(meth, &block)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Parametric
2
4
  class Top
3
5
  attr_reader :errors
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "date"
2
4
 
3
5
  module Parametric
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "parametric"
2
4
 
3
5
  module Parametric
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "parametric/field_dsl"
2
4
 
3
5
  module Parametric
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Parametric
2
4
  # Field DSL
3
5
  # host instance must implement:
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Parametric
2
4
  module Policies
3
5
  class Format
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'parametric/block_validator'
2
4
 
3
5
  module Parametric
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Parametric
2
4
  class Results
3
5
  attr_reader :output, :errors
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "parametric/context"
2
4
  require "parametric/results"
3
5
  require "parametric/field"
@@ -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.keys.each do |key|
60
- define_method key do
61
- _graph[key]
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 = field.meta_data[:schema]
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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Parametric
2
- VERSION = "0.2.10"
4
+ VERSION = "0.2.11"
3
5
  end
@@ -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"
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  describe 'default coercions' do
4
4
  def test_coercion(key, value, expected)
5
- coercion = Parametric.registry.coercions[key]
5
+ coercion = Parametric.registry.policies[key]
6
6
  expect(coercion.new.coerce(value, nil, nil)).to eq expected
7
7
  end
8
8
 
@@ -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.10
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: 2019-09-26 00:00:00.000000000 Z
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
- rubyforge_project:
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