Sha256: faa7e5cb7001bcd370635f30ff2dd3ae29bdcf4ce1a962a461dc2c3d999b3b2c

Contents?: true

Size: 1.99 KB

Versions: 9

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true

require 'active_support/core_ext/hash/reverse_merge'
require 'patches/core_ext/hash/flatten_keys'
require 'helpers/throws'
require 'active_set/attribute_instruction'
require 'active_set/filtering/operation'
require 'active_set/sorting/operation'
require 'active_set/paginating/operation'
require 'active_set/exporting/operation'

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

  # :nocov:
  def inspect
    "#<ActiveSet:#{object_id} @instructions=#{@instructions.inspect}>"
  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
  # :nocov:

  def filter(instructions_hash)
    filterer = Filtering::Operation.new(@view, instructions_hash)
    reinitialize(filterer.execute, :filter, filterer.operation_instructions)
  end

  def sort(instructions_hash)
    sorter = Sorting::Operation.new(@view, instructions_hash)
    reinitialize(sorter.execute, :sort, sorter.operation_instructions)
  end

  def paginate(instructions_hash)
    paginater = Paginating::Operation.new(@view, instructions_hash)
    reinitialize(paginater.execute, :paginate, paginater.operation_instructions)
  end

  def export(instructions_hash)
    exporter = Exporting::Operation.new(@view, instructions_hash)
    exporter.execute
  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

9 entries across 9 versions & 2 rubygems

Version Path
actionset-0.8.1 lib/active_set.rb
actionset-0.8.0 lib/active_set.rb
activeset-0.8.6 lib/active_set.rb
activeset-0.8.5 lib/active_set.rb
activeset-0.8.4 lib/active_set.rb
activeset-0.8.3 lib/active_set.rb
activeset-0.8.2 lib/active_set.rb
activeset-0.8.1 lib/active_set.rb
activeset-0.8.0 lib/active_set.rb