Sha256: 453259463ccc0c9568a00820301a9ccf94754a602b1a4a2febaa131bbf902166

Contents?: true

Size: 1.55 KB

Versions: 1

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

require 'fetcheable_on_api/configuration'
require 'fetcheable_on_api/filtreable'
require 'fetcheable_on_api/pagineable'
require 'fetcheable_on_api/sortable'
require 'fetcheable_on_api/version'
require 'active_support'

module FetcheableOnApi
  #
  # Configuration
  #
  class << self
    attr_accessor :configuration
  end

  def self.configuration
    @configuration ||= Configuration.new
  end

  def self.configure
    yield(configuration)
  end

  #
  # Supports
  #

  #
  # Public class methods
  #
  def self.included(klass)
    klass.class_eval do
      include Filtreable
      include Sortable
      include Pagineable
    end
  end

  #
  # Public instance methods
  #

  #
  # Protected instance methods
  #
  protected

  def apply_fetcheable(collection)
    collection = apply_filters(collection)
    collection = apply_sort(collection)

    apply_pagination(collection)
  end

  def valid_parameters!(*keys, permitted_types: default_permitted_types)
    raise ArgumentError.new(
      "Incorrect type #{params.dig(*keys).class} for params #{keys}"
    ) unless valid_params_types(*keys, permitted_types: permitted_types)
  end

  def valid_params_types(*keys, permitted_types: default_permitted_types)
    permitted_types.inject(false) do |res, type|
      res || valid_params_type(params.dig(*keys), type)
    end
  end

  def valid_params_type(value, type)
    value.is_a?(type)
  end

  def default_permitted_types
    [ActionController::Parameters, Hash]
  end
end

ActiveSupport.on_load :action_controller do
  include FetcheableOnApi
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fetcheable_on_api-0.1.6 lib/fetcheable_on_api.rb