README.md in n1_loader-1.0.0 vs README.md in n1_loader-1.1.0

- old
+ new

@@ -45,23 +45,21 @@ 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) + elements.each { |element| fulfill(element, [element]) } end # with custom loader n1_loader :something, MyLoader end # Custom loader that can be shared with many classes class MyLoader < N1Loader::Loader - # Has to return a hash that has keys as element from elements def perform(elements) - elements.group_by(&:itself) + elements.each { |element| fulfill(element, [element]) } end end # For single object ex = Example.new @@ -78,12 +76,11 @@ ```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) + elements.each { |element| fulfill(element, [element]) } end end object = Example.new # => nothing was done for loading object.anything # => first time loading @@ -97,12 +94,11 @@ ### 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) + elements.each { |element| fulfill(element, [element]) } end end class A include N1Loader::Loadable @@ -126,12 +122,11 @@ 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) + elements.each { |element| fulfill(element, [element]) } end end object = Example.new object.anything # => loader is executed first time and value was cached @@ -149,12 +144,11 @@ ### 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) + elements.each { |element| fulfill(element, [element]) } end end objects = [1, 2, 3, 4] loader = MyLoader.new(objects) @@ -169,18 +163,17 @@ 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) + elements.each { |element| fulfill(element, [element]) } end # Optimized for single object loading def single(element) # Just return a value you want to have for this element - element + [element] end end end object = Example.new @@ -204,12 +197,11 @@ include N1Loader::Loadable n1_loader :orders_count do |users| hash = Order.where(user: users).group(:user_id).count - # hash has to have keys as initial elements - hash.transform_keys! { |key| users.find { |user| user.id == key } } + users.each { |user| fulfill(user, hash[user.id]) } end end # For single user user = User.first @@ -232,12 +224,11 @@ class User < ActiveRecord::Base include N1Loader::Loadable n1_loader :orders_count do |users| hash = Order.where(user: users).group(:user_id).count - - # hash has to have keys as initial elements - hash.transform_keys! { |key| users.find { |user| user.id == key } } + + users.each { |user| fulfill(user, hash[user.id]) } end end # For single user user = User.first \ No newline at end of file