snfoil 0.5.5

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: 272fc63879df9b9d3e112dcc8e73638a85a1a75f2faab91530e20fb888cf6c80
4
+ data.tar.gz: 6e5994391f96c17b4016f55e2c52c1cb1fff935c6dfdb05d20572e0232bdbd6b
5
+ SHA512:
6
+ metadata.gz: 90fc1492c594aabe7dc780fc341c3e3fb3d12a7c264b6e85fb08c3bfebd45a69d2cd87a0903e02c979e43c6909f7e5a9c32c39d8cc5f66edf2af541b2182dba6
7
+ data.tar.gz: 1f08ef48c2509a37f25016d9d5ca2e2e0b34bd828180d16e46f42715820690a0ee950e55c869079209f2fe604ab8f6671ddccb7fd1a8cc8592d330bbf0b33bda
@@ -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/
@@ -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).
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ task default: :spec
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'sn_foil/version'
4
+ require_relative 'sn_foil/contexts/build_context'
5
+ require_relative 'sn_foil/contexts/index_context'
6
+ require_relative 'sn_foil/contexts/show_context'
7
+ require_relative 'sn_foil/contexts/create_context'
8
+ require_relative 'sn_foil/contexts/update_context'
9
+ require_relative 'sn_foil/contexts/destroy_context'
10
+ require_relative 'sn_foil/context'
11
+ require_relative 'sn_foil/policy'
12
+ require_relative 'sn_foil/searcher'
13
+ require_relative 'sn_foil/adapters/orms/base_adapter'
14
+ require_relative 'sn_foil/adapters/orms/active_record'
15
+ require 'active_support/core_ext/module/attribute_accessors'
16
+ require 'logger'
17
+
18
+ module SnFoil
19
+ class Error < StandardError; end
20
+
21
+ mattr_accessor :orm, default: 'active_record'
22
+ mattr_writer :logger
23
+
24
+ class << self
25
+ def logger
26
+ @logger ||= Logger.new($stdout).tap do |log|
27
+ log.progname = name
28
+ end
29
+ end
30
+
31
+ def adapter
32
+ return @adapter if @adapter
33
+
34
+ @adapter ||= if orm.instance_of?(String) || orm.instance_of?(Symbol)
35
+ if Object.const_defined?("SnFoil::Adapters::ORMs::#{orm.camelcase}")
36
+ "SnFoil::Adapters::ORMs::#{orm.camelcase}".constantize
37
+ else
38
+ orm.constantize
39
+ end
40
+ else
41
+ orm
42
+ end
43
+ end
44
+
45
+ def configure
46
+ yield self
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base_adapter'
4
+
5
+ module SnFoil
6
+ module Adapters
7
+ module ORMs
8
+ class ActiveRecord < SnFoil::Adapters::ORMs::BaseAdapter
9
+ def new(**params)
10
+ self.class.new(__getobj__.new(params))
11
+ end
12
+
13
+ def all
14
+ __getobj__.all
15
+ end
16
+
17
+ def save
18
+ __getobj__.save
19
+ end
20
+
21
+ def destroy
22
+ __getobj__.destroy
23
+ __getobj__.destroyed?
24
+ end
25
+
26
+ def attributes=(attributes)
27
+ __getobj__.attributes = attributes
28
+ end
29
+
30
+ def is_a?(klass)
31
+ __getobj__.class.object_id == klass.object_id
32
+ end
33
+ end
34
+ end
35
+ end
36
+ 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'
5
+ require_relative 'contexts/index_context'
6
+ require_relative 'contexts/show_context'
7
+ require_relative 'contexts/create_context'
8
+ require_relative 'contexts/update_context'
9
+ require_relative 'contexts/destroy_context'
10
+
11
+ module SnFoil
12
+ module Context
13
+ extend ActiveSupport::Concern
14
+
15
+ included do
16
+ include Contexts::BuildContext
17
+ include Contexts::IndexContext
18
+ include Contexts::ShowContext
19
+ include Contexts::CreateContext
20
+ include Contexts::UpdateContext
21
+ include Contexts::DestroyContext
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'
5
+ require_relative './change_context'
6
+
7
+ module SnFoil
8
+ module Contexts
9
+ module BuildContext
10
+ extend ActiveSupport::Concern
11
+
12
+ included do
13
+ include SetupContext
14
+ include ChangeContext
15
+ end
16
+
17
+ class_methods do
18
+ def build(params:, entity: nil, **options)
19
+ new(entity).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,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/concern'
4
+ require_relative './setup_context'
5
+
6
+ module SnFoil
7
+ module Contexts
8
+ module ChangeContext
9
+ extend ActiveSupport::Concern
10
+
11
+ included do
12
+ include SetupContext
13
+ end
14
+
15
+ class_methods do # rubocop:disable Metrics/BlockLength
16
+ attr_reader :i_params, :i_setup_change_hooks, :i_before_change_hooks, :i_after_change_hooks,
17
+ :i_after_change_success_hooks, :i_after_change_failure_hooks
18
+ def params(*whitelisted_params)
19
+ @i_params ||= []
20
+ @i_params |= whitelisted_params
21
+ end
22
+
23
+ def setup_change(method = nil, **options, &block)
24
+ raise ArgumentError, '#setup_change requires either a method name or a block' if method.nil? && block.nil?
25
+
26
+ (@i_setup_change_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
27
+ end
28
+
29
+ def before_change(method = nil, **options, &block)
30
+ raise ArgumentError, '#before_change requires either a method name or a block' if method.nil? && block.nil?
31
+
32
+ (@i_before_change_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
33
+ end
34
+
35
+ def after_change(method = nil, **options, &block)
36
+ raise ArgumentError, '#after_change requires either a method name or a block' if method.nil? && block.nil?
37
+
38
+ (@i_after_change_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
39
+ end
40
+
41
+ def after_change_success(method = nil, **options, &block)
42
+ raise ArgumentError, '#after_change_success requires either a method name or a block' if method.nil? && block.nil?
43
+
44
+ (@i_after_change_success_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
45
+ end
46
+
47
+ def after_change_failure(method = nil, **options, &block)
48
+ raise ArgumentError, '#after_change_failure requires either a method name or a block' if method.nil? && block.nil?
49
+
50
+ (@i_after_change_failure_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
51
+ end
52
+ end
53
+
54
+ def param_names
55
+ @param_names ||= self.class.i_params
56
+ end
57
+
58
+ def setup_change(**options)
59
+ options[:params] = options[:params].select { |params| param_names.include?(params) } if param_names
60
+ options
61
+ end
62
+
63
+ def before_change(**options)
64
+ options
65
+ end
66
+
67
+ def after_change(**options)
68
+ options
69
+ end
70
+
71
+ def after_change_success(**options)
72
+ options
73
+ end
74
+
75
+ def after_change_failure(**options)
76
+ options
77
+ end
78
+
79
+ def setup_change_hooks
80
+ self.class.i_setup_change_hooks || []
81
+ end
82
+
83
+ def before_change_hooks
84
+ self.class.i_before_change_hooks || []
85
+ end
86
+
87
+ def after_change_hooks
88
+ self.class.i_after_change_hooks || []
89
+ end
90
+
91
+ def after_change_success_hooks
92
+ self.class.i_after_change_success_hooks || []
93
+ end
94
+
95
+ def after_change_failure_hooks
96
+ self.class.i_after_change_failure_hooks || []
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,167 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/concern'
4
+ require_relative './setup_context'
5
+ require_relative './change_context'
6
+
7
+ module SnFoil
8
+ module Contexts
9
+ module CreateContext # rubocop:disable Metrics/ModuleLength
10
+ extend ActiveSupport::Concern
11
+
12
+ included do
13
+ include SetupContext
14
+ include ChangeContext
15
+ end
16
+
17
+ class_methods do
18
+ attr_reader :i_setup_create_hooks, :i_before_create_hooks, :i_after_create_hooks,
19
+ :i_after_create_success_hooks, :i_after_create_failure_hooks
20
+ def create(params:, entity: nil, **options)
21
+ new(entity).create(**options, params: params)
22
+ end
23
+
24
+ def setup_create(method = nil, **options, &block)
25
+ raise ArgumentError, '#setup_create requires either a method name or a block' if method.nil? && block.nil?
26
+
27
+ (@i_setup_create_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
28
+ end
29
+
30
+ def before_create(method = nil, **options, &block)
31
+ raise ArgumentError, '#before_create requires either a method name or a block' if method.nil? && block.nil?
32
+
33
+ (@i_before_create_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
34
+ end
35
+
36
+ def after_create(method = nil, **options, &block)
37
+ raise ArgumentError, '#after_create requires either a method name or a block' if method.nil? && block.nil?
38
+
39
+ (@i_after_create_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
40
+ end
41
+
42
+ def after_create_success(method = nil, **options, &block)
43
+ raise ArgumentError, '#after_create_success requires either a method name or a block' if method.nil? && block.nil?
44
+
45
+ (@i_after_create_success_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
46
+ end
47
+
48
+ def after_create_failure(method = nil, **options, &block)
49
+ raise ArgumentError, '#after_create_failure requires either a method name or a block' if method.nil? && block.nil?
50
+
51
+ (@i_after_create_failure_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
52
+ end
53
+ end
54
+
55
+ def setup_create_object(params: {}, object: nil, **options)
56
+ object = if object
57
+ wrap_object(object)
58
+ else
59
+ klass = options.fetch(:model) { model }
60
+ wrap_object(klass).new
61
+ end
62
+
63
+ object.attributes = params
64
+ options.merge! object: object
65
+ end
66
+
67
+ def create(**options)
68
+ options[:action] = :create
69
+ options = before_setup_create_object(**options)
70
+ options = setup_create_object(**options)
71
+ authorize(options[:object], :create?, **options)
72
+ options = create_hooks(**options)
73
+ unwrap_object(options[:object])
74
+ end
75
+
76
+ def setup_create(**options)
77
+ options
78
+ end
79
+
80
+ def before_create(**options)
81
+ options
82
+ end
83
+
84
+ def after_create(**options)
85
+ options
86
+ end
87
+
88
+ def after_create_success(**options)
89
+ options
90
+ end
91
+
92
+ def after_create_failure(**options)
93
+ options
94
+ end
95
+
96
+ def setup_create_hooks
97
+ self.class.i_setup_create_hooks || []
98
+ end
99
+
100
+ def before_create_hooks
101
+ self.class.i_before_create_hooks || []
102
+ end
103
+
104
+ def after_create_hooks
105
+ self.class.i_after_create_hooks || []
106
+ end
107
+
108
+ def after_create_success_hooks
109
+ self.class.i_after_create_success_hooks || []
110
+ end
111
+
112
+ def after_create_failure_hooks
113
+ self.class.i_after_create_failure_hooks || []
114
+ end
115
+
116
+ private
117
+
118
+ def before_setup_create_object(**options)
119
+ options = setup_create(**options)
120
+ options = setup_create_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
121
+ options = setup_change(**options)
122
+ options = setup_change_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
123
+ options = setup(**options)
124
+ setup_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
125
+ end
126
+
127
+ # This method is private to help protect the order of execution of hooks
128
+ def create_hooks(options)
129
+ options = before_create_save(**options)
130
+ options = if options[:object].save
131
+ after_create_save_success(**options)
132
+ else
133
+ after_create_save_failure(**options)
134
+ end
135
+ after_create_save(**options)
136
+ end
137
+
138
+ def before_create_save(**options)
139
+ options = before_create(**options)
140
+ options = before_create_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
141
+ options = before_change(**options)
142
+ before_change_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
143
+ end
144
+
145
+ def after_create_save(**options)
146
+ options = after_create(**options)
147
+ options = after_create_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
148
+ options = after_change(**options)
149
+ after_change_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
150
+ end
151
+
152
+ def after_create_save_success(**options)
153
+ options = after_create_success(**options)
154
+ options = after_create_success_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
155
+ options = after_change_success(**options)
156
+ after_change_success_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
157
+ end
158
+
159
+ def after_create_save_failure(**options)
160
+ options = after_create_failure(**options)
161
+ options = after_create_failure_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
162
+ options = after_change_failure(**options)
163
+ after_change_failure_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
164
+ end
165
+ end
166
+ end
167
+ end