mealib 0.1.0
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 +7 -0
- data/.gitignore +10 -0
- data/.rubocop.yml +50 -0
- data/.ruby-version +1 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +20 -0
- data/README.md +11 -0
- data/bin/console +14 -0
- data/bin/rails +13 -0
- data/bin/setup +8 -0
- data/config/initializers/core_ext/string.rb +62 -0
- data/lib/dry/errors.rb +77 -0
- data/lib/dry/rule.rb +69 -0
- data/lib/dry/rules/and.rb +11 -0
- data/lib/dry/rules/between.rb +18 -0
- data/lib/dry/rules/binary.rb +16 -0
- data/lib/dry/rules/collection.rb +16 -0
- data/lib/dry/rules/composite.rb +19 -0
- data/lib/dry/rules/equal.rb +18 -0
- data/lib/dry/rules/format.rb +18 -0
- data/lib/dry/rules/greater_than.rb +18 -0
- data/lib/dry/rules/greater_than_or_equal.rb +18 -0
- data/lib/dry/rules/included.rb +18 -0
- data/lib/dry/rules/length_between.rb +18 -0
- data/lib/dry/rules/length_equal.rb +18 -0
- data/lib/dry/rules/less_than.rb +18 -0
- data/lib/dry/rules/less_than_or_equal.rb +18 -0
- data/lib/dry/rules/max_length.rb +18 -0
- data/lib/dry/rules/min_length.rb +18 -0
- data/lib/dry/rules/not_equal.rb +18 -0
- data/lib/dry/rules/or.rb +13 -0
- data/lib/dry/rules/present.rb +21 -0
- data/lib/dry/rules/then.rb +13 -0
- data/lib/dry/rules_factory.rb +118 -0
- data/lib/dry/schema.rb +148 -0
- data/lib/mealib.rb +48 -0
- data/lib/mealib/abstract_serializer.rb +28 -0
- data/lib/mealib/dict.rb +26 -0
- data/lib/mealib/engine.rb +5 -0
- data/lib/mealib/fucker/name/first_name_ru_man.txt +64 -0
- data/lib/mealib/fucker/name/last_name_ru_man.txt +252 -0
- data/lib/mealib/fucker/name/name.rb +27 -0
- data/lib/mealib/fucker/name/sur_name_ru_man.txt +53 -0
- data/lib/mealib/geo/distance.rb +22 -0
- data/lib/mealib/token_generator.rb +7 -0
- data/lib/mealib/version.rb +3 -0
- data/mealib.gemspec +26 -0
- metadata +117 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 59fe9fdc843f31b1f435c4810c1712f5d947fbcec6e7fd3c4d8800ce7df9f10e
|
4
|
+
data.tar.gz: 284d947973a7cd84471535d1e54fb52cbb498bfe9279e04c1082d0c278de84ca
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7223f57b7ae359591689621f082a009b44b1d9899ee365ffd0ede6858e3ae33ad7d4cd63c22ca58832462c2d158040f1c6fe8ac8a479a9229db76781e204b0de
|
7
|
+
data.tar.gz: 9dbebed80e16a562ee93f0346f833287f2023b342584bf53abbecbde3bb54480cf46cd46de6fa35ebf3f6e8d11ce0099133525a533fb66ff6e376a1eab6429ac
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
Style/AsciiComments:
|
2
|
+
Description: 'Use only ascii symbols in comments.'
|
3
|
+
StyleGuide: '#english-comments'
|
4
|
+
Enabled: false
|
5
|
+
Style/FrozenStringLiteralComment:
|
6
|
+
Enabled: false
|
7
|
+
Style/CommentedKeyword:
|
8
|
+
Enabled: false
|
9
|
+
Style/SymbolArray:
|
10
|
+
Enabled: false
|
11
|
+
Metrics/LineLength:
|
12
|
+
Max: 120
|
13
|
+
Enabled: false
|
14
|
+
Metrics/MethodLength:
|
15
|
+
Enabled: false
|
16
|
+
Metrics/ModuleLength:
|
17
|
+
Enabled: false
|
18
|
+
Metrics/ParameterLists:
|
19
|
+
Max: 3
|
20
|
+
CountKeywordArgs: true
|
21
|
+
Layout/LeadingCommentSpace:
|
22
|
+
Enabled: false
|
23
|
+
Layout/SpaceBeforeComment:
|
24
|
+
Enabled: false
|
25
|
+
Layout/ExtraSpacing:
|
26
|
+
Enabled: false
|
27
|
+
Layout/CaseIndentation:
|
28
|
+
Enabled: false
|
29
|
+
Layout/SpaceAroundOperators:
|
30
|
+
Enabled: false
|
31
|
+
Style/FormatString:
|
32
|
+
Enabled: false
|
33
|
+
Style/FormatStringToken:
|
34
|
+
Enabled: false
|
35
|
+
Layout/AlignParameters:
|
36
|
+
Enabled: false
|
37
|
+
Metrics/BlockNesting:
|
38
|
+
Enabled: false
|
39
|
+
Layout/TrailingWhitespace:
|
40
|
+
Enabled: false
|
41
|
+
Metrics/AbcSize:
|
42
|
+
Enabled: false
|
43
|
+
Style/Documentation:
|
44
|
+
Enabled: false
|
45
|
+
Layout/AlignHash:
|
46
|
+
Enabled: false
|
47
|
+
Metrics/ClassLength:
|
48
|
+
Enabled: false
|
49
|
+
Lint/Syntax:
|
50
|
+
Enabled: false
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.6.6
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/README.md
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "mealib"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/rails
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# This command will automatically be run when you run "rails" with Rails gems
|
3
|
+
# installed from the root of your application.
|
4
|
+
|
5
|
+
ENGINE_ROOT = File.expand_path('../..', __FILE__)
|
6
|
+
ENGINE_PATH = File.expand_path('../../lib/mealib/engine', __FILE__)
|
7
|
+
|
8
|
+
# Set up gems listed in the Gemfile.
|
9
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
|
10
|
+
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
|
11
|
+
|
12
|
+
require 'rails/all'
|
13
|
+
require 'rails/engine/commands'
|
data/bin/setup
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
class String
|
2
|
+
|
3
|
+
def clear_query
|
4
|
+
dup.clear_query!
|
5
|
+
end
|
6
|
+
|
7
|
+
def clear_query!
|
8
|
+
gsub!(/\s+/, ' ')
|
9
|
+
strip!
|
10
|
+
self
|
11
|
+
end
|
12
|
+
|
13
|
+
def nums_only
|
14
|
+
scan(/\d/).join
|
15
|
+
end
|
16
|
+
|
17
|
+
def numeric?
|
18
|
+
true if Float(self) rescue false
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_bool
|
22
|
+
%w[t true yes on 1].include? self
|
23
|
+
end
|
24
|
+
|
25
|
+
# def sanitize(options = {})
|
26
|
+
# ActionController::Base.helpers.sanitize(self, options)
|
27
|
+
# end
|
28
|
+
|
29
|
+
def nl2br
|
30
|
+
dup.nl2br!
|
31
|
+
end
|
32
|
+
|
33
|
+
def nl2br!
|
34
|
+
gsub!(/\r/, '')
|
35
|
+
gsub!(/\n/, '<br>')
|
36
|
+
self
|
37
|
+
end
|
38
|
+
|
39
|
+
def strip_leading_zeros
|
40
|
+
dup.strip_leading_zeros!
|
41
|
+
end
|
42
|
+
|
43
|
+
def strip_leading_zeros!
|
44
|
+
gsub!(/^0+/, '')
|
45
|
+
self
|
46
|
+
end
|
47
|
+
|
48
|
+
def try_to_integer
|
49
|
+
Integer( try_to_big_decimal ) rescue self
|
50
|
+
end
|
51
|
+
|
52
|
+
def try_to_big_decimal
|
53
|
+
gsub!(/\s/, '')
|
54
|
+
begin
|
55
|
+
Float(self)
|
56
|
+
rescue
|
57
|
+
return self
|
58
|
+
end
|
59
|
+
BigDecimal(self)
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
data/lib/dry/errors.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
module Dry
|
2
|
+
class Errors
|
3
|
+
|
4
|
+
attr_reader :messages
|
5
|
+
|
6
|
+
def initialize(messages = {})
|
7
|
+
@messages = messages
|
8
|
+
end
|
9
|
+
|
10
|
+
|
11
|
+
def add(key, message)
|
12
|
+
keys = key.to_s.split('.').map!(&:to_sym)
|
13
|
+
old = messages.dig(*keys[0...-1]) rescue {}
|
14
|
+
old = {} unless old.is_a?(Hash)
|
15
|
+
new = keys[0...-1].inject(messages) { |h, k| h[k] ||= {} rescue h = {}; h[k] = {} }
|
16
|
+
new[keys.last] = [] unless new.is_a?(Array)
|
17
|
+
new[keys.last] << message
|
18
|
+
new.merge!(old)
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
def merge!(error, parent_key = nil)
|
23
|
+
hash_to_dots(error.messages, {}, parent_key).each do |key, messages|
|
24
|
+
messages.each { |message| add(key, message) }
|
25
|
+
end
|
26
|
+
messages
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
def any?
|
31
|
+
messages.any?
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
def has_key?(key)
|
36
|
+
keys = key.to_s.split('.').map!(&:to_sym)
|
37
|
+
keys.size == 1 ? messages[keys.first].present? : (messages.dig(*keys[0...-1])[keys.last].present? rescue false)
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
def first_message
|
42
|
+
fetch_messages(messages.values.first).first
|
43
|
+
end
|
44
|
+
|
45
|
+
|
46
|
+
def clone
|
47
|
+
self.class.new(messages.clone)
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
|
54
|
+
def hash_to_dots(hash, results = {}, start_key = '')
|
55
|
+
hash.each do |key, value|
|
56
|
+
key = key.to_s
|
57
|
+
key_value = start_key.present? ? sprintf('%s.%s', start_key, key) : key
|
58
|
+
if value.is_a?(Hash)
|
59
|
+
results.merge!(hash_to_dots(value, results, key_value))
|
60
|
+
else
|
61
|
+
results[key_value] = value
|
62
|
+
end
|
63
|
+
end
|
64
|
+
results
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
def fetch_messages(value)
|
69
|
+
if value.is_a?(Hash)
|
70
|
+
fetch_messages(value.values.first)
|
71
|
+
else
|
72
|
+
value
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
end
|
data/lib/dry/rule.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
module Dry
|
2
|
+
class Rule
|
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 name
|
14
|
+
args[:name]
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
def add_error
|
19
|
+
errors.add(key, messages[name.to_s] || 'invalid')
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
def clone
|
24
|
+
self.class.new(value, errors.clone, args)
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
def and(right)
|
29
|
+
Dry::Rules::And.new(self, errors, args.merge(right: right))
|
30
|
+
end
|
31
|
+
alias :& :and
|
32
|
+
|
33
|
+
|
34
|
+
def then(right)
|
35
|
+
Dry::Rules::Then.new(self, errors, args.merge(right: right))
|
36
|
+
end
|
37
|
+
alias :> :then
|
38
|
+
|
39
|
+
|
40
|
+
def or(right)
|
41
|
+
Dry::Rules::Or.new(self, errors, args.merge(right: right))
|
42
|
+
end
|
43
|
+
alias :| :or
|
44
|
+
|
45
|
+
|
46
|
+
def +(right)
|
47
|
+
Dry::Rules::Collection.new(self, errors, args.merge(right: right))
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
def valid?
|
52
|
+
raise NotImplementedError
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
|
59
|
+
def messages
|
60
|
+
@messages ||= (args[:messages] || {}).deep_stringify_keys
|
61
|
+
end
|
62
|
+
|
63
|
+
|
64
|
+
def key
|
65
|
+
@key ||= args[:key] || (raise 'Missing required param "key"')
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
end
|