super_current 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: c7b06278344d52daeca7f894da9550e61b31949db0aeab3d8b253ecd715745b4
4
+ data.tar.gz: 73966d1ce60a49767b6510c77e268f7d792cb5a33bf89831822bc476ad955234
5
+ SHA512:
6
+ metadata.gz: 399e4951acc3605e3e88865b987f6af2627216889571450ad2186cb938e4aa856b18defd257e7e432f0e8611e596bdd6c701da7c5345f65c2d691dfb7634f465
7
+ data.tar.gz: 316da47d7a8161393f221166276ef556c3c03b04fe2797ee5899a33a080bbd5beaaa904991f31868b4ee1d5e0b95e8f6677d1ab8b758bcdcb2f0a382bf214a67
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2022 Sampo Kuokkanen
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.md ADDED
@@ -0,0 +1,85 @@
1
+ # SuperCurrent
2
+ `SuperCurrent` takes away the need to define a `Current` class and instead makes it available from anywhere in your app without having to define it.
3
+
4
+ This means that your test coverage will not suffer from `ActiveSupport::CurrentAttributes`!
5
+
6
+ You can define the class name of the `Current` class, so if you're already using `Current` for something else, you can use a different name.
7
+
8
+ ```ruby
9
+ # config/initializers/super_current.rb
10
+ SuperCurrent.config.current_class_name = 'SuperCurrent'
11
+ ```
12
+
13
+ Then you can have your existing `Current` class be a subclass of `SuperCurrent` and it will still work with all the exciting features of `SuperCurrent`.
14
+
15
+ ```ruby
16
+ class Current < SuperCurrent
17
+ # ...
18
+ end
19
+ ```
20
+
21
+ ## Usage
22
+ Just think up a name for your attribute and assign it! It will be created on the `Current` class automatically.
23
+
24
+ ```ruby
25
+ Current.foo = 'bar'
26
+ Current.foo # => 'bar'
27
+ ```
28
+
29
+ `SuperCurrent` also adds the ability to use `[]` and `[]=` to access the attributes.
30
+
31
+ ```ruby
32
+ Current[:foo] = 'bar'
33
+ Current[:foo] # => 'bar'
34
+ ```
35
+
36
+ Even something like `Current[:foo][:bar]` will work!
37
+
38
+ ```ruby
39
+ # No need to define Current[:hoge] beforehand!
40
+ Current[:hoge][:bar] = "hello"
41
+ Current[:hoge][:bar] # => 'bar'
42
+ ```
43
+
44
+ This means that you no longer need to define the attributes on the `Current` class beforehand.
45
+
46
+ So if you have a class like this:
47
+
48
+ ```ruby
49
+ class Current < ActiveSupport::CurrentAttributes
50
+ attribute :foo
51
+ attribute :bar
52
+ attribute :baz
53
+ attribute :hoge
54
+ end
55
+ ```
56
+
57
+ You can just delete it and enjoy the benefits of `SuperCurrent`! Test coverage might go up, too!
58
+
59
+ ## Installation
60
+ Add this line to your application's Gemfile:
61
+
62
+ ```ruby
63
+ gem "super_current"
64
+ ```
65
+
66
+ And then execute:
67
+ ```bash
68
+ $ bundle
69
+ ```
70
+
71
+ Or install it yourself as:
72
+ ```bash
73
+ $ gem install super_current
74
+ ```
75
+
76
+ ## Inspiration
77
+
78
+ Inspired by:
79
+ https://wiki.php.net/rfc/deprecate_dynamic_properties
80
+
81
+ ## Contributing
82
+ Feel free to contribute.
83
+
84
+ ## License
85
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require "bundler/setup"
2
+
3
+ require "bundler/gem_tasks"
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SuperCurrent
4
+ class Railtie < ::Rails::Railtie
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SuperCurrent
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'super_current/version'
4
+ require 'super_current/railtie'
5
+ require 'dry-configurable'
6
+
7
+ # SuperCurrent makes it easier to use ActiveSupport::CurrentAttributes
8
+ module SuperCurrent
9
+ extend Dry::Configurable
10
+
11
+ setting :current_class_name, default: 'Current', reader: true
12
+
13
+ # The class that actually has the CurrentAttributes as parent class
14
+ # Named SuperSuperCurrent to avoid conflicts with the SuperCurrent module
15
+ class SuperSuperCurrent < ActiveSupport::CurrentAttributes
16
+ attribute :__current_hash
17
+
18
+ @methods = []
19
+
20
+ def self.[](key)
21
+ self.__current_hash ||= {}
22
+ self.__current_hash[key] || self.__current_hash[key] = {}
23
+ end
24
+
25
+ def self.[]=(key, value)
26
+ self.__current_hash ||= {}
27
+ self.__current_hash[key] = value
28
+ end
29
+
30
+ def self.method_missing(method, *args)
31
+ super unless method.to_s.end_with?('=')
32
+
33
+ attribute_name = method.to_s.chomp('=')
34
+ attribute_value = args.first
35
+
36
+ class_eval { attribute attribute_name.to_sym }
37
+ public_send "#{attribute_name}=", attribute_value
38
+ @methods << attribute_name
39
+ end
40
+
41
+ def self.respond_to_missing?(method, include_private = false)
42
+ @methods.include?(method.to_s) || method.to_s.end_with?('=') || super
43
+ end
44
+ end
45
+ end
46
+
47
+ Object.const_set(SuperCurrent.config.current_class_name, SuperCurrent::SuperSuperCurrent)
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :super_current do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: super_current
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sampo Kuokkanen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-11-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 7.0.4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 7.0.4
27
+ - !ruby/object:Gem::Dependency
28
+ name: dry-configurable
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: standard
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: SuperCurrent adds more fun to your Rails app by making it harder to predict!
56
+ email:
57
+ - sampo.kuokkanen@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - lib/super_current.rb
66
+ - lib/super_current/railtie.rb
67
+ - lib/super_current/version.rb
68
+ - lib/tasks/super_current_tasks.rake
69
+ homepage: https://www.google.com
70
+ licenses:
71
+ - MIT
72
+ metadata:
73
+ homepage_uri: https://www.google.com
74
+ source_code_uri: https://www.google.com
75
+ changelog_uri: https://www.google.com
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubygems_version: 3.3.13
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: SuperCurrent is a way to add magic to CurrentAttributes.
95
+ test_files: []