Sha256: e7d3855aecb6651ea3f917437fa3fe374bb53ee9794f16a407f96d66ebde79ab

Contents?: true

Size: 1.8 KB

Versions: 31

Compression:

Stored size: 1.8 KB

Contents

require 'active_support'

class LHS::Record

  # Allows configuring endpoints
  # like which keys are used for the items, offset, total etc.
  module Configuration
    extend ActiveSupport::Concern

    mattr_accessor :configuration

    module ClassMethods
      def configuration(args)
        @configuration = args.freeze || {}
      end

      def item_key
        symbolize_unless_complex(
          @configuration.try(:[], :item_key) || :item
        )
      end

      def items_key
        symbolize_unless_complex(
          @configuration.try(:[], :items_key) || :items
        )
      end

      def item_created_key
        symbolize_unless_complex(
          @configuration.try(:[], :item_created_key)
        )
      end

      def limit_key(type = nil)
        symbolize_unless_complex(
          pagination_parameter(@configuration.try(:[], :limit_key), type) ||
          :limit
        )
      end

      def total_key
        symbolize_unless_complex(
          @configuration.try(:[], :total_key) || :total
        )
      end

      # Key used for determine current page
      def pagination_key(type = nil)
        symbolize_unless_complex(
          pagination_parameter(@configuration.try(:[], :pagination_key), type) ||
          :offset
        )
      end

      # Strategy used for calculationg next pages and navigate pages
      def pagination_strategy
        symbolize_unless_complex(
          @configuration.try(:[], :pagination_strategy) || :offset
        )
      end

      private

      def symbolize_unless_complex(value)
        return if value.blank?
        return value.to_sym unless value.is_a?(Array)
        value
      end

      def pagination_parameter(configuration, type)
        return configuration unless configuration.is_a?(Hash)
        configuration[type]
      end
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
lhs-15.5.1 lib/lhs/concerns/record/configuration.rb
lhs-15.5.0 lib/lhs/concerns/record/configuration.rb
lhs-15.4.1 lib/lhs/concerns/record/configuration.rb
lhs-15.4.0 lib/lhs/concerns/record/configuration.rb
lhs-15.4.0.pre.hasone.1 lib/lhs/concerns/record/configuration.rb
lhs-15.3.3 lib/lhs/concerns/record/configuration.rb
lhs-15.3.3.pre.fixoptions.1 lib/lhs/concerns/record/configuration.rb
lhs-15.3.2 lib/lhs/concerns/record/configuration.rb
lhs-15.3.1 lib/lhs/concerns/record/configuration.rb
lhs-15.3.1.pre.fixlhc.1 lib/lhs/concerns/record/configuration.rb
lhs-15.3.0 lib/lhs/concerns/record/configuration.rb
lhs-15.2.5 lib/lhs/concerns/record/configuration.rb
lhs-15.2.4 lib/lhs/concerns/record/configuration.rb
lhs-15.2.3 lib/lhs/concerns/record/configuration.rb
lhs-15.2.3.pre.favorites.1 lib/lhs/concerns/record/configuration.rb
lhs-15.2.2.pre.favorites.1 lib/lhs/concerns/record/configuration.rb
lhs-15.2.2 lib/lhs/concerns/record/configuration.rb
lhs-15.2.1 lib/lhs/concerns/record/configuration.rb
lhs-15.2.0 lib/lhs/concerns/record/configuration.rb
lhs-15.1.1 lib/lhs/concerns/record/configuration.rb