README.md in n1_loader-0.1.2 vs README.md in n1_loader-1.0.0
- old
+ new
@@ -7,15 +7,19 @@
We have a solution for you!
[N1Loader][8] is designed to solve the issue for good!
It has many benefits:
-- it loads data lazily (even when you initialized preloading)
-- it supports shared loaders between multiple classes
+- it can be [isolated](#isolated-loaders)
+- it loads data [lazily](#lazy-loading)
+- it supports [shareable loaders](#shareable-loaders) between multiple classes
+- it supports [reloading](#reloading)
+- it supports optimized [single object loading](#optimized-single-case)
- it has an integration with [ActiveRecord][5] which makes it brilliant ([example](#activerecord))
- it has an integration with [ArLazyPreload][6] which makes it excellent ([example](#arlazypreload))
+... and even more features to come! Stay tuned!
## Installation
Add this line to your application's Gemfile:
@@ -23,20 +27,22 @@
gem 'n1_loader'
```
You can add integration with [ActiveRecord][5] by:
```ruby
-require 'n1_loader/active_record'
+gem 'n1_loader', require: 'n1_loader/active_record'
```
You can add the integration with [ActiveRecord][5] and [ArLazyPreload][6] by:
```ruby
-require 'n1_loader/ar_lazy_preload'
+gem 'n1_loader', require: 'n1_loader/ar_lazy_preload'
```
## Usage
+**Supported Ruby version:** 2.5, 2.6, 2.7, 3.0, and latest.
+
```ruby
class Example
include N1Loader::Loadable
# with inline loader
@@ -65,10 +71,134 @@
objects = [Example.new, Example.new]
N1Loader::Preloader.new(objects).preload(:anything)
objects.map(&:anything)
```
+### Lazy loading
+
+```ruby
+class Example
+ include N1Loader::Loadable
+
+ n1_loader :anything do |elements|
+ # Has to return a hash that has keys as element from elements
+ elements.group_by(&:itself)
+ end
+end
+
+object = Example.new # => nothing was done for loading
+object.anything # => first time loading
+
+objects = [Example.new, Example.new] # => nothing was done for loading
+N1Loader::Preloader.new([objects]).preload(:anything) # => we only initial loader but didn't perform it yet
+objects.map(&:anything) # => loading happen for the first time (without N+1)
+```
+
+
+### Shareable loaders
+
+```ruby
+class MyLoader < N1Loader::Loader
+ def perform(elements)
+ # Has to return a hash that has keys as element from elements
+ elements.group_by(&:itself)
+ end
+end
+
+class A
+ include N1Loader::Loadable
+
+ n1_loader :anything, MyLoader
+end
+
+class B
+ include N1Loader::Loadable
+
+ n1_loader :something, MyLoader
+end
+
+A.new.anything # => works
+B.new.something # => works
+```
+
+### Reloading
+
+```ruby
+class Example
+ include N1Loader::Loadable
+
+ # with inline loader
+ n1_loader :anything do |elements|
+ # Has to return a hash that has keys as element from elements
+ elements.group_by(&:itself)
+ end
+end
+
+object = Example.new
+object.anything # => loader is executed first time and value was cached
+object.anything(reload: true) # => loader is executed again and a new value was cached
+
+objects = [Example.new, Example.new]
+
+N1Loader::Preloader.new(objects).preload(:anything) # => loader was initialized but not yet executed
+objects.map(&:anything) # => loader was executed first time without N+1 issue and values were cached
+
+N1Loader::Preloader.new(objects).preload(:anything) # => loader was initialized again but not yet executed
+objects.map(&:anything) # => new loader was executed first time without N+1 issue and new values were cached
+```
+
+### Isolated loaders
+
+```ruby
+class MyLoader < N1Loader::Loader
+ def perform(elements)
+ # Has to return a hash that has keys as element from elements
+ elements.group_by(&:itself)
+ end
+end
+
+objects = [1, 2, 3, 4]
+loader = MyLoader.new(objects)
+objects.each do |object|
+ loader.for(object) # => it has no N+1 and it doesn't require to be injected in the class
+end
+```
+
+### Optimized single case
+
+```ruby
+class Example
+ include N1Loader::Loadable
+
+ n1_loader :something do # no arguments passed to the block, so we can override both perform and single.
+ def perform(elements)
+ # Has to return a hash that has keys as element from elements
+ elements.group_by(&:itself)
+ end
+
+ # Optimized for single object loading
+ def single(element)
+ # Just return a value you want to have for this element
+ element
+ end
+ end
+end
+
+object = Example.new
+object.something # single will be used here
+
+objects = [Example.new, Example.new]
+N1Loader::Preloader.new(objects).preload(:something)
+objects.map(&:something) # perform will be used once without N+1
+```
+
+## Integrations
+
### [ActiveRecord][5]
+
+**Supported versions**: 5, 6.
+
+_Note_: Please open an issue if you interested in support of version 7 or other.
```ruby
class User < ActiveRecord::Base
include N1Loader::Loadable
\ No newline at end of file