context_provider 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4312544e6eaeec3ef290c59ca1953364e610a6f0d063d0d3cdd881b303b90326
4
+ data.tar.gz: '0808b0d8c3061208467a56f239054e080293dd4fb8ff9e35b03f9d9f5b581b40'
5
+ SHA512:
6
+ metadata.gz: 051c19bc1721fd6193435fec645f0333b7836fd8f35c58b5c18c0df6e5ffcd6c293d311a3d83a38cfa93a8f03a35feb08359cb8df60210f12da356d5032ce3a8
7
+ data.tar.gz: 8f88e600ffac354307c422671ab75dcc035b1cc2d22627979860f0f7fbb9a5770b227a1e2895e2a99bd010b2f155827f9e59d601d5ac370b6ca5e229c1ee2539
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ *.gem
11
+
12
+ TODO.md
data/.rubocop.yml ADDED
@@ -0,0 +1,16 @@
1
+ # The behavior of RuboCop can be controlled via the .rubocop.yml
2
+ # configuration file. It makes it possible to enable/disable
3
+ # certain cops (checks) and to alter their behavior if they accept
4
+ # any parameters. The file can be placed either in your home
5
+ # directory or in some project directory.
6
+ #
7
+ # RuboCop will start looking for the configuration file in the directory
8
+ # where the inspected file is and continue its way up to the root directory.
9
+ #
10
+ # See https://docs.rubocop.org/rubocop/configuration
11
+
12
+ AllCops:
13
+ NewCops: enable
14
+
15
+ Metrics/BlockLength:
16
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.2
6
+ before_install: gem install bundler -v 2.1.4
data/CHANGELOG.md ADDED
@@ -0,0 +1,17 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
+ and this project adheres to [ZeroVer](https://0ver.org/).
6
+
7
+ ## [Unreleased]
8
+
9
+
10
+ ## [0.0.1] - 2021-03-21
11
+ ### Added
12
+ - `ContextProvider`
13
+ - README
14
+
15
+
16
+ [Unreleased]: https://github.com/olivierlacan/keep-a-changelog/compare/v1.0.0...HEAD
17
+ [0.0.1]: https://github.com/olivierlacan/keep-a-changelog/releases/tag/v0.0.1
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in context_provider.gemspec
6
+ gemspec
7
+
8
+ gem 'minitest', '~> 5.0'
9
+ gem 'pry', '~> 0.13.1'
10
+ gem 'rake', '~> 12.0'
11
+ gem 'rubocop', '~> 1.11', require: false
12
+ gem 'rubocop-minitest', require: false
13
+ gem 'rubocop-rake', require: false
data/Gemfile.lock ADDED
@@ -0,0 +1,56 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ context_provider (0.0.1)
5
+ concurrent-ruby (>= 1.0.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.2)
11
+ coderay (1.1.3)
12
+ concurrent-ruby (1.1.8)
13
+ method_source (1.0.0)
14
+ minitest (5.14.4)
15
+ parallel (1.20.1)
16
+ parser (3.0.0.0)
17
+ ast (~> 2.4.1)
18
+ pry (0.13.1)
19
+ coderay (~> 1.1)
20
+ method_source (~> 1.0)
21
+ rainbow (3.0.0)
22
+ rake (12.3.3)
23
+ regexp_parser (2.1.1)
24
+ rexml (3.2.4)
25
+ rubocop (1.11.0)
26
+ parallel (~> 1.10)
27
+ parser (>= 3.0.0.0)
28
+ rainbow (>= 2.2.2, < 4.0)
29
+ regexp_parser (>= 1.8, < 3.0)
30
+ rexml
31
+ rubocop-ast (>= 1.2.0, < 2.0)
32
+ ruby-progressbar (~> 1.7)
33
+ unicode-display_width (>= 1.4.0, < 3.0)
34
+ rubocop-ast (1.4.1)
35
+ parser (>= 2.7.1.5)
36
+ rubocop-minitest (0.10.3)
37
+ rubocop (>= 0.87, < 2.0)
38
+ rubocop-rake (0.5.1)
39
+ rubocop
40
+ ruby-progressbar (1.11.0)
41
+ unicode-display_width (2.0.0)
42
+
43
+ PLATFORMS
44
+ ruby
45
+
46
+ DEPENDENCIES
47
+ context_provider!
48
+ minitest (~> 5.0)
49
+ pry (~> 0.13.1)
50
+ rake (~> 12.0)
51
+ rubocop (~> 1.11)
52
+ rubocop-minitest
53
+ rubocop-rake
54
+
55
+ BUNDLED WITH
56
+ 2.1.4
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Vinicius Tabille Manjabosco
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # ContextProvider
2
+
3
+ This is the Ruby version of the context pattern originally outlined in
4
+ [React](https://reactjs.org/docs/context.html).
5
+
6
+ The context patter is a solution for a problem that in React is called prop
7
+ drilling.
8
+
9
+ We have found this pattern to be quite useful for Rails applications.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'context_provider'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle install
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install contextprovider
26
+
27
+ ## Usage
28
+
29
+ A `ContextProvider` allows data to be passed down the call stack in a explicit
30
+ way while avoiding having to pass parameters just so it can be used by a nested
31
+ function.
32
+
33
+ ```ruby
34
+ fooContext = ContextProvider.new
35
+
36
+ fooContext.value_provided? #=> false
37
+ fooContext.get # raises ContextNotProvidedError
38
+
39
+ fooContext.provide("bar") do
40
+ if fooContext.value_provided? #=> true
41
+ fooContext.get #=> "bar"
42
+ end
43
+
44
+ fooContext.provide("baz") do # raise ContextAlreadyProvidedError
45
+ # ...
46
+ end
47
+
48
+ fooContext.where_provide_was_called #=> `provide` caller call stack
49
+ end
50
+ ```
51
+
52
+ ## Development
53
+
54
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
55
+ `rake test` to run the tests. You can also run `bin/console` for an
56
+ interactive prompt that will allow you to experiment.
57
+
58
+ To install this gem onto your local machine, run
59
+ `bundle exec rake install`. To release a new version, update the version
60
+ number in `version.rb`, and then run `bundle exec rake release`, which
61
+ will create a git tag for the version, push git commits and tags, and
62
+ push the `.gem` file to [rubygems.org](https://rubygems.org).
63
+
64
+ ## Contributing
65
+
66
+ Bug reports and pull requests are welcome on GitHub at
67
+ https://github.com/thevtm/contextprovider.
68
+
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'test'
8
+ t.libs << 'lib'
9
+ t.test_files = FileList['test/**/*_test.rb']
10
+ end
11
+
12
+ task default: :test
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 'context_provider'
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,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/context_provider/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'context_provider'
7
+ spec.version = ContextProvider::VERSION
8
+ spec.licenses = ['MIT']
9
+ spec.authors = ['Vinicius Manjabosco']
10
+ spec.email = ['vinicius.manjabosco@lendahand.com']
11
+
12
+ spec.summary = 'Ruby version of the context pattern from React.'
13
+ # spec.description = %q{TODO: Write a longer description or delete this line.}
14
+ spec.homepage = 'https://github.com/thevtm/context_provider'
15
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.4.0')
16
+
17
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
18
+
19
+ spec.metadata['homepage_uri'] = spec.homepage
20
+ spec.metadata['source_code_uri'] = spec.homepage
21
+ spec.metadata['changelog_uri'] = 'https://github.com/thevtm/contextprovider/CHANGELOG.md'
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{^(test|spec|features)/}) }
27
+ end
28
+ spec.bindir = 'exe'
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ['lib']
31
+
32
+ spec.add_dependency 'concurrent-ruby', '~> 1.0'
33
+ end
@@ -0,0 +1,117 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'concurrent-ruby'
4
+
5
+ require 'context_provider/version'
6
+
7
+ # A +ContextProvider+ allows data to be passed down the call stack in a explicit
8
+ # way while avoiding having to pass parameters just so it can be used by a
9
+ # nested function.
10
+ #
11
+ # @example
12
+ # context = ContextProvider.new
13
+ #
14
+ # context.provide("bar") do
15
+ # context.get #=> "bar"
16
+ # end
17
+ class ContextProvider
18
+ class Error < StandardError; end
19
+
20
+ class ContextAlreadyProvidedError < Error; end
21
+
22
+ class ContextNotProvidedError < Error; end
23
+
24
+ def initialize
25
+ @provided = Concurrent::ThreadLocalVar.new
26
+ @provide_call_stack = Concurrent::ThreadLocalVar.new
27
+ end
28
+
29
+ # Provided value that can be accessed using +get+ inside the nested block.
30
+ #
31
+ # @raise [ContextAlreadyDefinedError] if context has already been provide
32
+ #
33
+ # @param [Object] value Value to be passed down the stack
34
+ # @param [Proc] block Block where provided value is going to be available.
35
+ def provide(value, &block)
36
+ if value_provided?
37
+ raise ContextAlreadyProvidedError,
38
+ 'Failed to provide context because it has already been provided!'
39
+ end
40
+
41
+ begin
42
+ set_provided_value(value)
43
+ block.call
44
+ ensure
45
+ unset_provided_value
46
+ end
47
+ end
48
+
49
+ # Gets value from provided by the context.
50
+ #
51
+ # @raises [ContextNotProvidedError] if no value has been provided
52
+ #
53
+ # @return [Object] provided value
54
+ def get
55
+ unless value_provided?
56
+ raise ContextNotProvidedError,
57
+ 'Unable to get context value as it has not been defined!'
58
+ end
59
+
60
+ @provided.value
61
+ end
62
+
63
+ # Returns +true+ when value has been provided (inside a +provide+ block)
64
+ # +false+ otherwise.
65
+ #
66
+ # @return [Boolean]
67
+ def value_provided?
68
+ @provide_call_stack.value != nil
69
+ end
70
+
71
+ # Returns the callstack for where +provide+ was called.
72
+ #
73
+ # @raise [ContextNotProvidedError] if no value has been provided
74
+ #
75
+ # @return [Array] callstack for where +provide+ was called
76
+ def where_provide_was_called
77
+ unless value_provided?
78
+ raise ContextNotProvidedError,
79
+ 'Unable to get the provide callstack because context has not been ' \
80
+ 'provided!'
81
+ end
82
+
83
+ @provide_call_stack.value
84
+ end
85
+
86
+ # Sets provided value.
87
+ # Doesn't perform any checks.
88
+ # Doesn't clean up (see +dangerously_unset_provided_value+).
89
+ # Should only be used as a last resort.
90
+ #
91
+ # @param [Object] value
92
+ def dangerously_set_provided_value(value)
93
+ set_provided_value(value)
94
+ end
95
+
96
+ # Unsets provided value.
97
+ # Doesn't perform any checks.
98
+ # Meant to be used in conjunction with +dangerously_set_provided_value+.
99
+ # Should only be used as a last resort.
100
+ def dangerously_unset_provided_value
101
+ unset_provided_value
102
+ end
103
+
104
+ private
105
+
106
+ # rubocop:disable Naming/AccessorMethodName
107
+ def set_provided_value(value)
108
+ @provide_call_stack.value = caller(2)
109
+ @provided.value = value
110
+ end
111
+ # rubocop:enable Naming/AccessorMethodName
112
+
113
+ def unset_provided_value
114
+ @provided.value = nil
115
+ @provide_call_stack.value = nil
116
+ end
117
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ContextProvider
4
+ VERSION = '0.0.1'
5
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: context_provider
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Vinicius Manjabosco
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-03-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: concurrent-ruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ description:
28
+ email:
29
+ - vinicius.manjabosco@lendahand.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - ".rubocop.yml"
36
+ - ".travis.yml"
37
+ - CHANGELOG.md
38
+ - Gemfile
39
+ - Gemfile.lock
40
+ - LICENSE
41
+ - README.md
42
+ - Rakefile
43
+ - bin/console
44
+ - bin/setup
45
+ - contextprovider.gemspec
46
+ - lib/context_provider.rb
47
+ - lib/context_provider/version.rb
48
+ homepage: https://github.com/thevtm/context_provider
49
+ licenses:
50
+ - MIT
51
+ metadata:
52
+ homepage_uri: https://github.com/thevtm/context_provider
53
+ source_code_uri: https://github.com/thevtm/context_provider
54
+ changelog_uri: https://github.com/thevtm/contextprovider/CHANGELOG.md
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.4
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: Ruby version of the context pattern from React.
74
+ test_files: []