Sha256: 785070db6ca43232d11d5102f80bbc3d17dec77c5f6ac85ae550f8f91d7f203e

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

require 'active_set/version'

require 'active_set/processor_filter'
require 'active_set/processor_sort'
require 'active_set/processor_paginate'
require 'active_set/processor_transform'

class ActiveSet
  include Enumerable

  attr_reader :set, :view, :instructions

  def initialize(set, view: nil, instructions: {})
    @set = set
    @view = view || set
    @instructions = instructions
  end

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

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

  def method_missing(method_name, *args, &block)
    return @view.send(method_name, *args, &block) if @view.respond_to?(method_name)

    super
  end

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

  def filter(instructions)
    filterer = Processor::Filter.new(@view, instructions)
    reinitialize(filterer.process, :filter, instructions)
  end

  def sort(instructions)
    sorter = Processor::Sort.new(@view, instructions)
    reinitialize(sorter.process, :sort, instructions)
  end

  def paginate(instructions)
    paginater = Processor::Paginate.new(@view, instructions)
    reinitialize(paginater.process, :paginate, instructions)
  end

  def transform(instructions)
    transformer = Processor::Transform.new(@view, instructions)
    transformer.process
  end

  private

  def reinitialize(processed_set, method, instructions)
    self.class.new(@set,
                   view: processed_set,
                   instructions: @instructions.merge(
                     method => instructions))
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activeset-0.6.1 lib/active_set.rb