ruby jsonapi_suite用于内存Iterables适配器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby jsonapi_suite用于内存Iterables适配器相关的知识,希望对你有一定的参考价值。

module Jsonapi
  module Adapters
    # Adapter for resources already loaded in memory
    # 
    # The scope is meant to be something like an Array or Iterable of objects
    # 
    # @example Time Series
    # 
    # scope = [
    #   OpenStruct.new(at: Time.now.noon, name: 'I ate'),
    #   OpenStruct.new(at: Time.now.midnight, name: 'I went to sleep')
    # ]
    # 
    # Sorting by time 
    #   scope.sort_by(&:at)
    # Filtering by event name
    #   scope.select { |i| i.name == 'I ate' }
    #
    # @author [Cyril]
    #
    class InMemoryIterableAdapter < TransactionlessMongoidAdapter
      # @override
      def resolve(scope)
        scope
      end

      # @override for array sort
      def filter(scope, attribute, value)
        scope.select do |item|
          item.send(attribute) == value
        end
      end

      # @Override
      def order(scope, attribute, direction)
        # return scope if attribute == :id # Seems broken
        if direction == :asc
          scope.sort_by { |i| i.public_send(attribute) }
        elsif direction == :desc
          scope.sort_by { |i| i.public_send(attribute) }.reverse
        end
      end

      # @Override
      def paginate(scope, current_page, per_page)
        pg_start = (current_page - 1) * per_page
        pg_eng = (current_page) * per_page - 1
        scope[pg_start..pg_eng]
      end

      # @Override
      def count(scope, _attr)
        scope.size
      end
    end
  end
end

以上是关于ruby jsonapi_suite用于内存Iterables适配器的主要内容,如果未能解决你的问题,请参考以下文章

text 适用于jsonapi_suite的Mongoid适配器

ruby 用于测试Rails应用程序的内存使用情况的脚本。它将针对指定的操作运行30个请求并报告f

ruby 用于测试Rails应用程序的内存使用情况的脚本。它将针对指定的操作运行30个请求并报告f

在 Ruby 中迭代数组时如何修改数组?

MATLAB中的\ite是啥?啥情况下要用括起来,啥时候不要?

QDataWidgetMapper-在pyqt中查找sql​​ite数据库行的索引