Sha256: a68455676aa13958a9070052a5760abd7e3be2e06524fe65c2edb931bd99e118

Contents?: true

Size: 903 Bytes

Versions: 1

Compression:

Stored size: 903 Bytes

Contents

module SearchObject
  class Search
    attr_reader :params

    class << self
      def build_for(config, options)
        scope   = options[:scope] || (config[:scope] && config[:scope].call)
        filters = Helper.stringify_keys(options.fetch(:filters, {}))
        params  = config[:defaults].merge Helper.slice_keys(filters, config[:actions].keys)

        raise MissingScopeError unless scope

        new scope, params, config[:actions]
      end
    end

    def initialize(scope, params, actions)
      @scope    = scope
      @actions  = actions
      @params   = params
    end

    def param(name)
      @params[name]
    end

    def query(context)
      @params.inject(@scope) do |scope, (name, value)|
        new_scope = context.instance_exec scope, value, &@actions[name]
        new_scope || scope
      end
    end

    def count(context)
      query(context).count
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
search_object-1.1.3 lib/search_object/search.rb