
Security News
MCP Community Begins Work on Official MCP Metaregistry
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
lazy_load_attributes
Advanced tools
Simple "Ruby core"-inspired syntactic sugar for defining cached, lazy-loaded attributes which intuitively handle inheritence and attribute redefinition.
Add this line to your application's Gemfile:
gem 'lazy_load_attributes'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install lazy_load_attributes
The class-level lazy_attr_reader
method defines a lazy-loaded attribute.
A simple use-case:
require "lazy_load_attributes"
class Example
extend LazyLoadAttributes
lazy_attr_reader(:lazy_attr) { puts "loaded"; lazy_attr_method }
def lazy_attr_method
"lazy value"
end
end
example = Example.new
example.lazy_attr
# loaded
# => "lazy value"
example.lazy_attr
# => "lazy value"
This illustrates four major powers of LazyLoadAttributes:
example
in the sample above), so its instance methods are available during evaluation as they would be within the body of a normal instance methodInheritence is handled transparently:
class Superclass
extend LazyLoadAttributes
lazy_attr_reader(:super_attr) { super_attr_value }
lazy_attr_reader(:redefine_attr) { "redefine_attr from Superclass" }
def super_attr_value
"Superclass#super_attr_value"
end
def sub_attr_value
"Superclass#sub_attr_value"
end
end
class Subclass < Superclass
lazy_attr_reader(:redefine_attr) { "redefine_attr from Subclass" }
lazy_attr_reader(:sub_attr) { sub_attr_value }
def super_attr_value
"Subclass#super_attr_value"
end
end
super_instance = Superclass.new
super_instance.super_attr
# => "Superclass#super_attr_value"
super_instance.redefine_attr
# => "redefine_attr from Superclass"
sub_instance = Subclass.new
sub_instance.super_attr
# => "Subclass#super_attr_value"
sub_instance.sub_attr
# => "Superclass#sub_attr_value"
sub_instance.redefine_attr
# => "redefine_attr from Subclass"
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
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
The call to sub_instance.redefine_attr
shows that an attribute redefined in a subclass will override its definition in the superclass
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.
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.
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.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the LazyLoadAttributes project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
FAQs
Unknown package
We found that lazy_load_attributes demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
Research
Security News
Socket uncovers an npm Trojan stealing crypto wallets and BullX credentials via obfuscated code and Telegram exfiltration.
Research
Security News
Malicious npm packages posing as developer tools target macOS Cursor IDE users, stealing credentials and modifying files to gain persistent backdoor access.