garrison 0.0.1

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.
Files changed (74) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +8 -0
  3. data/Gemfile +14 -0
  4. data/Gemfile.lock +141 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.rdoc +3 -0
  7. data/Rakefile +21 -0
  8. data/garrison.gemspec +30 -0
  9. data/lib/garrison.rb +34 -0
  10. data/lib/garrison/checker_abstract.rb +22 -0
  11. data/lib/garrison/injectee.rb +19 -0
  12. data/lib/garrison/keeper.rb +34 -0
  13. data/lib/garrison/locking_active_record.rb +20 -0
  14. data/lib/garrison/version.rb +3 -0
  15. data/lib/tasks/garrison_tasks.rake +4 -0
  16. data/spec/dummy/README.rdoc +28 -0
  17. data/spec/dummy/Rakefile +6 -0
  18. data/spec/dummy/app/assets/images/.keep +0 -0
  19. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  20. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  21. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  22. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  23. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  24. data/spec/dummy/app/mailers/.keep +0 -0
  25. data/spec/dummy/app/models/.keep +0 -0
  26. data/spec/dummy/app/models/concerns/.keep +0 -0
  27. data/spec/dummy/app/models/model_a.rb +2 -0
  28. data/spec/dummy/app/models/model_b.rb +2 -0
  29. data/spec/dummy/app/models/model_c.rb +2 -0
  30. data/spec/dummy/app/models/user.rb +2 -0
  31. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  32. data/spec/dummy/bin/bundle +3 -0
  33. data/spec/dummy/bin/rails +4 -0
  34. data/spec/dummy/bin/rake +4 -0
  35. data/spec/dummy/config.ru +4 -0
  36. data/spec/dummy/config/application.rb +38 -0
  37. data/spec/dummy/config/boot.rb +5 -0
  38. data/spec/dummy/config/environment.rb +5 -0
  39. data/spec/dummy/config/environments/development.rb +29 -0
  40. data/spec/dummy/config/environments/production.rb +80 -0
  41. data/spec/dummy/config/environments/test.rb +36 -0
  42. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  43. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  44. data/spec/dummy/config/initializers/inflections.rb +16 -0
  45. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  46. data/spec/dummy/config/initializers/secret_token.rb +12 -0
  47. data/spec/dummy/config/initializers/session_store.rb +3 -0
  48. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  49. data/spec/dummy/config/locales/en.yml +23 -0
  50. data/spec/dummy/config/routes.rb +56 -0
  51. data/spec/dummy/db/migrate/20160612023105_create_model_as.rb +9 -0
  52. data/spec/dummy/db/migrate/20160612023109_create_model_bs.rb +9 -0
  53. data/spec/dummy/db/migrate/20160612025050_create_model_cs.rb +9 -0
  54. data/spec/dummy/db/migrate/20160612033904_create_users.rb +9 -0
  55. data/spec/dummy/db/schema.rb +40 -0
  56. data/spec/dummy/lib/assets/.keep +0 -0
  57. data/spec/dummy/log/.keep +0 -0
  58. data/spec/dummy/public/404.html +58 -0
  59. data/spec/dummy/public/422.html +58 -0
  60. data/spec/dummy/public/500.html +57 -0
  61. data/spec/dummy/public/favicon.ico +0 -0
  62. data/spec/dummy/spec/factories/model_as.rb +5 -0
  63. data/spec/dummy/spec/factories/model_bs.rb +5 -0
  64. data/spec/dummy/spec/factories/model_cs.rb +5 -0
  65. data/spec/dummy/spec/factories/users.rb +5 -0
  66. data/spec/dummy/spec/models/model_a_spec.rb +5 -0
  67. data/spec/dummy/spec/models/model_b_spec.rb +5 -0
  68. data/spec/dummy/spec/models/model_c_spec.rb +5 -0
  69. data/spec/dummy/spec/models/user_spec.rb +5 -0
  70. data/spec/garrison/base_spec.rb +56 -0
  71. data/spec/garrison/keeper_spec.rb +138 -0
  72. data/spec/rails_helper.rb +65 -0
  73. data/spec/spec_helper.rb +92 -0
  74. metadata +241 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: eddf974d2cc75ec708a682c06b259cf213c99c0d
