lazy_load_attributes 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: ad7ccf90af014e09150c0445aa798702fbdcff84203562aa2c7cd5fc960ed090
4
+ data.tar.gz: 3bd0d5a98e55beb2ef2c7c0e58c65b0be5ccff570ef262a43a134804566837c8
5
+ SHA512:
6
+ metadata.gz: e0a48e5efc3240a93dc0624d572188b1ab88a02eb80b1e3ab7733d76c8a08e6eefbfd8034af8f11c0785255efbc6a900e68557c2f47480b9e304d2e244511fd5
7
+ data.tar.gz: 403da3d15930f376b7fef7965f37e5e57a99d322d45746f4729659cd161b12ba08fed34530e96c45783f67cbdf88d2346aac80e36e4cd35e9907ba342d4829d7
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased Changes]
2
+
3
+ ## 0.1.0
4
+
5
+ - initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at eizengan@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/README.md ADDED
@@ -0,0 +1,119 @@
1
+ # LazyLoadAttributes
2
+
3
+ Simple "Ruby core"-inspired syntactic sugar for defining cached, lazy-loaded attributes which intuitively handle inheritence and attribute redefinition.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'lazy_load_attributes'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install lazy_load_attributes
20
+
21
+ ## Usage
22
+
23
+ The class-level `lazy_attr_reader` method defines a lazy-loaded attribute.
24
+
25
+ A simple use-case:
26
+
27
+ ```ruby
28
+ require "lazy_load_attributes"
29
+
30
+ class Example
31
+ extend LazyLoadAttributes
32
+
33
+ lazy_attr_reader(:lazy_attr) { puts "loaded"; lazy_attr_method }
34
+
35
+ def lazy_attr_method
36
+ "lazy value"
37
+ end
38
+ end
39
+
40
+ example = Example.new
41
+ example.lazy_attr
42
+ # loaded
43
+ # => "lazy value"
44
+ example.lazy_attr
45
+ # => "lazy value"
46
+ ```
47
+
48
+ This illustrates four major powers of LazyLoadAttributes:
49
+
50
+ - the attribute calls evaluate and return their definition's initializer
51
+ - the initializer is not evaluated until the first attribute call
52
+ - the initializer is evaluated in the context of the calling instance (i.e. `example` in the sample above), so its instance methods are available during evaluation as they would be within the body of a normal instance method
53
+ - the initializer is evaluated only once; subsequent attribute calls are served from a cache
54
+
55
+ Inheritence is handled transparently:
56
+
57
+ ```ruby
58
+ class Superclass
59
+ extend LazyLoadAttributes
60
+
61
+ lazy_attr_reader(:super_attr) { super_attr_value }
62
+ lazy_attr_reader(:redefine_attr) { "redefine_attr from Superclass" }
63
+
64
+ def super_attr_value
65
+ "Superclass#super_attr_value"
66
+ end
67
+
68
+ def sub_attr_value
69
+ "Superclass#sub_attr_value"
70
+ end
71
+ end
72
+
73
+ class Subclass < Superclass
74
+ lazy_attr_reader(:redefine_attr) { "redefine_attr from Subclass" }
75
+ lazy_attr_reader(:sub_attr) { sub_attr_value }
76
+
77
+ def super_attr_value
78
+ "Subclass#super_attr_value"
79
+ end
80
+ end
81
+
82
+ super_instance = Superclass.new
83
+ super_instance.super_attr
84
+ # => "Superclass#super_attr_value"
85
+ super_instance.redefine_attr
86
+ # => "redefine_attr from Superclass"
87
+
88
+ sub_instance = Subclass.new
89
+ sub_instance.super_attr
90
+ # => "Subclass#super_attr_value"
91
+ sub_instance.sub_attr
92
+ # => "Superclass#sub_attr_value"
93
+ sub_instance.redefine_attr
94
+ # => "redefine_attr from Subclass"
95
+ ```
96
+
97
+ The call to `sub_instance.super_attr` shows that the instance methods used by an initializer respect inheritence even when the attribute itself is defined in the superclass
98
+
99
+ The call to `sub_instance.sub_attr` shows that initializers used by definitions in a subclass have access to instance methods defined in a superclass
100
+
101
+ The call to `sub_instance.redefine_attr` shows that an attribute redefined in a subclass will override its definition in the superclass
102
+
103
+ ## Development
104
+
105
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
106
+
107
+ 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).
108
+
109
+ ## Contributing
110
+
111
+ Bug reports and pull requests are welcome on GitHub at https://github.com/eizengan/lazy_load_attributes. 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/eizengan/lazy_load_attributes/blob/main/CODE_OF_CONDUCT.md).
112
+
113
+ ## License
114
+
115
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
116
+
117
+ ## Code of Conduct
118
+
119
+ Everyone interacting in the LazyLoadAttributes project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/eizengan/lazy_load_attributes/blob/main/CODE_OF_CONDUCT.md).
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/lazy_load_attributes/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes." unless spec.respond_to?(:metadata)
7
+
8
+ spec.name = "lazy_load_attributes"
9
+ spec.version = LazyLoadAttributes::VERSION
10
+ spec.authors = ["Nate Eizenga"]
11
+ spec.email = ["eizengan@gmail.com"]
12
+
13
+ spec.summary = "Concise definition of lazy-loaded class attributes"
14
+ spec.description = <<~DESCRIPTION
15
+ Simple "Ruby core"-inspired syntactic sugar for defining cached, lazy-loaded attributes which intuitively handle
16
+ inheritence and attribute redefinition.
17
+ DESCRIPTION
18
+ spec.homepage = "https://github.com/eizengan/lazy_load_attributes"
19
+ spec.license = "MIT"
20
+
21
+ spec.metadata["homepage_uri"] = spec.homepage
22
+ spec.metadata["source_code_uri"] = spec.homepage
23
+ spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
24
+ spec.metadata["rubygems_mfa_required"] = "true"
25
+
26
+ # Specify which files should be added to the gem when it is released.
27
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
28
+ spec.files = %w[
29
+ CHANGELOG.md
30
+ CODE_OF_CONDUCT.md
31
+ lazy_load_attributes.gemspec
32
+ README.md
33
+ ] + Dir["lib/**/*"]
34
+ spec.bindir = "bin"
35
+ spec.executables += spec.files.grep(%r{\A#{spec.bindir}/}) { |f| File.basename(f) }
36
+ spec.require_paths = ["lib"]
37
+
38
+ spec.required_ruby_version = [">= 2.7", "< 4"]
39
+
40
+ spec.add_development_dependency "pry-byebug", "~> 3.0"
41
+ spec.add_development_dependency "rake", "~> 13.0"
42
+ spec.add_development_dependency "rspec", "~> 3.0"
43
+ spec.add_development_dependency "rubocop", "~> 1.21"
44
+ spec.add_development_dependency "rubocop-performance", "~> 1.0"
45
+ spec.add_development_dependency "rubocop-rake", "~> 0.0"
46
+ spec.add_development_dependency "rubocop-rspec", "~> 2.0"
47
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LazyLoadAttributes
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "set"
4
+ require_relative "lazy_load_attributes/version"
5
+
6
+ module LazyLoadAttributes
7
+ ATTR_NAME_REGEX = /\A[a-z0-9_]+\z/.freeze
8
+
9
+ def self.extended(base)
10
+ base.extend(ClassMethods)
11
+ base.include(InstanceMethods)
12
+ end
13
+
14
+ module ClassMethods
15
+ def lazy_attributes
16
+ @lazy_attributes ||= Set.new
17
+ end
18
+
19
+ def all_lazy_attributes
20
+ ancestors.select { |ancestor| ancestor.singleton_class.include?(::LazyLoadAttributes) }
21
+ .map(&:lazy_attributes)
22
+ .reduce(&:|)
23
+ end
24
+
25
+ def lazy_attr_reader(attribute, &initializer) # rubocop:disable Metrics/MethodLength
26
+ raise NameError, "bad attribute name '#{attribute}' (use a-z, 0-9, _)" unless attribute.match? ATTR_NAME_REGEX
27
+ raise ArgumentError, "no initializer block given in lazy-loaded attribute definition" if initializer.nil?
28
+
29
+ lazy_attributes.add(attribute)
30
+
31
+ define_method(attribute) do
32
+ instance_variable = :"@#{attribute}"
33
+ if instance_variable_defined?(instance_variable)
34
+ instance_variable_get(instance_variable)
35
+ else
36
+ instance_variable_set(instance_variable, instance_eval(&initializer))
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ module InstanceMethods
43
+ def eager_load_attributes!
44
+ self.class.all_lazy_attributes.each { |attribute| send(attribute) }
45
+ end
46
+ end
47
+ end
metadata ADDED
@@ -0,0 +1,156 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lazy_load_attributes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nate Eizenga
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-10-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry-byebug
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '13.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '13.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.21'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.21'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop-performance
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop-rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '2.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '2.0'
111
+ description: |
112
+ Simple "Ruby core"-inspired syntactic sugar for defining cached, lazy-loaded attributes which intuitively handle
113
+ inheritence and attribute redefinition.
114
+ email:
115
+ - eizengan@gmail.com
116
+ executables: []
117
+ extensions: []
118
+ extra_rdoc_files: []
119
+ files:
120
+ - CHANGELOG.md
121
+ - CODE_OF_CONDUCT.md
122
+ - README.md
123
+ - lazy_load_attributes.gemspec
124
+ - lib/lazy_load_attributes.rb
125
+ - lib/lazy_load_attributes/version.rb
126
+ homepage: https://github.com/eizengan/lazy_load_attributes
127
+ licenses:
128
+ - MIT
129
+ metadata:
130
+ homepage_uri: https://github.com/eizengan/lazy_load_attributes
131
+ source_code_uri: https://github.com/eizengan/lazy_load_attributes
132
+ changelog_uri: https://github.com/eizengan/lazy_load_attributes/blob/main/CHANGELOG.md
133
+ rubygems_mfa_required: 'true'
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '2.7'
143
+ - - "<"
144
+ - !ruby/object:Gem::Version
145
+ version: '4'
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ requirements: []
152
+ rubygems_version: 3.1.6
153
+ signing_key:
154
+ specification_version: 4
155
+ summary: Concise definition of lazy-loaded class attributes
156
+ test_files: []