Sha256: 880119d80d12e909f95b09b3d2f092f3a6751eb2699c63323f1e3e794e52893f

Contents?: true

Size: 1.7 KB

Versions: 1

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

require 'active_set/version'

require 'active_set/processors/filter_processor'
require 'active_set/processors/sort_processor'
require 'active_set/processors/paginate_processor'
require 'active_set/processors/transform_processor'

class ActiveSet
  include Enumerable

  attr_reader :set, :instructions, :total_count

  def initialize(set, instructions: {}, total_count: nil)
    @set = set
    @instructions = instructions
    @total_count = total_count || @set.count
  end

  def each(&block)
    @set.each(&block)
  end

  def ==(other)
    return @set == other unless other.is_a?(ActiveSet)
    @set == other.set
  end

  def method_missing(method_name, *args, &block)
    @set.send(method_name, *args, &block) || super
  end

  def respond_to_missing?(method_name, include_private = false)
    @set.respond_to?(method_name) || super
  end

  def filter(instructions)
    filterer = FilterProcessor.new(@set, instructions)
    self.class.new(filterer.process,
                   instructions: @instructions.merge(filter: instructions),
                   total_count: @total_count)
  end

  def sort(instructions)
    sorter = SortProcessor.new(@set, instructions)
    self.class.new(sorter.process,
                   instructions: @instructions.merge(sort: instructions),
                   total_count: @total_count)
  end

  def paginate(instructions)
    paginater = PaginateProcessor.new(@set, instructions)
    self.class.new(paginater.process,
                   instructions: @instructions.merge(paginate: instructions),
                   total_count: @total_count)
  end

  def transform(instructions)
    transformer = TransformProcessor.new(@set, instructions)
    transformer.process
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activeset-0.5.2 lib/active_set.rb