4
+ data.tar.gz: e3f7539e5e0b73e91fac8cadaaa97b9877b22c90
5
+ SHA512:
6
+ metadata.gz: edc9db5f5f080cb37b1328b02e565095fba8c72cab33e10576bbab3fa7a540371eded6ee87c1fe743fafc0a40df44595ba7967942b0e2f5edb3f6bfdf7b0fa22
7
+ data.tar.gz: 72f1a6b0807d9f1e335eee5fef3b707c46ed5c771328fe810e8d94eab85cec9c774cffce7a014903453e442d3f39f8e04cb6aef8da8e998e4117444913db8ee6
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ .bundle/
2
+ log/*.log
3
+ pkg/
4
+ spec/dummy/db/*.sqlite3
5
+ spec/dummy/db/*.sqlite3-journal
6
+ spec/dummy/log/*.log
7
+ spec/dummy/tmp/
8
+ spec/dummy/.sass-cache
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Declare your gem's dependencies in garrison.gemspec.
4
+ # Bundler will treat runtime dependencies like base dependencies, and
5
+ # development dependencies will be added by default to the :development group.
6
+ gemspec
7
+
8
+ # Declare any dependencies that are still in development here instead of in
9
+ # your gemspec. These might include edge Rails or gems from your path or
10
+ # Git. Remember to move these dependencies to your gemspec before releasing
11
+ # your gem to rubygems.org.
12
+
13
+ # To use debugger
14
+ # gem 'debugger'
data/Gemfile.lock ADDED
@@ -0,0 +1,141 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ garrison (0.0.1)
5
+ rails (~> 4.0.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actionmailer (4.0.13)
11
+ actionpack (= 4.0.13)
12
+ mail (~> 2.5, >= 2.5.4)
13
+ actionpack (4.0.13)
14
+ activesupport (= 4.0.13)
15
+ builder (~> 3.1.0)
16
+ erubis (~> 2.7.0)
17
+ rack (~> 1.5.2)
18
+ rack-test (~> 0.6.2)
19
+ activemodel (4.0.13)
20
+ activesupport (= 4.0.13)
21
+ builder (~> 3.1.0)
22
+ activerecord (4.0.13)
23
+ activemodel (= 4.0.13)
24
+ activerecord-deprecated_finders (~> 1.0.2)
25
+ activesupport (= 4.0.13)
26
+ arel (~> 4.0.0)
27
+ activerecord-deprecated_finders (1.0.4)
28
+ activesupport (4.0.13)
29
+ i18n (~> 0.6, >= 0.6.9)
30
+ minitest (~> 4.2)
31
+ multi_json (~> 1.3)
32
+ thread_safe (~> 0.1)
33
+ tzinfo (~> 0.3.37)
34
+ arel (4.0.2)
35
+ builder (3.1.4)
36
+ concurrent-ruby (1.0.2)
37
+ coveralls (0.8.13)
38
+ json (~> 1.8)
39
+ simplecov (~> 0.11.0)
40
+ term-ansicolor (~> 1.3)
41
+ thor (~> 0.19.1)
42
+ tins (~> 1.6.0)
43
+ diff-lcs (1.2.5)
44
+ docile (1.1.5)
45
+ erubis (2.7.0)
46
+ factory_girl (4.7.0)
47
+ activesupport (>= 3.0.0)
48
+ factory_girl_rails (4.7.0)
49
+ factory_girl (~> 4.7.0)
50
+ railties (>= 3.0.0)
51
+ i18n (0.7.0)
52
+ json (1.8.3)
53
+ mail (2.6.4)
54
+ mime-types (>= 1.16, < 4)
55
+ mime-types (3.1)
56
+ mime-types-data (~> 3.2015)
57
+ mime-types-data (3.2016.0521)
58
+ mini_portile2 (2.1.0)
59
+ minitest (4.7.5)
60
+ multi_json (1.12.1)
61
+ nokogiri (1.6.8)
62
+ mini_portile2 (~> 2.1.0)
63
+ pkg-config (~> 1.1.7)
64
+ pkg-config (1.1.7)
65
+ rack (1.5.5)
66
+ rack-test (0.6.3)
67
+ rack (>= 1.0)
68
+ rails (4.0.13)
69
+ actionmailer (= 4.0.13)
70
+ actionpack (= 4.0.13)
71
+ activerecord (= 4.0.13)
72
+ activesupport (= 4.0.13)
73
+ bundler (>= 1.3.0, < 2.0)
74
+ railties (= 4.0.13)
75
+ sprockets-rails (~> 2.0)
76
+ railties (4.0.13)
77
+ actionpack (= 4.0.13)
78
+ activesupport (= 4.0.13)
79
+ rake (>= 0.8.7)
80
+ thor (>= 0.18.1, < 2.0)
81
+ rake (10.5.0)
82
+ rspec (3.4.0)
83
+ rspec-core (~> 3.4.0)
84
+ rspec-expectations (~> 3.4.0)
85
+ rspec-mocks (~> 3.4.0)
86
+ rspec-core (3.4.4)
87
+ rspec-support (~> 3.4.0)
88
+ rspec-expectations (3.4.0)
89
+ diff-lcs (>= 1.2.0, < 2.0)
90
+ rspec-support (~> 3.4.0)
91
+ rspec-html-matchers (0.8.0)
92
+ nokogiri (~> 1)
93
+ rspec (>= 3.0.0.a, < 4)
94
+ rspec-mocks (3.4.1)
95
+ diff-lcs (>= 1.2.0, < 2.0)
96
+ rspec-support (~> 3.4.0)
97
+ rspec-rails (3.4.2)
98
+ actionpack (>= 3.0, < 4.3)
99
+ activesupport (>= 3.0, < 4.3)
100
+ railties (>= 3.0, < 4.3)
101
+ rspec-core (~> 3.4.0)
102
+ rspec-expectations (~> 3.4.0)
103
+ rspec-mocks (~> 3.4.0)
104
+ rspec-support (~> 3.4.0)
105
+ rspec-support (3.4.1)
106
+ simplecov (0.11.2)
107
+ docile (~> 1.1.0)
108
+ json (~> 1.8)
109
+ simplecov-html (~> 0.10.0)
110
+ simplecov-html (0.10.0)
111
+ sprockets (3.6.0)
112
+ concurrent-ruby (~> 1.0)
113
+ rack (> 1, < 3)
114
+ sprockets-rails (2.3.3)
115
+ actionpack (>= 3.0)
116
+ activesupport (>= 3.0)
117
+ sprockets (>= 2.8, < 4.0)
118
+ sqlite3 (1.3.11)
119
+ term-ansicolor (1.3.2)
120
+ tins (~> 1.0)
121
+ thor (0.19.1)
122
+ thread_safe (0.3.5)
123
+ tins (1.6.0)
124
+ tzinfo (0.3.49)
125
+
126
+ PLATFORMS
127
+ ruby
128
+
129
+ DEPENDENCIES
130
+ bundler (~> 1.10)
131
+ coveralls
132
+ factory_girl_rails
133
+ garrison!
134
+ rake (~> 10.0)
135
+ rspec
136
+ rspec-html-matchers
137
+ rspec-rails
138
+ sqlite3
139
+
140
+ BUNDLED WITH
141
+ 1.11.2
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2016 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = Garrison
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Garrison'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+ Bundler::GemHelper.install_tasks
21
+
data/garrison.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+
3
+ # Maintain your gem's version:
4
+ require "garrison/version"
5
+
6
+ # Describe your gem and declare its dependencies:
7
+ Gem::Specification.new do |s|
8
+ s.name = "garrison"
9
+ s.version = Garrison::VERSION
10
+ s.authors = ["mmmpa"]
11
+ s.email = ["mmmpa.mmmpa@gmail.com"]
12
+ s.homepage = "http://mmmpa.net"
13
+ s.summary = "Authorization"
14
+ s.description = "Authorization"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ s.add_dependency "rails", "~> 4.0.0"
21
+
22
+ s.add_development_dependency "bundler", "~> 1.10"
23
+ s.add_development_dependency "rake", "~> 10.0"
24
+ s.add_development_dependency "sqlite3"
25
+ s.add_development_dependency "rspec"
26
+ s.add_development_dependency "rspec-rails"
27
+ s.add_development_dependency "rspec-html-matchers"
28
+ s.add_development_dependency "factory_girl_rails"
29
+ s.add_development_dependency "coveralls"
30
+ end
data/lib/garrison.rb ADDED
@@ -0,0 +1,34 @@
1
+ require 'garrison/locking_active_record'
2
+ require 'garrison/checker_abstract'
3
+ require 'garrison/keeper'
4
+ require 'garrison/injectee'
5
+
6
+ module Garrison
7
+ class << self
8
+ attr_reader :models
9
+
10
+ def lock!(*models)
11
+ @models = models
12
+ enchant_lock
13
+ end
14
+
15
+ def target?(obj)
16
+ !@models || @models.include?(obj.class)|| @models.include?(obj)
17
+ end
18
+
19
+ private
20
+
21
+ def enchant_lock
22
+ return if @enchanted
23
+ @enchanted = true
24
+ ActiveRecord::Base.include LockingActiveRecord
25
+ end
26
+ end
27
+
28
+ class Locked < StandardError
29
+ end
30
+
31
+ class Forbidden < StandardError
32
+ end
33
+ end
34
+
@@ -0,0 +1,22 @@
1
+ module Garrison
2
+ class CheckerAbstract
3
+ attr_reader :user, :obj
4
+
5
+ def initialize(user, obj)
6
+ @user = user || AnonymousOne.new
7
+ @obj = obj
8
+ end
9
+
10
+ def can_write?
11
+ false
12
+ end
13
+
14
+ class AnonymousOne
15
+ def method_missing(name, *)
16
+ return false if !!name.match(/\?$/)
17
+ return nil if !!name.match(/^to_/)
18
+ AnonymousOne.new
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,19 @@
1
+ module Garrison
2
+ class Injectee
3
+ def lock_initial
4
+ lock if @locked.nil?
5
+ end
6
+
7
+ def lock
8
+ @locked = true
9
+ end
10
+
11
+ def unlock
12
+ @locked = false
13
+ end
14
+
15
+ def locked?
16
+ !!@locked
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,34 @@
1
+ module Garrison
2
+ class Keeper
3
+ attr_reader :user
4
+
5
+ def initialize(user)
6
+ @user = user
7
+ end
8
+
9
+ def write(obj, writable: true, &block)
10
+ process(:write, obj, writable, &block)
11
+ end
12
+
13
+ def method_missing(name, obj, writable: false, &block)
14
+ process(name, obj, writable, &block)
15
+ end
16
+
17
+ private
18
+
19
+ def check!(obj, doing)
20
+ keeper_class = "#{obj.class.name}Checker"
21
+ method_name = "can_#{doing}?"
22
+
23
+ raise Forbidden unless eval(keeper_class.gsub('::', '')).new(user, obj).send(method_name)
24
+ end
25
+
26
+ def process(name, obj, writable = false, &block)
27
+ check!(obj, name)
28
+ obj.garrison.unlock if writable && obj.respond_to?(:garrison)
29
+ result = block.call(obj) if block_given?
30
+ obj.garrison.lock if writable && obj.respond_to?(:garrison)
31
+ result
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,20 @@
1
+ module Garrison
2
+ module LockingActiveRecord
3
+ def self.included(klass)
4
+ klass.instance_eval {
5
+ after_initialize -> { garrison.lock_initial if Garrison.target?(self) }
6
+ before_save -> { raise Garrison::Locked if garrison.locked? }
7
+ after_save -> { garrison.lock if Garrison.target?(self) }
8
+ } if klass.respond_to? :after_initialize
9
+ end
10
+
11
+ def garrison
12
+ @garrison_injector ||= Injectee.new
13
+ end
14
+
15
+ def garrison_locked=(val)
16
+ !!val ? garrison.lock : garrison.unlock
17
+ end
18
+ end
19
+ end
20
+
@@ -0,0 +1,3 @@
1
+ module Garrison
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :garrison do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Dummy::Application.load_tasks