Skip to content
This repository was archived by the owner on Mar 13, 2024. It is now read-only.

Commit da85aa4

Browse files
author
Udalov Igor
committed
[UCMS-7655] Add EntitiesLogDump, FlexibleBoolean, UrlFormatter, CiFormatter; other
1 parent c79d5ed commit da85aa4

17 files changed

+615
-28
lines changed

Gemfile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ source 'https://rubygems.org'
55
# Specify your gem's dependencies in unity-utils.gemspec
66
gemspec
77

8-
gem 'pry', '~> 3.0'
8+
gem 'pry', '>= 0.14.2'
99

1010
gem 'rspec', '~> 3.0'
1111

1212
gem 'rubocop', '~> 1.21.0'
13-
14-
gem 'ruby-progressbar', '~> 1.11.0'

Gemfile.lock

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,37 @@
11
PATH
22
remote: .
33
specs:
4-
unity-utils (0.1.0)
5-
ruby-progressbar
4+
unity-utils (0.1.2)
5+
activesupport (>= 4.2)
6+
ruby-progressbar (>= 1.11.0)
67

78
GEM
89
remote: https://rubygems.org/
910
specs:
11+
activesupport (7.0.7.2)
12+
concurrent-ruby (~> 1.0, >= 1.0.2)
13+
i18n (>= 1.6, < 2)
14+
minitest (>= 5.1)
15+
tzinfo (~> 2.0)
1016
ast (2.4.2)
1117
coderay (1.1.3)
18+
concurrent-ruby (1.2.2)
1219
diff-lcs (1.4.4)
20+
i18n (1.14.1)
21+
concurrent-ruby (~> 1.0)
1322
method_source (1.0.0)
14-
parallel (1.20.1)
15-
parser (3.0.2.0)
23+
minitest (5.19.0)
24+
parallel (1.23.0)
25+
parser (3.2.2.3)
1626
ast (~> 2.4.1)
17-
pry (0.14.0)
27+
racc
28+
pry (0.14.2)
1829
coderay (~> 1.1)
1930
method_source (~> 1.0)
20-
rainbow (3.0.0)
21-
regexp_parser (2.1.1)
22-
rexml (3.2.5)
31+
racc (1.7.1)
32+
rainbow (3.1.1)
33+
regexp_parser (2.8.1)
34+
rexml (3.2.6)
2335
rspec (3.10.0)
2436
rspec-core (~> 3.10.0)
2537
rspec-expectations (~> 3.10.0)
@@ -33,7 +45,7 @@ GEM
3345
diff-lcs (>= 1.2.0, < 2.0)
3446
rspec-support (~> 3.10.0)
3547
rspec-support (3.10.2)
36-
rubocop (1.20.0)
48+
rubocop (1.21.0)
3749
parallel (~> 1.10)
3850
parser (>= 3.0.0.0)
3951
rainbow (>= 2.2.2, < 4.0)
@@ -42,20 +54,22 @@ GEM
4254
rubocop-ast (>= 1.9.1, < 2.0)
4355
ruby-progressbar (~> 1.7)
4456
unicode-display_width (>= 1.4.0, < 3.0)
45-
rubocop-ast (1.11.0)
46-
parser (>= 3.0.1.1)
57+
rubocop-ast (1.29.0)
58+
parser (>= 3.2.1.0)
4759
ruby-progressbar (1.11.0)
48-
unicode-display_width (2.0.0)
60+
tzinfo (2.0.6)
61+
concurrent-ruby (~> 1.0)
62+
unicode-display_width (2.4.2)
4963

5064
PLATFORMS
5165
x86_64-darwin-19
66+
x86_64-darwin-21
5267

5368
DEPENDENCIES
54-
pry
55-
rspec
56-
rubocop
57-
ruby-progressbar
69+
pry (>= 0.14.2)
70+
rspec (~> 3.0)
71+
rubocop (~> 1.21.0)
5872
unity-utils!
5973

6074
BUNDLED WITH
61-
2.2.12
75+
2.4.19

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,24 @@ Gem with utilities used by Unity team
55
# Documentation
66

