c80_shared 0.1.59 → 0.1.60
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/repositories/accounts/admin_account_repository.rb +35 -0
- data/app/repositories/accounts/central_agent_account_repository.rb +46 -0
- data/app/repositories/accounts/client_account_repository.rb +43 -0
- data/app/repositories/accounts/manager_account_repository.rb +35 -0
- data/app/repositories/accounts/manager_guest_account_repository.rb +35 -0
- data/app/repositories/accounts/moderator_account_repository.rb +35 -0
- data/app/schemas/base.rb +67 -0
- data/app/schemas/base_boat_schema.rb +235 -0
- data/app/schemas/central_agent/save_boat_schema.rb +57 -0
- data/app/services/abstract_prices_service.rb +70 -0
- data/app/services/boats/boat_prices_save_service.rb +81 -0
- data/app/services/boats/boat_sale_prices_save_service.rb +60 -0
- data/app/services/boats/dimension_service.rb +44 -0
- data/app/services/central_agent/save_boat_service.rb +111 -0
- data/app/services/lease/create_broadcast_inquiry_service.rb +62 -0
- data/app/services/lease/destroy_inquiry_service.rb +5 -0
- data/config/initializers/core_ext/active_record_log_subscriber.rb +60 -0
- data/config/initializers/core_ext/string.rb +62 -0
- data/lib/c80_shared/dry/errors.rb +77 -0
- data/lib/c80_shared/dry/rule.rb +69 -0
- data/lib/c80_shared/dry/rules/and.rb +11 -0
- data/lib/c80_shared/dry/rules/between.rb +20 -0
- data/lib/c80_shared/dry/rules/binary.rb +18 -0
- data/lib/c80_shared/dry/rules/collection.rb +16 -0
- data/lib/c80_shared/dry/rules/composite.rb +19 -0
- data/lib/c80_shared/dry/rules/equal.rb +18 -0
- data/lib/c80_shared/dry/rules/format.rb +18 -0
- data/lib/c80_shared/dry/rules/greater_than.rb +18 -0
- data/lib/c80_shared/dry/rules/greater_than_or_equal.rb +18 -0
- data/lib/c80_shared/dry/rules/included.rb +18 -0
- data/lib/c80_shared/dry/rules/length_between.rb +18 -0
- data/lib/c80_shared/dry/rules/length_equal.rb +18 -0
- data/lib/c80_shared/dry/rules/less_than.rb +18 -0
- data/lib/c80_shared/dry/rules/less_than_or_equal.rb +18 -0
- data/lib/c80_shared/dry/rules/max_length.rb +18 -0
- data/lib/c80_shared/dry/rules/min_length.rb +18 -0
- data/lib/c80_shared/dry/rules/not_equal.rb +18 -0
- data/lib/c80_shared/dry/rules/or.rb +15 -0
- data/lib/c80_shared/dry/rules/present.rb +21 -0
- data/lib/c80_shared/dry/rules/then.rb +13 -0
- data/lib/c80_shared/dry/rules_factory.rb +118 -0
- data/lib/c80_shared/dry/schema.rb +148 -0
- data/lib/c80_shared/version.rb +1 -1
- data/lib/c80_shared.rb +1 -1
- metadata +44 -3
- data/scratch_105___02.txt +0 -13
@@ -0,0 +1,15 @@
|
|
1
|
+
require_relative 'composite'
|
2
|
+
|
3
|
+
module Dry
|
4
|
+
module Rules
|
5
|
+
class Or < Dry::Rules::Composite
|
6
|
+
|
7
|
+
def valid?
|
8
|
+
return true if lt.valid? || rt.valid?
|
9
|
+
[lt.errors, rt.errors].each { |e| errors.merge!(e) if e.any? }
|
10
|
+
false
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,118 @@
|
|
1
|
+
module Dry
|
2
|
+
class RulesFactory
|
3
|
+
|
4
|
+
attr_reader :value, :errors, :args
|
5
|
+
|
6
|
+
def initialize(value, errors = Dry::Errors.new, **args)
|
7
|
+
@value = value
|
8
|
+
@errors = errors
|
9
|
+
@args = args.symbolize_keys
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
def present?
|
14
|
+
Dry::Rules::Present.new(value, errors, args)
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
def equal?(right)
|
19
|
+
Dry::Rules::Equal.new(value, errors, build_args(right))
|
20
|
+
end
|
21
|
+
alias :eq? :equal?
|
22
|
+
|
23
|
+
|
24
|
+
def not_equal?(right)
|
25
|
+
Dry::Rules::NotEqual.new(value, errors, build_args(right))
|
26
|
+
end
|
27
|
+
alias :not_eq? :not_equal?
|
28
|
+
|
29
|
+
|
30
|
+
def greater_than?(right)
|
31
|
+
Dry::Rules::GreaterThan.new(value, errors, build_args(right))
|
32
|
+
end
|
33
|
+
alias :gt? :greater_than?
|
34
|
+
|
35
|
+
|
36
|
+
def greater_than_or_equal?(right)
|
37
|
+
Dry::Rules::GreaterThanOrEqual.new(value, errors, build_args(right))
|
38
|
+
end
|
39
|
+
alias :gt_eq? :greater_than_or_equal?
|
40
|
+
|
41
|
+
|
42
|
+
def less_than?(right)
|
43
|
+
Dry::Rules::LessThan.new(value, errors, build_args(right))
|
44
|
+
end
|
45
|
+
alias :lt? :less_than?
|
46
|
+
|
47
|
+
|
48
|
+
def less_than_or_equal?(right)
|
49
|
+
Dry::Rules::LessThanOrEqual.new(value, errors, build_args(right))
|
50
|
+
end
|
51
|
+
alias :lt_eq? :less_than_or_equal?
|
52
|
+
|
53
|
+
|
54
|
+
def included?(right)
|
55
|
+
Dry::Rules::Included.new(value, errors, build_args(right))
|
56
|
+
end
|
57
|
+
|
58
|
+
|
59
|
+
def length_equal?(right)
|
60
|
+
Dry::Rules::LengthEqual.new(value, errors, build_args(right))
|
61
|
+
end
|
62
|
+
|
63
|
+
|
64
|
+
def min_length?(right)
|
65
|
+
Dry::Rules::MinLength.new(value, errors, build_args(right))
|
66
|
+
end
|
67
|
+
|
68
|
+
|
69
|
+
def max_length?(right)
|
70
|
+
Dry::Rules::MaxLength.new(value, errors, build_args(right))
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
def between?(right)
|
75
|
+
Dry::Rules::Between.new(value, errors, build_args(right))
|
76
|
+
end
|
77
|
+
|
78
|
+
|
79
|
+
def length_between?(right)
|
80
|
+
Dry::Rules::LengthBetween.new(value, errors, build_args(right))
|
81
|
+
end
|
82
|
+
|
83
|
+
|
84
|
+
def format?(right)
|
85
|
+
Dry::Rules::Format.new(value, errors, build_args(right))
|
86
|
+
end
|
87
|
+
|
88
|
+
|
89
|
+
def each(&block)
|
90
|
+
return if value.blank?
|
91
|
+
value.inject(nil) do |rule, e|
|
92
|
+
_rule = self.class.new(e, errors, args).instance_exec(&block)
|
93
|
+
rule = rule ? rule & _rule : _rule
|
94
|
+
rule
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
|
99
|
+
def each_value(&block)
|
100
|
+
return if value.blank?
|
101
|
+
value.inject(nil) do |rule, (k, v)|
|
102
|
+
_rule = self.class.new(v, errors, args).instance_exec(&block)
|
103
|
+
rule = rule ? rule & _rule : _rule
|
104
|
+
rule
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
|
109
|
+
private
|
110
|
+
|
111
|
+
|
112
|
+
def build_args(right)
|
113
|
+
rt = right.is_a?(self.class) ? right.value : right
|
114
|
+
args.merge(right: rt)
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
118
|
+
end
|
@@ -0,0 +1,148 @@
|
|
1
|
+
module Dry
|
2
|
+
module Schema
|
3
|
+
|
4
|
+
attr_reader :attributes, :errors
|
5
|
+
|
6
|
+
def initialize(params)
|
7
|
+
@errors = Dry::Errors.new
|
8
|
+
@attributes = cast({ wrap: params }.deep_symbolize_keys[:wrap], schema)
|
9
|
+
JSON::Validator.validate!(schema, attributes)
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
def [](key)
|
14
|
+
attributes[key.to_sym]
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
def valid?
|
19
|
+
validate_attributes(attributes, schema)
|
20
|
+
validate_additional
|
21
|
+
!errors.any?
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
def schema
|
26
|
+
raise NotImplemented
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
|
33
|
+
def validate_additional
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
def validate_attributes(props, schema, parents = [], key = nil)
|
38
|
+
case props
|
39
|
+
when Dry::Schema
|
40
|
+
errors.merge!(props.errors, (parents + [key]).compact.join('.')) unless props.valid?
|
41
|
+
else
|
42
|
+
[schema[:rule]].flatten.compact.each do |rule|
|
43
|
+
rule = rule.arity == 0 ? rule(key, parents, &rule) : rule.call(parents)
|
44
|
+
rule.valid? if rule
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
if schema[:properties]
|
49
|
+
(schema[:properties] || {}).each do |_key, data|
|
50
|
+
validate_attributes((props.present? ? props : {})[_key.to_sym], data, (parents + [key]).compact, _key)
|
51
|
+
end
|
52
|
+
elsif schema[:items]
|
53
|
+
(props.present? ? props : []).each_with_index do |e, i|
|
54
|
+
validate_attributes(e, schema[:items], (parents + [key]).compact, i)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
if schema[:oneOf] || schema[:allOf] || schema[:anyOf]
|
59
|
+
[:oneOf, :allOf, :anyOf].each do |k|
|
60
|
+
s = schema[k]
|
61
|
+
case s
|
62
|
+
when Hash
|
63
|
+
validate_attributes(props, s, parents, key)
|
64
|
+
when Array
|
65
|
+
s.each { |_s| validate_attributes(props, _s, parents, key) if _s.is_a?(Hash) }
|
66
|
+
else
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
|
73
|
+
def cast(params, schema)
|
74
|
+
props = schema.is_a?(Hash) && schema[:cast] ? schema[:cast].call(params) : params
|
75
|
+
|
76
|
+
case props
|
77
|
+
when Hash
|
78
|
+
props.each_with_object({}) do |(key, value), result|
|
79
|
+
result[key] = find_schemas_for_attribute(key, schema).inject((value.dup rescue value)) do
|
80
|
+
|_value, _schema| _value = cast(_value, _schema); _value
|
81
|
+
end
|
82
|
+
end
|
83
|
+
when Array
|
84
|
+
props.map { |e| cast(e, (schema[:items] rescue nil)) }
|
85
|
+
else
|
86
|
+
props
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
|
91
|
+
def rule(attrs, parents, messages = {}, name = nil, &block)
|
92
|
+
if block.arity == 0
|
93
|
+
_attrs = [attrs].flatten.compact
|
94
|
+
raise 'Invalid rule' if _attrs.size > 1
|
95
|
+
single_rule(parents + _attrs, messages, &block)
|
96
|
+
else
|
97
|
+
multi_rule(attrs, parents, messages, name, &block)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
|
102
|
+
def single_rule(keys, messages = {}, &block)
|
103
|
+
value = attributes.dig(*keys) rescue nil
|
104
|
+
factory = Dry::RulesFactory.new(value, errors, key: keys.join('.'), messages: messages)
|
105
|
+
factory.instance_exec &block
|
106
|
+
end
|
107
|
+
|
108
|
+
|
109
|
+
def multi_rule(keys, parents, messages = {}, name = nil, &block)
|
110
|
+
_keys = [keys].flatten.compact
|
111
|
+
data = parents.present? ? attributes.dig(*parents) : attributes
|
112
|
+
|
113
|
+
data = _keys.inject({}) do |res, key|
|
114
|
+
res[key.to_sym] = data[key.to_sym] rescue nil; res
|
115
|
+
end
|
116
|
+
|
117
|
+
factories = data.map do |key, value|
|
118
|
+
_key = name || key
|
119
|
+
_key = (parents + [_key]).join('.')
|
120
|
+
Dry::RulesFactory.new(value, errors, key: _key, messages: messages)
|
121
|
+
end
|
122
|
+
|
123
|
+
block.call *factories
|
124
|
+
end
|
125
|
+
|
126
|
+
|
127
|
+
def find_schemas_for_attribute(key, schema, schemas = [])
|
128
|
+
case schema
|
129
|
+
when Hash
|
130
|
+
s = schema[:properties][key] rescue nil
|
131
|
+
schemas << s if s
|
132
|
+
[:allOf, :anyOf, :oneOf].each { |k| find_schemas_for_attribute(key, schema[k], schemas) }
|
133
|
+
when Array
|
134
|
+
schema.each { |s| find_schemas_for_attribute(key, s, schemas) }
|
135
|
+
else
|
136
|
+
end
|
137
|
+
|
138
|
+
|
139
|
+
schemas
|
140
|
+
end
|
141
|
+
|
142
|
+
|
143
|
+
def method_missing(method)
|
144
|
+
attributes[method.to_sym]
|
145
|
+
end
|
146
|
+
|
147
|
+
end
|
148
|
+
end
|
data/lib/c80_shared/version.rb
CHANGED
data/lib/c80_shared.rb
CHANGED
@@ -8,7 +8,7 @@ end.call
|
|
8
8
|
# загружаем все классы из директории app
|
9
9
|
lambda do
|
10
10
|
lib_path = File.expand_path('../../app', __FILE__)
|
11
|
-
dir_names = %w[dicts helpers serializers] # order matters!
|
11
|
+
dir_names = %w[dicts helpers serializers repositories schemas] # order matters!
|
12
12
|
|
13
13
|
dir_names.each do |dir|
|
14
14
|
dir_path = '%s/%s/**/*.rb' % [lib_path, dir]
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: c80_shared
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.60
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- C80609A
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-10-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -71,6 +71,15 @@ files:
|
|
71
71
|
- app/helpers/custom/price_helper.rb
|
72
72
|
- app/helpers/custom/select_helper.rb
|
73
73
|
- app/helpers/site/time_helper.rb
|
74
|
+
- app/repositories/accounts/admin_account_repository.rb
|
75
|
+
- app/repositories/accounts/central_agent_account_repository.rb
|
76
|
+
- app/repositories/accounts/client_account_repository.rb
|
77
|
+
- app/repositories/accounts/manager_account_repository.rb
|
78
|
+
- app/repositories/accounts/manager_guest_account_repository.rb
|
79
|
+
- app/repositories/accounts/moderator_account_repository.rb
|
80
|
+
- app/schemas/base.rb
|
81
|
+
- app/schemas/base_boat_schema.rb
|
82
|
+
- app/schemas/central_agent/save_boat_schema.rb
|
74
83
|
- app/serializers/account_serializer.rb
|
75
84
|
- app/serializers/boat_location_serializer.rb
|
76
85
|
- app/serializers/boat_price_serializer.rb
|
@@ -88,12 +97,21 @@ files:
|
|
88
97
|
- app/serializers/lib/boats/dimensions.rb
|
89
98
|
- app/serializers/lib/boats/rent_price_per_season.rb
|
90
99
|
- app/serializers/lib/boats/sale_price.rb
|
100
|
+
- app/services/abstract_prices_service.rb
|
101
|
+
- app/services/boats/boat_prices_save_service.rb
|
102
|
+
- app/services/boats/boat_sale_prices_save_service.rb
|
103
|
+
- app/services/boats/dimension_service.rb
|
104
|
+
- app/services/central_agent/save_boat_service.rb
|
105
|
+
- app/services/lease/create_broadcast_inquiry_service.rb
|
106
|
+
- app/services/lease/destroy_inquiry_service.rb
|
91
107
|
- app/services/users/find_or_generate_email_token_service.rb
|
92
108
|
- app/services/users/generate_email_token_service.rb
|
93
109
|
- bin/console
|
94
110
|
- bin/rails
|
95
111
|
- bin/setup
|
96
112
|
- c80_shared.gemspec
|
113
|
+
- config/initializers/core_ext/active_record_log_subscriber.rb
|
114
|
+
- config/initializers/core_ext/string.rb
|
97
115
|
- config/locales/boat/en.yml
|
98
116
|
- config/locales/boat/ru.yml
|
99
117
|
- config/locales/dicts/account_type/en.yml
|
@@ -118,12 +136,35 @@ files:
|
|
118
136
|
- lib/c80_shared/aasm/handle_event.rb
|
119
137
|
- lib/c80_shared/abstract_serializer.rb
|
120
138
|
- lib/c80_shared/dict.rb
|
139
|
+
- lib/c80_shared/dry/errors.rb
|
140
|
+
- lib/c80_shared/dry/rule.rb
|
141
|
+
- lib/c80_shared/dry/rules/and.rb
|
142
|
+
- lib/c80_shared/dry/rules/between.rb
|
143
|
+
- lib/c80_shared/dry/rules/binary.rb
|
144
|
+
- lib/c80_shared/dry/rules/collection.rb
|
145
|
+
- lib/c80_shared/dry/rules/composite.rb
|
146
|
+
- lib/c80_shared/dry/rules/equal.rb
|
147
|
+
- lib/c80_shared/dry/rules/format.rb
|
148
|
+
- lib/c80_shared/dry/rules/greater_than.rb
|
149
|
+
- lib/c80_shared/dry/rules/greater_than_or_equal.rb
|
150
|
+
- lib/c80_shared/dry/rules/included.rb
|
151
|
+
- lib/c80_shared/dry/rules/length_between.rb
|
152
|
+
- lib/c80_shared/dry/rules/length_equal.rb
|
153
|
+
- lib/c80_shared/dry/rules/less_than.rb
|
154
|
+
- lib/c80_shared/dry/rules/less_than_or_equal.rb
|
155
|
+
- lib/c80_shared/dry/rules/max_length.rb
|
156
|
+
- lib/c80_shared/dry/rules/min_length.rb
|
157
|
+
- lib/c80_shared/dry/rules/not_equal.rb
|
158
|
+
- lib/c80_shared/dry/rules/or.rb
|
159
|
+
- lib/c80_shared/dry/rules/present.rb
|
160
|
+
- lib/c80_shared/dry/rules/then.rb
|
161
|
+
- lib/c80_shared/dry/rules_factory.rb
|
162
|
+
- lib/c80_shared/dry/schema.rb
|
121
163
|
- lib/c80_shared/engine.rb
|
122
164
|
- lib/c80_shared/geo/distance.rb
|
123
165
|
- lib/c80_shared/middleware/images_proxy.rb
|
124
166
|
- lib/c80_shared/token_generator.rb
|
125
167
|
- lib/c80_shared/version.rb
|
126
|
-
- scratch_105___02.txt
|
127
168
|
homepage: http://google.com
|
128
169
|
licenses:
|
129
170
|
- MIT
|