transactify 0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 48a5b7c36d8e42d76b055b8ce4ed926f42dd3fd3
4
+ data.tar.gz: 8fb5dbef224c54f71a865a5f5e328efae98d9b15
5
+ SHA512:
6
+ metadata.gz: 3b1c76b13baeecca5fbded365411a16e7b89af71cc510001167365f8cd3523e3e32b319dcc73cf2bc423c65eb0aef22dd30bdd39faedb2758bbe2620d747b61e
7
+ data.tar.gz: 62b49a75aa81c8606c9ddf4c3b9084b8ce6e5bd06f6f3dc36ecde76e87ad0c7ebd5168965f942b6ea53719ee2de923ffbf901a4df6571497a40d32a48127017d
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ coverage
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,15 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - "2.0"
5
+ - "2.1"
6
+ - "2.2"
7
+ - ruby-head
8
+
9
+ before_install: "gem install bundler && gem install rspec"
10
+
11
+ cache: bundler
12
+
13
+ script: bundle exec rspec
14
+
15
+ bundler_args: --without development --jobs=3 --retry=3 --path=../vendor/bundle
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in transactify.gemspec
4
+ gemspec
5
+
6
+ gem 'pry'
7
+ gem 'rspec'
8
+ gem 'rspec-core'
9
+ gem 'sqlite3'
10
+ gem 'sequel'
11
+ gem 'activerecord'
12
+ gem 'simplecov', :require => false, :group => :test
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Igor Kasyanchuk
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.
data/README.md ADDED
@@ -0,0 +1,139 @@
1
+ # Transactify
2
+
3
+ [![Code Climate](https://codeclimate.com/github/igorkasyanchuk/transactify/badges/gpa.svg)](https://codeclimate.com/github/igorkasyanchuk/transactify)
4
+ [![Build Status](https://travis-ci.org/igorkasyanchuk/transactify.svg?branch=master)](https://travis-ci.org/igorkasyanchuk/transactify)
5
+
6
+ [![Sample](https://raw.githubusercontent.com/igorkasyanchuk/transactify/master/transactify.png)](https://github.com/igorkasyanchuk/transactify)
7
+
8
+ Transactify gem can run your methods in database transaction. Previously you had to wrap you code in `ActiveRecord::Base.transaction do .. end` but now it can be done in much more simpler way.
9
+
10
+ Sample or usage: https://github.com/igorkasyanchuk/transactify/blob/master/spec/app.rb#L20
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'transactify'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install transactify
27
+
28
+
29
+ **Note: works only for Ruby 2+**
30
+
31
+ ## Usage
32
+
33
+ Let's say you have model `Question` with method `reject!`.
34
+
35
+
36
+ ```ruby
37
+ def reject!(comment_text = nil)
38
+ assignment_log = question_logs.where(action_type: ASSIGNED_STATUS).last
39
+ owner.rejected_questions << self
40
+ owner.update_attribute(average_assigned_questions_count: question_count/total_questions)
41
+ log = question_logs.create(assignee: assignment_log.assigner, assigner: assignment_log.assignee, action_type: REJECTED_STATUS)
42
+ QuestionMailer.update(self, log).deliver_later
43
+ end
44
+ ```
45
+
46
+ Now imagine that `total_questions` returns 0. And you will have an exception (dividing by zero), but one operation (`owner.rejected_questions << self`) will be performed to the DB. This is wrong, and normally you can avoid this by adding Transactions:
47
+
48
+
49
+ ```ruby
50
+ def reject!(comment_text = nil)
51
+ ActiveRecord::Base.transaction do
52
+ assignment_log = question_logs.where(action_type: ASSIGNED_STATUS).last
53
+ owner.rejected_questions << self
54
+ owner.update_attribute(average_assigned_questions_count: question_count/total_questions)
55
+ log = question_logs.create(assignee: assignment_log.assigner, assigner: assignment_log.assignee, action_type: REJECTED_STATUS)
56
+ QuestionMailer.update(self, log).deliver_later
57
+ end
58
+ end
59
+ ```
60
+
61
+ But what if you have many-many such methods? This is where **transactify** gem can help. You can speficy which methods you want to run in transaction. So final code will looks like:
62
+
63
+ ```ruby
64
+ def reject!(comment_text = nil)
65
+ assignment_log = question_logs.where(action_type: ASSIGNED_STATUS).last
66
+ owner.rejected_questions << self
67
+ owner.update_attribute(average_assigned_questions_count: question_count/total_questions)
68
+ log = question_logs.create(assignee: assignment_log.assigner, assigner: assignment_log.assignee, action_type: REJECTED_STATUS)
69
+ QuestionMailer.update(self, log).deliver_later
70
+ end
71
+
72
+ transactify :reject! # With this line you can specify which methods you want to make safe for DB
73
+ ```
74
+
75
+ Main benefit of this gem is that you don't need to edit all your methods and add transaction blocks. So without any existing code modification you can add support for transaction for whole methods.
76
+
77
+
78
+ Gem allows to **transactify** instance and class methods.
79
+
80
+ ### Functionality
81
+
82
+ `include Transactify` - put in your classes, models to add support for transactions
83
+
84
+ `transactify :method_name` - transactify your insatance method.
85
+
86
+ `ctransactify :method_name` - transactify your class method.
87
+
88
+ ## Samples of Usage
89
+
90
+ ```ruby
91
+ class Question < ActiveRecord::Base
92
+ include Transactify
93
+
94
+ transactify :reject!, :save_question
95
+ ctransactify :generate_report
96
+
97
+ def reject!(comment_text = nil)
98
+ assignment_log = question_logs.where(action_type: ASSIGNED_STATUS).last
99
+ owner.rejected_questions << self
100
+ owner.update_attribute(average_assigned_questions_count: question_count/total_questions)
101
+ log = question_logs.create(assignee: assignment_log.assigner, assigner: assignment_log.assignee, action_type: REJECTED_STATUS)
102
+ QuestionMailer.update(self, log).deliver_later
103
+ end
104
+
105
+ def save_question(user, answer, comment_content = nil)
106
+ update_attributes(owner_id: user.id)
107
+ question_logs.create(assignee: user, assigner: user, action_type: SAVE_STATUS, answers: answer)
108
+ save_comment(user, comment_content)
109
+ end
110
+
111
+ def self.generate_report
112
+ report = Report.create_new_report
113
+ report.create_schema
114
+ report.populate_users
115
+ report.populate_questions
116
+ report
117
+ end
118
+
119
+ end
120
+ ```
121
+
122
+ ## Plans
123
+
124
+ * Add support for Sequel gem
125
+ * Add more specs
126
+
127
+ ## Development
128
+
129
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
130
+
131
+ 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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
132
+
133
+ ## Contributing
134
+
135
+ 1. Fork it ( https://github.com/[my-github-username]/transactify/fork )
136
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
137
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
138
+ 4. Push to the branch (`git push origin my-new-feature`)
139
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "transactify"
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
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,48 @@
1
+ require 'securerandom'
2
+ require 'transactify/version'
3
+
4
+ module Transactify
5
+
6
+ def self.included(base_klass)
7
+ base_klass.extend(ClassMethods)
8
+ interceptor = const_set("#{base_klass.name.demodulize}Interceptor", Module.new)
9
+ base_klass.send(:prepend, interceptor)
10
+ end
11
+
12
+ module ClassMethods
13
+
14
+ def ctransactify(*cmethods)
15
+ interceptor = const_get("#{name.demodulize}Interceptor")
16
+ klass = const_get(name)
17
+ helper = const_set("Transactify#{SecureRandom.hex}Helper", Module.new)
18
+ cmethods.each do |method_name|
19
+ interceptor.module_eval do
20
+ helper.send :define_singleton_method, :prepended do |base|
21
+ define_method(method_name) do |*args, &block|
22
+ ActiveRecord::Base.transaction do
23
+ super(*args, &block)
24
+ end
25
+ end
26
+ end
27
+ (class << klass; self; end).module_eval do
28
+ prepend(helper)
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ def transactify(*cmethods)
35
+ interceptor = const_get("#{name.demodulize}Interceptor")
36
+ cmethods.each do |method_name|
37
+ interceptor.module_eval do
38
+ define_method(method_name) do |*args, &block|
39
+ ActiveRecord::Base.transaction do
40
+ super(*args, &block)
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,3 @@
1
+ module Transactify
2
+ VERSION = "0.1"
3
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'transactify/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "transactify"
8
+ spec.version = Transactify::VERSION
9
+ spec.authors = ["Igor Kasyanchuk"]
10
+ spec.email = ["igorkasyanchuk@gmail.com"]
11
+
12
+ spec.summary = %q{Wrap your code in DB transactions.}
13
+ spec.description = %q{Transactify gem can run your methods in database transaction. Previously you had to wrap you code in `ActiveRecord::Base.transaction do .. end` but now it can be done in much more simpler way.}
14
+ spec.homepage = "https://github.com/igorkasyanchuk/transactify"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.required_ruby_version = '>= 2.0'
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.9"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "activerecord", "> 0"
27
+ spec.add_development_dependency "rspec", "> 0"
28
+ spec.add_development_dependency "pry", "> 0"
29
+ spec.add_development_dependency "simplecov", "> 0"
30
+ spec.add_development_dependency 'sqlite3', "> 0"
31
+ end
data/transactify.png ADDED
Binary file
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: transactify
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Igor Kasyanchuk
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-11 00:00:00.000000000 Z
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.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activerecord
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">"
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">"
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">"
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">"
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">"
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">"
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">"
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">"
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: sqlite3
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">"
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">"
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Transactify gem can run your methods in database transaction. Previously
112
+ you had to wrap you code in `ActiveRecord::Base.transaction do .. end` but now it
113
+ can be done in much more simpler way.
114
+ email:
115
+ - igorkasyanchuk@gmail.com
116
+ executables: []
117
+ extensions: []
118
+ extra_rdoc_files: []
119
+ files:
120
+ - ".gitignore"
121
+ - ".rspec"
122
+ - ".travis.yml"
123
+ - CODE_OF_CONDUCT.md
124
+ - Gemfile
125
+ - LICENSE.txt
126
+ - README.md
127
+ - Rakefile
128
+ - bin/console
129
+ - bin/setup
130
+ - lib/transactify.rb
131
+ - lib/transactify/version.rb
132
+ - transactify.gemspec
133
+ - transactify.png
134
+ homepage: https://github.com/igorkasyanchuk/transactify
135
+ licenses:
136
+ - MIT
137
+ metadata: {}
138
+ post_install_message:
139
+ rdoc_options: []
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '2.0'
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ requirements: []
153
+ rubyforge_project:
154
+ rubygems_version: 2.4.6
155
+ signing_key:
156
+ specification_version: 4
157
+ summary: Wrap your code in DB transactions.
158
+ test_files: []