class_store 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ffc4cad3f8f4d70400fa68f8d4eef97a1a2ee4287bca220b7ea902f778185ec9
4
+ data.tar.gz: 255fac79049cdfc9fa3deec66df07ddf764514e821196b82752ba547e2f1e7e1
5
+ SHA512:
6
+ metadata.gz: 9c47575057406c03c452d10010356f4ffd2eb13bf2e06c4c4c75a0bf2162b6305f83f4f9dd432abdbec41a20272ef9d5fbf31b3ee4ef8d6386d2f9b0a3514267
7
+ data.tar.gz: 025303bf83ab6f3f76942b5cdee572ed9fd517f22214e60a5c4a31494fcdb076a11af39ab9a48a66fe5c7455629376dce4c8fe106a8231f67e21b89e8aeb31f4
data/.rubocop.yml ADDED
@@ -0,0 +1,24 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.0
3
+ SuggestExtensions: false
4
+ NewCops: enable
5
+ Exclude:
6
+ - "bin/**/*"
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ EnforcedStyle: double_quotes
10
+
11
+ Metrics/BlockLength:
12
+ Enabled: false
13
+
14
+ Metrics/AbcSize:
15
+ Exclude:
16
+ - "test/**/*"
17
+
18
+ Metrics/CyclomaticComplexity:
19
+ Exclude:
20
+ - "test/**/*"
21
+
22
+ Metrics/MethodLength:
23
+ Exclude:
24
+ - "test/**/*"
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-02-17
4
+
5
+ - Initial release
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 drewthorp
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,90 @@
1
+ # ClassStore
2
+
3
+ ClassStore provides a simple way to store data in arrays and hashes at a class level. Unlike class variables, changes made on a subclass are not replicated in their base class, and unlike class instance variables the contents of a base class are replicated in it's subclasses.
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add class_store
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install class_store
14
+
15
+ ## Usage
16
+
17
+ By default the data is stored in a Hash:
18
+
19
+ ```ruby
20
+ class MyClass
21
+ include ClassStore
22
+
23
+ class_store :data
24
+ end
25
+
26
+ MyClass.data[:foo] = 'bar'
27
+ MyClass.data[:foo] # => 'bar'
28
+ ```
29
+
30
+ Alternatively, the data can be stored in an Array:
31
+
32
+ ```ruby
33
+ class MyArrayStore
34
+ include ClassStore
35
+
36
+ class_store :data, inital_state: []
37
+ end
38
+
39
+ MyArrayStore.data << 'foo'
40
+ MyArrayStore.data # => ['foo']
41
+ ```
42
+
43
+ Derived classes inherit the data from their base class and can add there own. The data added in the derived class is not replicated in the base class:
44
+
45
+ ```ruby
46
+ class MyClass
47
+ include ClassStore
48
+
49
+ class_store :data
50
+
51
+ def self.add_data(key, value)
52
+ data[key] = value
53
+ end
54
+
55
+ add_data(:foo, 'bar')
56
+ end
57
+
58
+ class MySubClass < MyClass
59
+ add_data(:baz, 'qux')
60
+ end
61
+
62
+ MyClass.data # => {:foo => 'bar'}
63
+ MySubClass.data # => {:foo => 'bar', :baz => 'qux'}
64
+ ```
65
+
66
+ Calls to a stores reset method will reset the data in that class alone. It will not alter the base class of a derived class, and, if called on a base class will not propagate to it's subclasses:
67
+
68
+ ```ruby
69
+ MyClass.reset_data
70
+ MyClass.data # => {}
71
+ MySubClass.data # => {:baz => 'qux'}
72
+ ```
73
+
74
+ ## Development
75
+
76
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
77
+
78
+ 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).
79
+
80
+ ## Contributing
81
+
82
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/class_store. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/class_store/blob/main/CODE_OF_CONDUCT.md).
83
+
84
+ ## License
85
+
86
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
87
+
88
+ ## Code of Conduct
89
+
90
+ Everyone interacting in the ClassStore project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/class_store/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'minitest/test_task'
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[test rubocop]
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ClassStore
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,121 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'class_store/version'
4
+ require 'active_support/concern'
5
+ require 'active_support/core_ext/module/delegation'
6
+ require 'active_support/core_ext/class/attribute'
7
+ require 'active_support/core_ext/object/deep_dup'
8
+
9
+ # module ClassStorage
10
+ # extend ActiveSupport::Concern
11
+ #
12
+ # included do
13
+ # class_attribute :_storage, instance_accessor: false, instance_predicate: false
14
+ # self._storage = {}
15
+ # end
16
+ #
17
+ # class_methods do
18
+ # def inherited(subclass)
19
+ # super
20
+ # # Ensure each subclass starts with a copy of its parent's storage
21
+ # subclass._storage = _storage.deep_dup
22
+ # end
23
+ #
24
+ # def set(key, value)
25
+ # _storage[key] = value
26
+ # end
27
+ #
28
+ # def get(key)
29
+ # _storage[key]
30
+ # end
31
+ # end
32
+ # end
33
+
34
+ # def deep_dup_with_procs(obj)
35
+ # case obj
36
+ # when Hash
37
+ # obj.each_with_object({}) do |(k, v), new_hash|
38
+ # new_hash[deep_dup_with_procs(k)] = deep_dup_with_procs(v)
39
+ # end
40
+ # when Array
41
+ # obj.map { |e| deep_dup_with_procs(e) }
42
+ # else
43
+ # obj.duplicable? ? obj.dup : obj
44
+ # end
45
+ # end
46
+
47
+ module ClassStore
48
+ class DuplicateClassStoreError < StandardError; end
49
+ class IllegalStoreTyoeError < StandardError; end
50
+
51
+ # Class methods for registering, accessing and resetting class stores
52
+ module Registration
53
+ extend ActiveSupport::Concern
54
+
55
+ included do
56
+ class_attribute :_registered_class_stores, instance_writer: false
57
+ self._registered_class_stores = {}
58
+ end
59
+
60
+ class_methods do
61
+ # Register a class store on the class with the given name and options.
62
+ def class_store(name, **options)
63
+ raise ArgumentError, 'name is a required argument' if name.nil?
64
+ if _registered_class_stores.key?(name)
65
+ raise DuplicateClassStoreError, "Class store #{name} has already been registered"
66
+ end
67
+
68
+ inital_state = options.delete(:initial_state) || {}
69
+ unless inital_state.is_a?(Hash) || inital_state.is_a?(Array)
70
+ raise IllegalStoreTyoeError, 'initial_state must be a hash or array'
71
+ end
72
+
73
+ _registered_class_stores[name] = { initial_state: inital_state, options: options }
74
+ _create_class_store(name, inital_state, options)
75
+ end
76
+
77
+ def inherited(subclass)
78
+ super
79
+
80
+ # Clone the registered class stores on the subclass
81
+ subclass._registered_class_stores = _registered_class_stores.deep_dup
82
+
83
+ # Ensure each subclass starts with a copy of its parent's stores
84
+ subclass._registered_class_stores.each do |name, store_options|
85
+ subclass._create_class_store(name, store_options[:initial_state], store_options[:options])
86
+ subclass.send("_#{name}=", send("_#{name}").deep_dup)
87
+ end
88
+ end
89
+
90
+ def _create_class_store(name, initial_state, _options)
91
+ class_attribute :"_#{name}", instance_writer: false
92
+ send("_#{name}=", initial_state.deep_dup)
93
+ _generate_class_store_accessor(name)
94
+ _generate_class_store_resetter(name)
95
+ end
96
+
97
+ private
98
+
99
+ def _generate_class_store_accessor(name)
100
+ define_singleton_method(name) do
101
+ send("_#{name}")
102
+ end
103
+
104
+ define_singleton_method("#{name}=") do |value|
105
+ send("_#{name}")[value] = value
106
+ end
107
+
108
+ delegate name.to_sym, to: :class
109
+ end
110
+
111
+ def _generate_class_store_resetter(name)
112
+ resetter_name = "reset_#{name}"
113
+ define_singleton_method(resetter_name) do
114
+ send("_#{name}=", _registered_class_stores[name][:initial_state].dup)
115
+ end
116
+
117
+ delegate resetter_name.to_sym, to: :class
118
+ end
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,4 @@
1
+ module ClassStore
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: class_store
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - drewthorp
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2025-05-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 7.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: 7.1.0
27
+ description: Provides a class store for when writing APIs that need to persist data
28
+ against a class.
29
+ email:
30
+ - gems@fishfur.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".rubocop.yml"
36
+ - CHANGELOG.md
37
+ - CODE_OF_CONDUCT.md
38
+ - LICENSE.txt
39
+ - README.md
40
+ - Rakefile
41
+ - lib/class_store.rb
42
+ - lib/class_store/version.rb
43
+ - sig/class_store.rbs
44
+ homepage: https://github.com/Fish-Fur/class_store
45
+ licenses:
46
+ - MIT
47
+ metadata:
48
+ homepage_uri: https://github.com/Fish-Fur/class_store
49
+ source_code_uri: https://github.com/Fish-Fur/class_store
50
+ changelog_uri: https://github.com/Fish-Fur/class_store
51
+ rubygems_mfa_required: 'true'
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 3.0.0
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubygems_version: 3.5.11
68
+ signing_key:
69
+ specification_version: 4
70
+ summary: Provides a class store for when writing APIs that need to persist data against
71
+ a class.
72
+ test_files: []