snfoil 0.1.0

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
+ SHA256:
3
+ metadata.gz: aa83f45d1f03f617f38920f9b8d5f58f91e3f09f086b1ce0cd5ee9885149b0c4
4
+ data.tar.gz: b66ceb4525d2d784057499975da6d63d01e0560f778f64ca91650fc51083899d
5
+ SHA512:
6
+ metadata.gz: 352fe39c6ebcb7abbe6119dddd3f0ad5b8cd3a0c21ea407fafc8791c57b0494ebc07130698eb9a90caff5129933b9ad2e746644d82fd27b713b0dd32c224ae9e
7
+ data.tar.gz: ee2073826354fa610f4a97a0347ca68f252b0a91cf4fde06a3797da2c6efb4ec47e9ac31f474f3676630ef78e2d7c1f46abcaaf3c6c6572942f130ac19320add
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.rubocop.yml ADDED
@@ -0,0 +1,28 @@
1
+ require:
2
+ - rubocop-rspec
3
+
4
+ # ================ Style ===============
5
+ # TODO: Activate later
6
+ Style/Documentation:
7
+ Enabled: false
8
+
9
+ # ================ LINT ================
10
+ Lint/AmbiguousBlockAssociation:
11
+ Exclude:
12
+ - spec/**/*_spec.rb
13
+
14
+ # ================ Metics ================
15
+ Metrics/LineLength:
16
+ Max: 150
17
+
18
+ Metrics/BlockLength:
19
+ Exclude:
20
+ - spec/**/*_spec.rb
21
+
22
+ # ================ RSPEC ================
23
+
24
+ RSpec/MultipleExpectations:
25
+ Max: 5
26
+
27
+ Rspec/NestedGroups:
28
+ Max: 5
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.6.5
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at howeszy@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in snfoil.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,112 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ snfoil (0.1.0)
5
+ activesupport (>= 5.1)
6
+ logger (~> 1.0)
7
+ pundit (~> 2.0)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ activesupport (6.0.2.1)
13
+ concurrent-ruby (~> 1.0, >= 1.0.2)
14
+ i18n (>= 0.7, < 2)
15
+ minitest (~> 5.1)
16
+ tzinfo (~> 1.1)
17
+ zeitwerk (~> 2.2)
18
+ ast (2.4.0)
19
+ byebug (11.0.1)
20
+ coderay (1.1.2)
21
+ concurrent-ruby (1.1.5)
22
+ diff-lcs (1.3)
23
+ dry-configurable (0.9.0)
24
+ concurrent-ruby (~> 1.0)
25
+ dry-core (~> 0.4, >= 0.4.7)
26
+ dry-container (0.7.2)
27
+ concurrent-ruby (~> 1.0)
28
+ dry-configurable (~> 0.1, >= 0.1.3)
29
+ dry-core (0.4.9)
30
+ concurrent-ruby (~> 1.0)
31
+ dry-equalizer (0.3.0)
32
+ dry-inflector (0.2.0)
33
+ dry-logic (1.0.5)
34
+ concurrent-ruby (~> 1.0)
35
+ dry-core (~> 0.2)
36
+ dry-equalizer (~> 0.2)
37
+ dry-struct (1.1.1)
38
+ dry-core (~> 0.4, >= 0.4.3)
39
+ dry-equalizer (~> 0.2)
40
+ dry-types (~> 1.0)
41
+ ice_nine (~> 0.11)
42
+ dry-types (1.2.2)
43
+ concurrent-ruby (~> 1.0)
44
+ dry-container (~> 0.3)
45
+ dry-core (~> 0.4, >= 0.4.4)
46
+ dry-equalizer (~> 0.3)
47
+ dry-inflector (~> 0.1, >= 0.1.2)
48
+ dry-logic (~> 1.0, >= 1.0.2)
49
+ i18n (1.8.2)
50
+ concurrent-ruby (~> 1.0)
51
+ ice_nine (0.11.2)
52
+ jaro_winkler (1.5.4)
53
+ logger (1.4.2)
54
+ method_source (0.9.2)
55
+ minitest (5.14.0)
56
+ parallel (1.19.0)
57
+ parser (2.6.5.0)
58
+ ast (~> 2.4.0)
59
+ pry (0.12.2)
60
+ coderay (~> 1.1.0)
61
+ method_source (~> 0.9.0)
62
+ pry-byebug (3.7.0)
63
+ byebug (~> 11.0)
64
+ pry (~> 0.10)
65
+ pundit (2.1.0)
66
+ activesupport (>= 3.0.0)
67
+ rainbow (3.0.0)
68
+ rake (13.0.1)
69
+ rspec (3.9.0)
70
+ rspec-core (~> 3.9.0)
71
+ rspec-expectations (~> 3.9.0)
72
+ rspec-mocks (~> 3.9.0)
73
+ rspec-core (3.9.0)
74
+ rspec-support (~> 3.9.0)
75
+ rspec-expectations (3.9.0)
76
+ diff-lcs (>= 1.2.0, < 2.0)
77
+ rspec-support (~> 3.9.0)
78
+ rspec-mocks (3.9.0)
79
+ diff-lcs (>= 1.2.0, < 2.0)
80
+ rspec-support (~> 3.9.0)
81
+ rspec-support (3.9.0)
82
+ rubocop (0.76.0)
83
+ jaro_winkler (~> 1.5.1)
84
+ parallel (~> 1.10)
85
+ parser (>= 2.6)
86
+ rainbow (>= 2.2.2, < 4.0)
87
+ ruby-progressbar (~> 1.7)
88
+ unicode-display_width (>= 1.4.0, < 1.7)
89
+ rubocop-rspec (1.36.0)
90
+ rubocop (>= 0.68.1)
91
+ ruby-progressbar (1.10.1)
92
+ thread_safe (0.3.6)
93
+ tzinfo (1.2.6)
94
+ thread_safe (~> 0.1)
95
+ unicode-display_width (1.6.0)
96
+ zeitwerk (2.2.2)
97
+
98
+ PLATFORMS
99
+ ruby
100
+
101
+ DEPENDENCIES
102
+ bundler (~> 2.0)
103
+ dry-struct (~> 1.0)
104
+ pry-byebug (~> 3.0)
105
+ rake (~> 13.0)
106
+ rspec (~> 3.9)
107
+ rubocop (~> 0.76.0)
108
+ rubocop-rspec (~> 1.36.0)
109
+ snfoil!
110
+
111
+ BUNDLED WITH
112
+ 2.0.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Matthew Howes
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,43 @@
1
+ # Sn::Foil
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/sn_foil`. 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 'snfoil'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install snfoil
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. 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]/snfoil. 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
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the Sn::Foil project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/snfoil/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ task default: :spec
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 'sn_foil'
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
@@ -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,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SnFoil
4
+ module Adapters
5
+ module ORMs
6
+ class ActiveRecord < SnFoil::Adapters::ORMs::BaseAdapter
7
+ def new(*params)
8
+ self.class.new(__getobj__.new(*params))
9
+ end
10
+
11
+ def all
12
+ __getobj__.all
13
+ end
14
+
15
+ def save
16
+ __getobj__.save
17
+ end
18
+
19
+ def destroy
20
+ __getobj__.destroy
21
+ __getobj__.destroyed?
22
+ end
23
+
24
+ def attributes=(*attributes)
25
+ __getobj__.attributes = attributes
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SnFoil
4
+ module Adapters
5
+ module ORMs
6
+ class BaseAdapter < SimpleDelegator
7
+ def new(*_params)
8
+ raise NotImplementedError, '#new not implemented in adapter'
9
+ end
10
+
11
+ def all
12
+ raise NotImplementedError, '#all not implemented in adapter'
13
+ end
14
+
15
+ def save
16
+ raise NotImplementedError, '#save not implemented in adapter'
17
+ end
18
+
19
+ def destroy
20
+ raise NotImplementedError, '#destroy not implemented in adapter'
21
+ end
22
+
23
+ def attributes=(**_attributes)
24
+ raise NotImplementedError, '#attributes= not implemented in adapter'
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/concern'
4
+ require_relative 'contexts/build_context_concern'
5
+ require_relative 'contexts/index_context_concern'
6
+ require_relative 'contexts/show_context_concern'
7
+ require_relative 'contexts/create_context_concern'
8
+ require_relative 'contexts/update_context_concern'
9
+ require_relative 'contexts/destroy_context_concern'
10
+
11
+ module SnFoil
12
+ module Context
13
+ extend ActiveSupport::Concern
14
+
15
+ included do
16
+ include Contexts::BuildContextConcern
17
+ include Contexts::IndexContextConcern
18
+ include Contexts::ShowContextConcern
19
+ include Contexts::CreateContextConcern
20
+ include Contexts::UpdateContextConcern
21
+ include Contexts::DestroyContextConcern
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/concern'
4
+ require_relative './setup_context_concern'
5
+ require_relative './change_context_concern'
6
+
7
+ module SnFoil
8
+ module Contexts
9
+ module BuildContextConcern
10
+ extend ActiveSupport::Concern
11
+
12
+ included do
13
+ include SetupContextConcern
14
+ include ChangeContextConcern
15
+ end
16
+
17
+ class_methods do
18
+ def build(params:, user: nil, **options)
19
+ new(user).build(**options, params: params)
20
+ end
21
+ end
22
+
23
+ def setup_build_object(params: {}, object: nil, **options)
24
+ SnFoil.logger.info 'Warning: Using build bypasses authorize. It is safer to interact with models through create' unless ENV['ISTEST']
25
+ return wrap_object(object) if object
26
+
27
+ klass = options.fetch(:model) { model }
28
+ options.merge! object: wrap_object(klass).new(**params)
29
+ end
30
+
31
+ def build(**options)
32
+ options[:action] = :build
33
+ options = setup_build(setup_change(**options))
34
+ options = setup_build_object(**options)
35
+ unwrap_object(options[:object])
36
+ end
37
+
38
+ def setup_build(**options)
39
+ options
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/concern'
4
+ require_relative './setup_context_concern'
5
+
6
+ module SnFoil
7
+ module Contexts
8
+ module ChangeContextConcern
9
+ extend ActiveSupport::Concern
10
+
11
+ included do
12
+ include SetupContextConcern
13
+ end
14
+
15
+ class_methods do
16
+ attr_reader :i_params, :i_before_change_hooks, :i_after_change_hooks, :i_after_change_success_hooks, :i_after_change_failure_hooks
17
+ def params(*whitelisted_params)
18
+ @i_params ||= []
19
+ @i_params |= whitelisted_params
20
+ end
21
+
22
+ def before_change(method = nil, **options, &block)
23
+ raise ArgumentError, '#on_change requires either a method name or a block' if method.nil? && block.nil?
24
+
25
+ (@i_before_change_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
26
+ end
27
+
28
+ def after_change(method = nil, **options, &block)
29
+ raise ArgumentError, '#after_change requires either a method name or a block' if method.nil? && block.nil?
30
+
31
+ (@i_after_change_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
32
+ end
33
+
34
+ def after_change_success(method = nil, **options, &block)
35
+ raise ArgumentError, '#after_change_success requires either a method name or a block' if method.nil? && block.nil?
36
+
37
+ (@i_after_change_success_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
38
+ end
39
+
40
+ def after_change_failure(method = nil, **options, &block)
41
+ raise ArgumentError, '#after_change_failure requires either a method name or a block' if method.nil? && block.nil?
42
+
43
+ (@i_after_change_failure_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
44
+ end
45
+ end
46
+
47
+ def param_names
48
+ @param_names ||= self.class.i_params
49
+ end
50
+
51
+ def setup_change(**options)
52
+ options[:params] = options[:params].select { |params| param_names.include?(params) } if param_names
53
+ options
54
+ end
55
+
56
+ def before_change(**options)
57
+ options
58
+ end
59
+
60
+ def after_change(**options)
61
+ options
62
+ end
63
+
64
+ def after_change_success(**options)
65
+ options
66
+ end
67
+
68
+ def after_change_failure(**options)
69
+ options
70
+ end
71
+
72
+ def before_change_hooks
73
+ self.class.i_before_change_hooks || []
74
+ end
75
+
76
+ def after_change_hooks
77
+ self.class.i_after_change_hooks || []
78
+ end
79
+
80
+ def after_change_success_hooks
81
+ self.class.i_after_change_success_hooks || []
82
+ end
83
+
84
+ def after_change_failure_hooks
85
+ self.class.i_after_change_failure_hooks || []
86
+ end
87
+
88
+ def run_hook(hook, **options)
89
+ return options unless hook_valid?(hook, **options)
90
+
91
+ return send(hook[:method], **options) if hook[:method]
92
+
93
+ hook[:block].call(options)
94
+ end
95
+
96
+ def hook_valid?(hook, **options)
97
+ return false if !hook[:if].nil? && hook[:if].call(options) == false
98
+ return false if !hook[:unless].nil? && hook[:unless].call(options) == true
99
+
100
+ true
101
+ end
102
+ end
103
+ end
104
+ end