77
- ### Utils:
8+
- **CiFormatter** - Custom rspec formatter: collapses pending tests and adds some time analytics.
9+
10+
```
11+
rspec --formatter CiFormatter
12+
...bash
13+
Rubric publishing using Muppet::PublishJob
14+
updating rubric
15+
enqueues Muppet::PublishJob | Duration: 0.03981s
16+
...
17+
Groups:
18+
7.20884s ./spec/services/topics_views_count/update_spec.rb (right after midnight)
19+
5.45071s ./spec/commands/ugc/approve_topic_spec.rb (approve logic)
20+
...
21+
Single examples:
22+
5.45071s ./spec/commands/ugc/approve_topic_spec.rb (approve logic / creates a topic)
23+
4.91609s ./spec/commands/topic/update/reviews_of_published_spec.rb (has correct state value / is expected to be need approval)
24+
```
25+
826
- **Retrier** - Restarting passed block.
927
```ruby
1028
Unity::Utils::Retrier.call { 5 / 0 }
@@ -20,6 +38,20 @@ Gem with utilities used by Unity team
2038
pool.run!
2139
```
2240
41+
- **UrlFormatter** - Formats url.
42+
```ruby
43+
formatter = ::Unity::Utils::UrlFormatter.new('blog/post/nykotyn-bez-syharet-hde-on-soderzytsia/')
44+
formatter.trailing_slash(enabled: false).build #=> "blog/post/nykotyn-bez-syharet-hde-on-soderzytsia"
45+
formatter.params({'erid' => 'test'}).build #=> "blog/post/nykotyn-bez-syharet-hde-on-soderzytsia?erid=test"
46+
```
47+
48+
- **UrlValidator** - Validates url.
49+
```ruby
50+
response = UrlValidator.new('ftp://motor.ru/reports/videoparis.htm').call
51+
response.valid? #=> false
52+
response.errors #=> ['Ссылка должна начинаться с http(s)']
53+
```
54+
2355
- ### Modules:
2456
- **CliModeable** - Wrapper over the ruby-progressbar.
2557
```ruby
@@ -35,6 +67,30 @@ Gem with utilities used by Unity team
3567
end
3668
```
3769
70+
- **EntitiesLogDump** - Module for service objects do dump entities.
71+
```ruby
72+
dump_entities(Topic.first, attributes: [:id, :headline]) # => [{ "id" => 1, "headline" => "foo" }, { "id" => 2, "headline" => "bar" }]
73+
dump_entities(Topic.first(2), attributes: [:id, :headline]) #=> { "id" => 1, "headline" => "foo" }
74+
```
75+
Will work both with ApplicationRecord or Mongoid::Document models.
76+
77+
- **FlexibleBoolean** - Concern to make easier work with virtual boolean attributes.
78+
```ruby
79+
class MyKlass
80+
include IsReactable
81+
82+
flexible_boolean :is_reactable
83+
84+
attr_reader :is_reactable
85+
86+
def intiailize(is_reactable)
87+
@is_reactable = is_reactable
88+
end
89+
end
90+
MyKlass.new(true).is_reactable # => true
91+
MyKlass.new(nil).is_reactable # => false
92+
```
93+
3894
- **Loggable** - Wrapper over logger.
3995
```ruby
4096
@log_file = 'some_file.log'

lib/unity/config/locales/ru.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
ru:
3+
unity_utils:
4+
errors:
5+
blank_url: Ссылка не может быть пустой
6+
invalid_url_format: Неправильный формат ссылки
7+
blank_host: Хост не может быть пустым
8+
invalid_protocol: Ссылка должна начинаться с http(s)

lib/unity/modules/cli_modeable.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,35 @@ module Modules
77
module CliModeable
88
private
99

10+
# @return [Boolean]
1011
def cli_mode?
1112
@cli_mode == true
1213
end
1314

15+
# @param amount [Fixnum]
16+
# @param title [String]
17+
# @return [void]
1418
def init_progressbar(amount, title = self.class.to_s)
1519
return unless cli_mode?
1620

1721
@progressbar = ProgressBar.create(title: title, format: '%t, %c/%C,%e: |%B|')
1822
@progressbar.total = amount
1923
end
2024

25+
# Increment value of progressbar
26+
# @return [void]
2127
def incr_progressbar
2228
@progressbar.increment if cli_mode?
2329
end
30+
31+
# @param amount [Fixnum]
32+
# @return [void]
33+
def add_to_progress(amount = 0)
34+
return unless cli_mode?
35+
36+
new_progress = @progressbar.progress + amount
37+
@progressbar.progress = new_progress if new_progress <= @progressbar.total
38+
end
2439
end
2540
end
2641
end
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# frozen_string_literal: true
2+
3+
require 'active_support/concern'
4+
5+
module Unity
6+
module Modules
7+
# You could use this module for service objects
8+
# class YourServiceObject
9+
# include Helpers::EntitiesLogDump
10+
#
11+
# You can get array of logged entities by custom attributes like that:
12+
# def topic_query
13+
# @topic_query ||= Topic.first(2)
14+
# end
15+
#
16+
# def foo
17+
# dump_entities(topic_query, attributes: [:id, :headline])
18+
# end
19+
# Result: [{ "id" => 1, "headline" => "foo" }, { "id" => 2, "headline" => "bar" }]
20+
#
21+
module EntitiesLogDump
22+
extend ::ActiveSupport::Concern
23+
24+
# @param entities [ActiveRecord::Relation,Array<Mongoid::Document>]
25+
# @param [Hash] options
26+
# @option options [Array<Symbol>] :attributes
27+
# @option options [Integer] :batch_size
28+
# @return [Array<Hash>]
29+
def dump_entities(entities, options = {})
30+
attributes = options[:attributes] || all_attributes(entities.first)
31+
batch_size = options[:batch_size] || 100
32+
33+
dump_entities = []
34+
entities.each_slice(batch_size) do |sliced_entities|
35+
sliced_entities.each do |entity|
36+
dump_entities.append(dump_entity(entity, attributes: attributes))
37+
end
38+
end
39+
40+
dump_entities
41+
end
42+
43+
# @param entity [ApplicationRecord,Mongoid::Document]
44+
# @param [Hash] options
45+
# @option options [Array<Symbol>] :attributes
46+
# @return [Hash]
47+
def dump_entity(entity, options = {})
48+
attributes = options[:attributes] || all_attributes(entity)
49+
attributes.map { |a| { a => try_array(entity.send(a)) } }.inject(:merge).deep_stringify_keys
50+
end
51+
52+
# @param entity [ApplicationRecord,Mongoid::Document]
53+
# @return [Array<String,Symbol>]
54+
def all_attributes(entity)
55+
return entity.class.attribute_names if object_is_a?(entity, 'ApplicationRecord')
56+
return entity.fields.keys if object_is_a?(entity, 'Mongoid::Document')
57+
58+
raise ArgumentError, "Can't determine full attributes list for this kind of entity."
59+
end
60+
61+
private
62+
63+
# @item [Object]
64+
# @return
65+
def try_array(item)
66+
return item.map { |i| i.try(:attributes) || i } if item.is_a?(Array)
67+
return item.map(&:attributes) if item.try(:any?) { |obj| object_is_a?(obj, 'ActiveRecord::Base') }
68+
69+
item
70+
end
71+
72+
# @param obj [Object]
73+
# @klass_name [String]
74+
# @return [Boolean]
75+
def object_is_a?(obj, klass_name)
76+
ancestors = obj.class.ancestors.map(&:to_s)
77+
ancestors.include?(klass_name)
78+
end
79+
end
80+
end
81+
end

lib/unity/modules/flexible_boolean.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# frozen_string_literal: true
2+
3+
require 'active_support/concern'
4+
5+
module Unity
6+
module Modules
7+
# Concern to make easier work with virtual boolean attributes.
8+
# Allows always store a boolean value in a pseudo attribute
9+
# (pass string or boolean, attribute will boolean value type always).
10+
#
11+
# Overrides target attribute setter method by adding a
12+
# comparison of the boolean attribute value with the 'true' string.
13+
#
14+
# Usage example:
15+
# flexible_boolean :is_reactable
16+
#
17+
module FlexibleBoolean
18+
extend ::ActiveSupport::Concern
19+
20+
class_methods do
21+
# @param attribute [Symbol, String]
22+
# @return [Symbol] defined method name
23+
def flexible_boolean(attribute)
24+
define_method(:"#{attribute}=") { |v| super(v.to_s == 'true') }
25+
end
26+
end
27+
end
28+
end
29+
end

lib/unity/modules/logable.rb renamed to lib/unity/modules/loggable.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
module Unity
66
module Modules
7-
module Logable
7+
module Loggable
88
attr_reader :log_file, :logger
99

1010
def clean_logfile

lib/unity/utils.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
# frozen_string_literal: true
22

33
# Modules
4-
require_relative 'modules/logable'
54
require_relative 'modules/cli_modeable'
5+
require_relative 'modules/entities_log_dump'
6+
require_relative 'modules/flexible_boolean'
7+
require_relative 'modules/loggable'
68

79
# Utils
10+
require_relative 'utils/ci_formatter'
811
require_relative 'utils/retrier'
912
require_relative 'utils/thread_pool'
13+
require_relative 'utils/url_formatter'
14+
require_relative 'utils/url_validator'
15+
16+
I18n.load_path += Dir[File.join(File.dirname(__FILE__), 'config/locales/*.yml')]
1017

1118
module Unity
1219
module Utils

0 commit comments

Comments
 (0)