Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cattr_reader_preloaded

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cattr_reader_preloaded

  • 0.0.1
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

Build Status - Code Climate tested with Ruby (1.8.7, 1.9.3, and 2.0.0), JRuby(1.8 and 1.9 mode), and Rubinius(1.8 and 1.9 mode) - see .travis.yml

##TL;DR Precompute asynchronously a value when the file is loaded, not on the 1st call.

cattr_reader_preloaded :gem_names do
  `gem list --no-version`  # <<< SLOW
end

##Problem : Some operations are very slow the 1st time you perform them, even when this takes place long after the program was started. Memoization will only speed up the 2nd and later calls.

Example : this operation

def gems_names
  @@_gems_names ||= `gem list --no-version`  # <<<
end

is slow :

sleep 10 # simulates the program running for a while.

3.times do
  start = Time.now
  gems_names  # <<< the slow operation
  puts "%.4f" % (Time.now - start)
end
1.6710  : 1st call = slow
0.0000  : 2nd call = fast (memoized)
0.0000  : 3rd call = fast (memoized)

##Solution :

This gem adds a cattr_reader_preloaded method to Kernel that starts precomputing - asynchronously the value as soon as the file (class or module) is loaded by Ruby

# replace
#   def gems_names
#     @@_gems_names ||= `gem list --no-version`
#   end
# by
cattr_reader_preloaded  :gem_names do
  `gem list --no-version`  # <<< SLOW
end

and the result is now

0.0000
0.0000
0.0000

Installation

Add this line to your application's Gemfile:

gem 'cattr_reader_preloaded'

And then execute:

$ bundle

Contributing

  1. Respect the code style and formatting.
  2. Fork it
  3. Create your feature branch (git checkout -b my-new-feature)
  4. Commit your changes (git commit -am 'Add some feature')
  5. Push to the branch (git push origin my-new-feature)
  6. Create new Pull Request

FAQs

Package last updated on 16 May 2013

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc