sparkl 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.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/Gemfile +10 -0
- data/Gemfile.lock +78 -0
- data/LICENSE.txt +21 -0
- data/README.md +137 -0
- data/Rakefile +8 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/sparkl/version.rb +5 -0
- data/lib/sparkl.rb +49 -0
- data/sparkl.gemspec +37 -0
- metadata +74 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1577ca6ac6f682e0522ee114ca3ef1ca9eb8f09a4b001889f2ee24c00952638e
|
4
|
+
data.tar.gz: 8510f89c773ecb4b8ca66ab0445fabd8389848f43e76a5b9d036793a19b2f407
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8a63e86de7dff24dfa97845fcda624409b74ebec4ce9489e3428aeeae27478e1c4cb623dc846f1ce0e82c9631b87486810c827b73ccd3ed3f03103585035f92c
|
7
|
+
data.tar.gz: 525c60f2b7a6e7decebd6b0d7abf39d479bbe53608659d387b11b719b4f49bf8c076881dbd1dd266ad3e9604b49927c126afaeeee03ee94df2dec55e2bdd86c6
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
sparkl (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
actionpack (7.0.3.1)
|
10
|
+
actionview (= 7.0.3.1)
|
11
|
+
activesupport (= 7.0.3.1)
|
12
|
+
rack (~> 2.0, >= 2.2.0)
|
13
|
+
rack-test (>= 0.6.3)
|
14
|
+
rails-dom-testing (~> 2.0)
|
15
|
+
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
16
|
+
actionview (7.0.3.1)
|
17
|
+
activesupport (= 7.0.3.1)
|
18
|
+
builder (~> 3.1)
|
19
|
+
erubi (~> 1.4)
|
20
|
+
rails-dom-testing (~> 2.0)
|
21
|
+
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
22
|
+
activesupport (7.0.3.1)
|
23
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
24
|
+
i18n (>= 1.6, < 2)
|
25
|
+
minitest (>= 5.1)
|
26
|
+
tzinfo (~> 2.0)
|
27
|
+
builder (3.2.4)
|
28
|
+
concurrent-ruby (1.1.10)
|
29
|
+
crass (1.0.6)
|
30
|
+
diff-lcs (1.4.4)
|
31
|
+
erubi (1.11.0)
|
32
|
+
i18n (1.12.0)
|
33
|
+
concurrent-ruby (~> 1.0)
|
34
|
+
loofah (2.18.0)
|
35
|
+
crass (~> 1.0.2)
|
36
|
+
nokogiri (>= 1.5.9)
|
37
|
+
mini_portile2 (2.8.0)
|
38
|
+
minitest (5.16.3)
|
39
|
+
nokogiri (1.13.8)
|
40
|
+
mini_portile2 (~> 2.8.0)
|
41
|
+
racc (~> 1.4)
|
42
|
+
racc (1.6.0)
|
43
|
+
rack (2.2.4)
|
44
|
+
rack-test (2.0.2)
|
45
|
+
rack (>= 1.3)
|
46
|
+
rails-dom-testing (2.0.3)
|
47
|
+
activesupport (>= 4.2.0)
|
48
|
+
nokogiri (>= 1.6)
|
49
|
+
rails-html-sanitizer (1.4.3)
|
50
|
+
loofah (~> 2.3)
|
51
|
+
rake (13.0.6)
|
52
|
+
rspec (3.10.0)
|
53
|
+
rspec-core (~> 3.10.0)
|
54
|
+
rspec-expectations (~> 3.10.0)
|
55
|
+
rspec-mocks (~> 3.10.0)
|
56
|
+
rspec-core (3.10.1)
|
57
|
+
rspec-support (~> 3.10.0)
|
58
|
+
rspec-expectations (3.10.1)
|
59
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
60
|
+
rspec-support (~> 3.10.0)
|
61
|
+
rspec-mocks (3.10.2)
|
62
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
63
|
+
rspec-support (~> 3.10.0)
|
64
|
+
rspec-support (3.10.2)
|
65
|
+
tzinfo (2.0.5)
|
66
|
+
concurrent-ruby (~> 1.0)
|
67
|
+
|
68
|
+
PLATFORMS
|
69
|
+
ruby
|
70
|
+
|
71
|
+
DEPENDENCIES
|
72
|
+
actionpack (>= 5.0)
|
73
|
+
rake (~> 13.0)
|
74
|
+
rspec (~> 3.0)
|
75
|
+
sparkl!
|
76
|
+
|
77
|
+
BUNDLED WITH
|
78
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 Joseph Johansen
|
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,137 @@
|
|
1
|
+
# Sparkl
|
2
|
+
|
3
|
+
Sparkl provides a decorator-like way of writing hooks for your Rails controllers. Example:
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
class MyController < ApplicationController
|
7
|
+
extend Sparkl::Decoration
|
8
|
+
|
9
|
+
def_decorator :special_auth, before_action: [:auth, if: -> { true or false }]
|
10
|
+
|
11
|
+
special_auth def show
|
12
|
+
# ... normal controller logic here
|
13
|
+
end
|
14
|
+
|
15
|
+
# instead of:
|
16
|
+
# before_action :auth, only: [:show], if: -> { true or false }
|
17
|
+
end
|
18
|
+
```
|
19
|
+
|
20
|
+
Decorators can also be chained:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
class MyController < ApplicationController
|
24
|
+
extend Sparkl::Decoration
|
25
|
+
|
26
|
+
def_decorator :no_auth, skip_before_action: :authorize
|
27
|
+
def_decorator :verify_scope, after_action: ->() {
|
28
|
+
# ... logic to check policy scoping...
|
29
|
+
}
|
30
|
+
|
31
|
+
verify_scope no_auth def show
|
32
|
+
# ... normal controller logic here
|
33
|
+
end
|
34
|
+
|
35
|
+
# or with parentheses:
|
36
|
+
# verify_scope(
|
37
|
+
# no_auth(
|
38
|
+
# def show
|
39
|
+
# # ... normal controller logic here
|
40
|
+
# end
|
41
|
+
# )
|
42
|
+
# )
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
and can also be reused:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
module MyActions
|
50
|
+
extend Sparkl::Decorator
|
51
|
+
|
52
|
+
def_decorator :redirect_if_true, before_action: [ :handle_redirect, { if: ->{ true } } ]
|
53
|
+
|
54
|
+
private def handle_redirect
|
55
|
+
redirect_to "..."
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
class MyController < ApplicationController
|
60
|
+
extend MyActions
|
61
|
+
|
62
|
+
redirect_if_true def index
|
63
|
+
# ...
|
64
|
+
end
|
65
|
+
end
|
66
|
+
```
|
67
|
+
|
68
|
+
`def_decorator` is also aliased as `decorator`.
|
69
|
+
|
70
|
+
## Installation
|
71
|
+
|
72
|
+
Add this line to your application's Gemfile:
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
gem 'sparkl'
|
76
|
+
```
|
77
|
+
|
78
|
+
And then execute:
|
79
|
+
|
80
|
+
$ bundle install
|
81
|
+
|
82
|
+
Or install it yourself as:
|
83
|
+
|
84
|
+
$ gem install sparkl
|
85
|
+
|
86
|
+
## Usage
|
87
|
+
|
88
|
+
See above for examples.
|
89
|
+
|
90
|
+
In the decorator definition, you define the moment the action will be performed - it can be
|
91
|
+
performed at multiple times if you like:
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
decorator :foo, before_action: ->{ puts 'foo' }, after_action: ->{ puts :bar }
|
95
|
+
```
|
96
|
+
|
97
|
+
Available timings mirror Rails' own options:
|
98
|
+
|
99
|
+
```
|
100
|
+
before_action
|
101
|
+
prepend_before_action
|
102
|
+
after_action
|
103
|
+
prepend_after_action
|
104
|
+
skip_before_action
|
105
|
+
skip_after_action
|
106
|
+
```
|
107
|
+
|
108
|
+
Each action can take any of the arguments that a normal rails action would:
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
decorator :foo, before_action: [:do_foo, if: :condition?]
|
112
|
+
|
113
|
+
# ...
|
114
|
+
|
115
|
+
# need to have do_foo and condition? methods defined
|
116
|
+
def do_foo
|
117
|
+
# ...
|
118
|
+
end
|
119
|
+
|
120
|
+
def condition?
|
121
|
+
true
|
122
|
+
end
|
123
|
+
```
|
124
|
+
|
125
|
+
## Development
|
126
|
+
|
127
|
+
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.
|
128
|
+
|
129
|
+
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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
130
|
+
|
131
|
+
## Contributing
|
132
|
+
|
133
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/johansenja/sparkl.
|
134
|
+
|
135
|
+
## License
|
136
|
+
|
137
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "sparkl"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/sparkl.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "sparkl/version"
|
4
|
+
|
5
|
+
module Sparkl
|
6
|
+
class InvalidActionsError < StandardError; end
|
7
|
+
|
8
|
+
DECORATORS_CONST_NAME = "Decorators".freeze
|
9
|
+
private_constant :DECORATORS_CONST_NAME
|
10
|
+
|
11
|
+
module Decoration
|
12
|
+
ACTIONS = %i[
|
13
|
+
before_action
|
14
|
+
after_action
|
15
|
+
skip_before_action
|
16
|
+
skip_after_action
|
17
|
+
prepend_before_action
|
18
|
+
prepend_after_action
|
19
|
+
].to_set.freeze
|
20
|
+
|
21
|
+
def self.extended(other)
|
22
|
+
super
|
23
|
+
module_container_for_decorator_methods = Module.new
|
24
|
+
other.const_set DECORATORS_CONST_NAME, module_container_for_decorator_methods
|
25
|
+
other.extend(module_container_for_decorator_methods)
|
26
|
+
|
27
|
+
other.define_singleton_method :extended do |beyond|
|
28
|
+
super beyond
|
29
|
+
beyond.extend module_container_for_decorator_methods
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def decorator(name, **opts)
|
34
|
+
if opts.empty? or opts.any? { |k, _| !ACTIONS.member?(k.to_sym) }
|
35
|
+
raise InvalidOptionsError, "Invalid options #{opts}. Allowed: #{ACTIONS}"
|
36
|
+
end
|
37
|
+
|
38
|
+
const_get(DECORATORS_CONST_NAME).define_method name do |action_name|
|
39
|
+
opts.each do |opt, args|
|
40
|
+
public_send opt, *args, only: [action_name]
|
41
|
+
end
|
42
|
+
|
43
|
+
action_name
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
alias def_decorator decorator
|
48
|
+
end
|
49
|
+
end
|
data/sparkl.gemspec
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/sparkl/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "sparkl"
|
7
|
+
spec.version = Sparkl::VERSION
|
8
|
+
spec.authors = ["Joseph Johansen"]
|
9
|
+
spec.email = ["joe@stotles.com"]
|
10
|
+
|
11
|
+
spec.summary = "Method decorators for your Rails controller actions"
|
12
|
+
spec.description = "Define behaviours for before, after and around actions using a decorator-like manner, similar to TypeScript or Python."
|
13
|
+
spec.homepage = "https://github.com/johansenja/sparkl"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
|
16
|
+
|
17
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
18
|
+
|
19
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
20
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
21
|
+
spec.metadata["changelog_uri"] =spec.homepage
|
22
|
+
|
23
|
+
# Specify which files should be added to the gem when it is released.
|
24
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
25
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
26
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
|
27
|
+
end
|
28
|
+
spec.bindir = "exe"
|
29
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
30
|
+
spec.require_paths = ["lib"]
|
31
|
+
|
32
|
+
# Uncomment to register a new dependency of your gem
|
33
|
+
spec.add_development_dependency "actionpack", ">= 5.0"
|
34
|
+
|
35
|
+
# For more information and examples about making a new gem, checkout our
|
36
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sparkl
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Joseph Johansen
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-09-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: actionpack
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.0'
|
27
|
+
description: Define behaviours for before, after and around actions using a decorator-like
|
28
|
+
manner, similar to TypeScript or Python.
|
29
|
+
email:
|
30
|
+
- joe@stotles.com
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- ".gitignore"
|
36
|
+
- ".rspec"
|
37
|
+
- Gemfile
|
38
|
+
- Gemfile.lock
|
39
|
+
- LICENSE.txt
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- bin/console
|
43
|
+
- bin/setup
|
44
|
+
- lib/sparkl.rb
|
45
|
+
- lib/sparkl/version.rb
|
46
|
+
- sparkl.gemspec
|
47
|
+
homepage: https://github.com/johansenja/sparkl
|
48
|
+
licenses:
|
49
|
+
- MIT
|
50
|
+
metadata:
|
51
|
+
allowed_push_host: https://rubygems.org
|
52
|
+
homepage_uri: https://github.com/johansenja/sparkl
|
53
|
+
source_code_uri: https://github.com/johansenja/sparkl
|
54
|
+
changelog_uri: https://github.com/johansenja/sparkl
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: 2.4.0
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
requirements: []
|
70
|
+
rubygems_version: 3.1.2
|
71
|
+
signing_key:
|
72
|
+
specification_version: 4
|
73
|
+
summary: Method decorators for your Rails controller actions
|
74
|
+
test_files: []
|