operationable 0.1.7

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0ade6d51c19fadefd0b41528d0f81f1da2ddbdf6
4
+ data.tar.gz: b890c49175142c5ed3067f407e52117dffbef8e5
5
+ SHA512:
6
+ metadata.gz: 3466d1c15547a9b30dd3e32442f5bf2785c9255edd647681f2b7ce5126e7b7c3ff4ea1b82c4ce73dfe44f06048037b1212f10277dedac359862b20b2b92e3a43
7
+ data.tar.gz: f3ba44086bc63975640ae53b77f26b21bed05d649d7e8bcec466d96fa241e152b1e822fdef7ec3cb0cb430d7ae51adf411d65c1d148bc4b07b260e346fce535c
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.5
@@ -0,0 +1,3 @@
1
+ # 0.1.0
2
+
3
+ - Inited
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem "rails", ">= 4.2.0"
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Kirill Suhodolov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,40 @@
1
+ # Operationable
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/operationable`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'operationable'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install operationable
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/operationable. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "operationable"
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
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+ class OperationJob < ActiveJob::Base
3
+ queue_as do
4
+ arguments.first[:queue]
5
+ end
6
+
7
+ def perform(options, props)
8
+ "Operationable::Runners::#{options[:type].capitalize}".constantize.call(options, props)
9
+ end
10
+
11
+ after_perform do
12
+ ActiveRecord::Base.clear_active_connections!
13
+ GC.start
14
+ end
15
+ end
@@ -0,0 +1,23 @@
1
+ require 'rails'
2
+ require 'active_support/dependencies'
3
+
4
+ require "operationable/version"
5
+
6
+ require 'operationable/builder'
7
+ require 'operationable/callback'
8
+ require 'operationable/operation'
9
+ require 'operationable/serializer'
10
+ require 'operationable/specification'
11
+
12
+ require 'operationable/runners/base'
13
+ require 'operationable/runners/serial'
14
+ require 'operationable/runners/separate'
15
+
16
+ require 'jobs/operation_job'
17
+ require 'operations/create'
18
+ require 'operations/destroy'
19
+ require 'operations/update'
20
+
21
+ module Operationable
22
+
23
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+ module Operationable
3
+ class Builder
4
+ attr_reader :record, :params
5
+
6
+ def initialize(record, params={})
7
+ @record = record
8
+ @params = params
9
+ end
10
+
11
+ def build
12
+ record
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+ module Operationable
3
+ class Callback
4
+ attr_reader :props
5
+
6
+ def initialize(props)
7
+ @props = props
8
+ end
9
+
10
+ def record
11
+ @record ||= props[:name].constantize.find(props[:id])
12
+ end
13
+
14
+ def user
15
+ @user ||= User.find(props[:changed_by_id])
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+ module Operationable
3
+ class Operation
4
+ attr_reader :record, :user, :params, :result
5
+
6
+ def initialize(record, user, params={})
7
+ @record = record
8
+ @user = user
9
+ @params = params
10
+ end
11
+
12
+ def process
13
+ return false unless valid
14
+
15
+ @result = build
16
+
17
+ return false unless persist
18
+
19
+ run
20
+
21
+ record.reload
22
+ end
23
+
24
+ private
25
+
26
+ def valid
27
+ true
28
+ end
29
+
30
+ def persist
31
+ record
32
+ end
33
+
34
+ def build
35
+ "#{class_name}::Builder".constantize.new(record, params).build
36
+ end
37
+
38
+ def run
39
+ "#{class_name}::Runner".constantize.new(record, params, result, user).run
40
+ end
41
+
42
+ def class_name
43
+ self.class.name
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+ module Operationable
3
+ module Runners
4
+ class Base
5
+ attr_reader :callbacks, :record, :user, :params, :result
6
+
7
+ def initialize(record, params, result, user)
8
+ @record = record
9
+ @params = params
10
+ @result = result
11
+ @user = user
12
+ @callbacks = []
13
+
14
+ initialize_callbacks
15
+ end
16
+
17
+ def run
18
+ end
19
+
20
+ private
21
+
22
+ def props
23
+ serializer_instance.serialize
24
+ end
25
+
26
+ def initialize_callbacks
27
+ end
28
+
29
+ def push_to_queue(callback_method_name, queue=nil)
30
+ callbacks << {
31
+ callback_method_name: callback_method_name.to_s,
32
+ queue: queue.to_s
33
+ }
34
+ end
35
+
36
+ def callback_names
37
+ check_callbacks.map { |callback| callback[:callback_method_name] }
38
+ end
39
+
40
+ def check_callbacks
41
+ callbacks.find_all do |callback|
42
+ satisfy = specification&.try("should_#{callback[:callback_method_name]}".to_sym)
43
+ satisfy.nil? || satisfy
44
+ end
45
+ end
46
+
47
+ def serializer_instance
48
+ serializer_class_name.constantize.new(record, user, params, result, activity, action_name)
49
+ end
50
+
51
+ def specification
52
+ @specification ||= specification_class_name.constantize.new(record, params, activity, action_name)
53
+ end
54
+
55
+ def callback_class_name
56
+ "#{operation_class_name}::Callback"
57
+ end
58
+
59
+ def serializer_class_name
60
+ "#{operation_class_name}::Serializer"
61
+ end
62
+
63
+ def specification_class_name
64
+ "#{operation_class_name}::Specification"
65
+ end
66
+
67
+ def operation_class_name
68
+ self.class.name.chomp('::Runner')
69
+ end
70
+
71
+ def activity
72
+ operation_class_name.split('::').last.underscore
73
+ end
74
+
75
+ def action_name
76
+ operation_class_name.split('::')[-2..-1].join(' ').titleize
77
+ end
78
+
79
+ def class_name
80
+ self.class.name
81
+ end
82
+
83
+ def perform_method
84
+ sync? ? :perform_now : :perform_later
85
+ end
86
+
87
+ def sync?
88
+ %w(test development).include? Rails.env
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+ module Operationable
3
+ module Runners
4
+ class Separate < ::Operationable::Runners::Base
5
+ def run
6
+ check_callbacks.each { |callback| process(callback) }
7
+ end
8
+
9
+ def process(callback_method_name:, queue: nil)
10
+ (queue.blank? ? self.class : OperationJob.method(perform_method)).call(options(callback_method_name, queue), props)
11
+ end
12
+
13
+ def options(callback_method_name, queue)
14
+ { type: 'separate',
15
+ callback_class_name: callback_class_name,
16
+ callback_method_name: callback_method_name,
17
+ queue: queue }
18
+ end
19
+
20
+ def self.call(options, props)
21
+ options[:callback_class_name].constantize.new(props).method(options[:callback_method_name]).call
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+ module Operationable
3
+ module Runners
4
+ class Serial < ::Operationable::Runners::Base
5
+ def run
6
+ process
7
+ end
8
+
9
+ def process
10
+ (queue.blank? ? self.class : OperationJob.method(perform_method)).call(options, props)
11
+ end
12
+
13
+ def self.call(options, props)
14
+ instance = options[:callback_class_name].constantize.new(props)
15
+ options[:callback_names].each { |method_name| instance.method(method_name).call }
16
+ end
17
+
18
+ def options
19
+ { type: 'serial',
20
+ callback_class_name: callback_class_name,
21
+ callback_names: callback_names,
22
+ queue: queue }
23
+ end
24
+
25
+ private
26
+
27
+ def queue
28
+ check_callbacks.first[:queue]
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+ module Operationable
3
+ class Serializer
4
+ attr_reader :record, :user, :params, :result, :activity, :action_name
5
+
6
+ RECORD_FIELDS = [].freeze
7
+ USER_FIELDS = %i(id role email name).freeze
8
+
9
+ def initialize(record, user, params, result, activity, action_name)
10
+ @record = record
11
+ @user = user
12
+ @params = params
13
+ @result = result
14
+ @activity = activity
15
+ @action_name = action_name
16
+ end
17
+
18
+ def serialize
19
+ stringify_arguments(extract_props(record, user))
20
+ end
21
+
22
+ private
23
+
24
+ def extract_props(record, user)
25
+ { **extract_from_record(record), **extract_from_user(user), activity: activity, action_name: action_name }
26
+ end
27
+
28
+ def extract_from_record(record)
29
+ extracted_props = record_fields.each_with_object({}) do |field, result|
30
+ result[field] = record.try(field)
31
+ result
32
+ end
33
+
34
+ extracted_props[:name] = record.class.name
35
+
36
+ extracted_props
37
+ end
38
+
39
+ def extract_from_user(user)
40
+ user_fields.each_with_object({}) do |field, result|
41
+ result["changed_by_#{field}".to_sym] = user.try(field)
42
+ result
43
+ end
44
+ end
45
+
46
+ def record_fields
47
+ self.class.const_get('RECORD_FIELDS')
48
+ end
49
+
50
+ def user_fields
51
+ self.class.const_get('USER_FIELDS')
52
+ end
53
+
54
+ def stringify_arguments(params)
55
+ params.compact!
56
+
57
+ params.transform_values! do |value|
58
+ if value.is_a? Array
59
+ value.map { |i| acceptable_argument?(i) ? i : i.to_s }
60
+ elsif value.is_a? Hash
61
+ stringify_arguments(value)
62
+ else
63
+ acceptable_argument?(value) ? value : value.to_s
64
+ end
65
+ end
66
+
67
+ params
68
+ end
69
+
70
+ def acceptable_argument?(arg)
71
+ [Numeric, String, NilClass].any? { |t| arg.is_a? t }
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+ module Operationable
3
+ class Specification
4
+ attr_reader :record, :params, :activity
5
+
6
+ def initialize(record, params={}, activity='', action_name='')
7
+ @record = record
8
+ @params = params
9
+ @activity = activity
10
+ @action_name = action_name
11
+ end
12
+
13
+ def satisfied_by?(list, record)
14
+ (list & record.previous_changes.keys).any?
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module Operationable
2
+ VERSION = "0.1.7"
3
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+ module Operations
3
+ class Create < ::Operationable::Operation
4
+ def persist
5
+ record.save
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+ module Operations
3
+ class Destroy < ::Operationable::Operation
4
+ def persist
5
+ record.destroy
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+ module Operations
3
+ class Update < ::Operationable::Operation
4
+ def persist
5
+ record.save
6
+ end
7
+ end
8
+ end
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'operationable/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "operationable"
8
+ spec.version = Operationable::VERSION
9
+ spec.authors = ["Kirill Suhodolov", "Vitaliy Korbut"]
10
+ spec.email = ["kirillsuhodolov@gmail.com"]
11
+
12
+ spec.summary = "Implementation of command pattern to avoid ActiveRecord callbacks " \
13
+ "and get advantages of async jobs for heavy calculations"
14
+ spec.description = "Implementation of command pattern to avoid ActiveRecord callbacks " \
15
+ "and get advantages of async jobs for heavy calculations"
16
+ spec.homepage = "https://github.com/KirillSuhodolov/operationable"
17
+ spec.license = "MIT"
18
+
19
+ spec.files = `git ls-files`.split("\n")
20
+ spec.test_files = `git ls-files -- spec/*`.split("\n")
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.required_ruby_version = '>= 2.1.0'
24
+
25
+ spec.add_dependency 'activejob', '>= 4.2.0'
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.13"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec", "~> 3.0"
30
+ end
@@ -0,0 +1,11 @@
1
+ require "spec_helper"
2
+
3
+ describe Operationable do
4
+ it "has a version number" do
5
+ expect(Operationable::VERSION).not_to be nil
6
+ end
7
+
8
+ it "does something useful" do
9
+ expect(false).to eq(true)
10
+ end
11
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
2
+ require "operationable"
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: operationable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.7
5
+ platform: ruby
6
+ authors:
7
+ - Kirill Suhodolov
8
+ - Vitaliy Korbut
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2016-12-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activejob
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: 4.2.0
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: 4.2.0
28
+ - !ruby/object:Gem::Dependency
29
+ name: bundler
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '1.13'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '1.13'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rake
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '10.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '10.0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rspec
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '3.0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '3.0'
70
+ description: Implementation of command pattern to avoid ActiveRecord callbacks and
71
+ get advantages of async jobs for heavy calculations
72
+ email:
73
+ - kirillsuhodolov@gmail.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - ".rspec"
80
+ - ".travis.yml"
81
+ - CHANGELOG.md
82
+ - Gemfile
83
+ - LICENSE.txt
84
+ - README.md
85
+ - Rakefile
86
+ - bin/console
87
+ - bin/setup
88
+ - lib/jobs/operation_job.rb
89
+ - lib/operationable.rb
90
+ - lib/operationable/builder.rb
91
+ - lib/operationable/callback.rb
92
+ - lib/operationable/operation.rb
93
+ - lib/operationable/runners/base.rb
94
+ - lib/operationable/runners/separate.rb
95
+ - lib/operationable/runners/serial.rb
96
+ - lib/operationable/serializer.rb
97
+ - lib/operationable/specification.rb
98
+ - lib/operationable/version.rb
99
+ - lib/operations/create.rb
100
+ - lib/operations/destroy.rb
101
+ - lib/operations/update.rb
102
+ - operationable-0.1.0.gem
103
+ - operationable-0.1.1.gem
104
+ - operationable-0.1.2.gem
105
+ - operationable-0.1.3.gem
106
+ - operationable-0.1.4.gem
107
+ - operationable-0.1.5.gem
108
+ - operationable-0.1.6.gem
109
+ - operationable.gemspec
110
+ - spec/operationable_spec.rb
111
+ - spec/spec_helper.rb
112
+ homepage: https://github.com/KirillSuhodolov/operationable
113
+ licenses:
114
+ - MIT
115
+ metadata: {}
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: 2.1.0
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubyforge_project:
132
+ rubygems_version: 2.5.1
133
+ signing_key:
134
+ specification_version: 4
135
+ summary: Implementation of command pattern to avoid ActiveRecord callbacks and get
136
+ advantages of async jobs for heavy calculations
137
+ test_files:
138
+ - spec/operationable_spec.rb
139
+ - spec/spec_helper.rb