revoke 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2cce86b0069c6ea07882712a63d598c1a4460295f7c8d477db04d6e2ef145ea5
4
+ data.tar.gz: 9f93bc55f89db92dd1846a5d1856c8698882eae79d06207ce1a678bfba5d2552
5
+ SHA512:
6
+ metadata.gz: cef794bbb51b1f6d04d62a83336a9b2092f612b56201af68ed34060e9b339b9c7b2c9316585edbe512420a6501457893568f14f6b7335c753c3d603396fd838a
7
+ data.tar.gz: 7ae22723e34651a78e61b44eb14bfa95402adf3c9b267b4216e7600802498f83fdfe4be0304e51289138a8cfba1337876cf73e9d2cd08cc4ac778b2b41a5a3a1
@@ -0,0 +1,33 @@
1
+ c
2
+ @subject.respond_to? :revoke_update_handler, true
3
+ c
4
+ @subject.respond_to?(:revoke_update_handler)
5
+ @subject.send(:revoke_update_handler)
6
+ @subject.revoke_update_handler
7
+ @subject
8
+ c
9
+ @subject._update_callbacks.filter
10
+ @subject._update_callbacks
11
+ @subject.:_update_callbacks
12
+ @subject.public_methods(false)
13
+ @subject.public_methods(false).grep /revoke/
14
+ FakeModel.public_methods(false).grep /revoke/
15
+ FakeModel.public_methods(false)
16
+ FakeModel.public_methods
17
+ FakeModel.__callbacks
18
+ @subject.revoke_udpdate_handler
19
+ @subject
20
+ 2subject
21
+ c
22
+ @subject.respond_to?(:revoke_update_handler)
23
+ @subject
24
+ c
25
+ subject
26
+ c
27
+ subject
28
+ c
29
+ FakeModel.__callbacks[:update].select { |x| x.instance_variable_get("@filter") == :revoke_update_handler }.first
30
+ FakeModel.__callbacks[:update].select { |x| x.instance_variable_get("@filter") == :revoke_update_handler }
31
+ FakeModel.__callbacks[:update]
32
+ FakeModel.__callbacks
33
+ FakeMode.__callbacks
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in revoke.gemspec
6
+ gemspec
@@ -0,0 +1,58 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ revoke (0.1.0)
5
+ activerecord
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (5.2.1)
11
+ activesupport (= 5.2.1)
12
+ activerecord (5.2.1)
13
+ activemodel (= 5.2.1)
14
+ activesupport (= 5.2.1)
15
+ arel (>= 9.0)
16
+ activesupport (5.2.1)
17
+ concurrent-ruby (~> 1.0, >= 1.0.2)
18
+ i18n (>= 0.7, < 2)
19
+ minitest (~> 5.1)
20
+ tzinfo (~> 1.1)
21
+ arel (9.0.0)
22
+ byebug (10.0.2)
23
+ concurrent-ruby (1.0.5)
24
+ diff-lcs (1.3)
25
+ i18n (1.1.1)
26
+ concurrent-ruby (~> 1.0)
27
+ minitest (5.11.3)
28
+ rake (10.5.0)
29
+ rspec (3.8.0)
30
+ rspec-core (~> 3.8.0)
31
+ rspec-expectations (~> 3.8.0)
32
+ rspec-mocks (~> 3.8.0)
33
+ rspec-core (3.8.0)
34
+ rspec-support (~> 3.8.0)
35
+ rspec-expectations (3.8.2)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.8.0)
38
+ rspec-mocks (3.8.0)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.8.0)
41
+ rspec-support (3.8.0)
42
+ thread_safe (0.3.6)
43
+ tzinfo (1.2.5)
44
+ thread_safe (~> 0.1)
45
+
46
+ PLATFORMS
47
+ ruby
48
+
49
+ DEPENDENCIES
50
+ activerecord
51
+ bundler (~> 1.16)
52
+ byebug
53
+ rake (~> 10.0)
54
+ revoke!
55
+ rspec (~> 3.7)
56
+
57
+ BUNDLED WITH
58
+ 1.16.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Abhishek kanojia
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,79 @@
1
+ ![alt text](https://raw.githubusercontent.com/abhishekkanojia/images/master/revoke_gem.png "Revoke Gem")
2
+ # Revoke
3
+ Adding revoke to your application provides you with an ability to prevent create, update and delete of an entity based on some event.
4
+
5
+ #### Usecase
6
+ Your blog application has comments on articles which you want to be updated only within 10 minutes of creation. Here, `Revoke` gem comes handy to handle this scenario with ease.
7
+
8
+ # Requirements
9
+ Revoke currently supports **Rails 5.2.0** and **Ruby >= 2.4.1**.
10
+
11
+
12
+ # Installation
13
+ Add following line to `Gemfile` and `bundle`
14
+
15
+ ```ruby
16
+ gem 'revoke'
17
+ ```
18
+ OR
19
+ ```ruby
20
+ gem install revoke
21
+ ```
22
+ # How to Use ?
23
+
24
+ Revoke gem provides two forms of `revoke` class method which can be called in model to prevent create, update or delete based on condition.
25
+ You can revoke 3 actions:
26
+ ## Event Revoke
27
+ ##### Update
28
+ ---
29
+ ```ruby
30
+ # Revoke update of object after 10 minutes of its creation.
31
+ revoke :update, :after, 10.minutes, :creation
32
+
33
+ # Revoke destroy of object after 10.minutes of its updation.
34
+ revoke :destroy, :after, 10.minutes, :updation
35
+ ```
36
+ #### Destroy
37
+ ---
38
+ ```ruby
39
+ # Revoke destroy of object after 10 minutes of its creation.
40
+ revoke :destroy, :after, 10.minutes, :creation
41
+ ```
42
+ `Default error message: '{class_name} can only be {destroyed|updated} for {time_duration} after #{creation|updation}.`
43
+
44
+ ## Conditional Revoke
45
+ ```ruby
46
+ revoke :update, if: :some_method?
47
+ ```
48
+
49
+ ```ruby
50
+ revoke :create, if: :some_method?
51
+ ```
52
+
53
+ ```ruby
54
+ revoke :destroy, if: :some_method?
55
+ ```
56
+ `Default error message: "Operation not allowed."`
57
+ ## Customizing Error Messages
58
+
59
+ These error message can be overridden with `message` option passed alongwith `revoke` method.
60
+
61
+ ```ruby
62
+ revoke :destroy, :after, 10.minutes, :creation, message: 'Cannot destroy.'
63
+ revoke :update, if: :some_condition?, message: 'Update not allowed.'
64
+ revoke :destroy, if: :some_condition?, message: 'Destroy not allowed.'
65
+ ```
66
+
67
+ ## Development
68
+
69
+ After checking out the repo, run `bin/setup` to install dependencies.
70
+
71
+ 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`, update the rspec 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).
72
+
73
+ ## Contributing
74
+
75
+ Bug reports and pull requests are welcome on GitHub at https://github.com/abhishekkanojia/revoke.
76
+
77
+ ## License
78
+
79
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "revoke"
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__)
@@ -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,8 @@
1
+ en:
2
+ revoke:
3
+ destroy:
4
+ after:
5
+ error: '%{class_name} can only be destroyed for %{time} %{event} %{deciding_event}.'
6
+ update:
7
+ after:
8
+ error: '%{class_name} can only be updated for %{time} %{event} %{deciding_event}.'
@@ -0,0 +1,27 @@
1
+ require "revoke/version"
2
+ require "revoke/constants"
3
+ require "revoke/callback_creator"
4
+ require "revoke/conditional_callback_creator"
5
+ require "revoke/engine" if defined?(Rails)
6
+
7
+ module Revoke
8
+
9
+ include Constants
10
+
11
+ def self.included(klass)
12
+ klass.extend(ClassMethods)
13
+ end
14
+
15
+ module ClassMethods
16
+ def revoke(*args)
17
+ if args.size.eql?(2)
18
+ Revoke::ConditionalCallbackCreator.new(self, args[0], args[1])
19
+ else
20
+ Revoke::CallbackCreator.new(self, args[0], args[1], args[2], args[3], args[4])
21
+ end
22
+ end
23
+
24
+ end
25
+ end
26
+
27
+ ActiveRecord::Base.send :include, Revoke if defined?(Rails)
@@ -0,0 +1,83 @@
1
+ module Revoke
2
+ class CallbackCreator
3
+ def initialize(class_name, callback_name, event, time, deciding_event, options = {})
4
+
5
+ if parameters_valid?(callback_name, event, time, deciding_event)
6
+ error_message = options[:message] if options.present?
7
+
8
+ class_name.class_eval do
9
+
10
+ send("before_#{callback_name}", :"revoke_#{callback_name}_handler")
11
+
12
+ define_method("revoke_#{callback_name}_handler") do
13
+
14
+ difference = (Time.current - self.send(Revoke::Constants::ACTION_MAP[deciding_event])).to_i
15
+
16
+ if difference.positive? && difference.send(Revoke::Constants::EVENT[event], time)
17
+ errors.add(:base, (error_message || I18n.t(:error, scope: [:revoke, callback_name, :after], class_name: self.class.name, time: send("allowed_time_to_#{callback_name}?"), event: event, deciding_event: deciding_event)))
18
+ throw(:abort)
19
+ end
20
+ end
21
+
22
+ define_method("allowed_time_to_#{callback_name}?") do
23
+ parts = ActiveSupport::Duration.build(time.to_i).parts
24
+ ''.tap do |string|
25
+ parts.each_pair do |key, value|
26
+ string.concat("#{value} #{key}")
27
+ end
28
+ string
29
+ end
30
+ end
31
+
32
+ private :"revoke_#{callback_name}_handler"
33
+ end
34
+ end
35
+ end
36
+
37
+ def parameters_valid?(callback_name, event, time, deciding_event)
38
+ callback_valid?(callback_name) &&
39
+ event_valid?(event) &&
40
+ time_valid?(time) &&
41
+ deciding_event_valid?(deciding_event)
42
+ end
43
+
44
+ private def callback_valid?(callback_name)
45
+ if callback_name.in?(Revoke::Constants::ACTION)
46
+ true
47
+ else
48
+ raise_error_message(callback_name, Revoke::Constants::ACTION)
49
+ end
50
+ end
51
+
52
+ private def event_valid?(event)
53
+ allowed_event_keys = Revoke::Constants::EVENT.keys
54
+ if event.in?(allowed_event_keys)
55
+ true
56
+ else
57
+ raise_error_message(event, allowed_event_keys)
58
+ end
59
+ end
60
+
61
+ private def time_valid?(time)
62
+ if time.is_a?(ActiveSupport::Duration)
63
+ true
64
+ else
65
+ raise ArgumentError, 'Enter duration like: 10.seconds'
66
+ end
67
+ end
68
+
69
+ private def deciding_event_valid?(deciding_event)
70
+ allowed_deciding_events = Revoke::Constants::ACTION_MAP.keys
71
+ if deciding_event.in?(allowed_deciding_events)
72
+ true
73
+ else
74
+ raise_error_message(deciding_event, allowed_deciding_events)
75
+ end
76
+ end
77
+
78
+ private def raise_error_message(option, allowed_options)
79
+ raise ArgumentError, "Invalid option supplied: #{option}\n Allowed Options: #{allowed_options.join(', ')}"
80
+ end
81
+
82
+ end
83
+ end
@@ -0,0 +1,48 @@
1
+ module Revoke
2
+ class ConditionalCallbackCreator
3
+
4
+ VALID_CALLBACK_NAME = [:create, :update, :destroy].freeze
5
+ DEFAULT_MESSAGE = 'Operation not allowed'.freeze
6
+
7
+ def initialize(class_name, callback_name, options)
8
+
9
+ unless callback_name.in?(VALID_CALLBACK_NAME)
10
+ raise ArgumentError, 'Invalid callback name, valid options are create, update and destroy.'
11
+ end
12
+
13
+ message = options[:message] || DEFAULT_MESSAGE
14
+
15
+ class_name.class_eval do
16
+
17
+ send("before_#{callback_name}", :"revoke_#{callback_name}_conditional_handler")
18
+
19
+ define_method("revoke_#{callback_name}_conditional_handler") do
20
+ if options[:if].present?
21
+ callable = options[:if]
22
+ elsif options[:unless].present?
23
+ callable = options[:unless]
24
+ else
25
+ raise ArgumentError, 'Invalid argument supplied for revoke.'
26
+ end
27
+
28
+ if callable.is_a?(Proc)
29
+ if callable.call
30
+ errors.add(:base, message)
31
+ throw(:abort)
32
+ end
33
+ end
34
+
35
+ if callable.is_a?(Symbol)
36
+ if send(callable)
37
+ errors.add(:base, message)
38
+ throw(:abort)
39
+ end
40
+ end
41
+ end
42
+
43
+ private :"revoke_#{callback_name}_conditional_handler"
44
+ end
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,13 @@
1
+ module Revoke
2
+ module Constants
3
+ ACTION = %i[update destroy].freeze
4
+ EVENT = {
5
+ after: '>'
6
+ }.freeze
7
+ ACTION_MAP = {
8
+ creation: 'created_at',
9
+ updation: 'updated_at',
10
+ deletion: 'deleted_at'
11
+ }.freeze
12
+ end
13
+ end
@@ -0,0 +1,6 @@
1
+ require 'rails'
2
+
3
+ module Revoke
4
+ class Engine < ::Rails::Engine
5
+ end
6
+ end
@@ -0,0 +1,3 @@
1
+ module Revoke
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,31 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "revoke/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "revoke"
8
+ spec.version = Revoke::VERSION
9
+ spec.authors = ["Abhishek kanojia"]
10
+ spec.email = ["abhishek.kanojia3193@gmail.com"]
11
+
12
+ spec.summary = %q{Add time based restriction to your rails application with Revoke gem.}
13
+ spec.description = %q{Revoke provides easy an easier way to prevent your rail's application model from being updated based on an event.}
14
+ spec.homepage = "https://github.com/abhishekkanojia/revoke.git"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency 'activerecord'
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.16'
27
+ spec.add_development_dependency 'activerecord'
28
+ spec.add_development_dependency 'byebug'
29
+ spec.add_development_dependency 'rspec', '~> 3.7'
30
+ spec.add_development_dependency 'rake', '~> 10.0'
31
+ end
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: revoke
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Abhishek kanojia
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-11-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
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: byebug
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: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.7'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.7'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ description: Revoke provides easy an easier way to prevent your rail's application
98
+ model from being updated based on an event.
99
+ email:
100
+ - abhishek.kanojia3193@gmail.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".byebug_history"
106
+ - ".gitignore"
107
+ - ".rspec"
108
+ - Gemfile
109
+ - Gemfile.lock
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - bin/console
114
+ - bin/setup
115
+ - config/locales/en.yml
116
+ - lib/revoke.rb
117
+ - lib/revoke/callback_creator.rb
118
+ - lib/revoke/conditional_callback_creator.rb
119
+ - lib/revoke/constants.rb
120
+ - lib/revoke/engine.rb
121
+ - lib/revoke/version.rb
122
+ - revoke.gemspec
123
+ homepage: https://github.com/abhishekkanojia/revoke.git
124
+ licenses:
125
+ - MIT
126
+ metadata: {}
127
+ post_install_message:
128
+ rdoc_options: []
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ requirements: []
142
+ rubyforge_project:
143
+ rubygems_version: 2.7.7
144
+ signing_key:
145
+ specification_version: 4
146
+ summary: Add time based restriction to your rails application with Revoke gem.
147
+ test_files: []