Module: CleanQueryParams

Defined in:
lib/clean_query_params.rb,
lib/clean_query_params/version.rb

Defined Under Namespace

Modules: Dsl

Constant Summary collapse

GLOBAL_DEFAULTS =

Hmmmm - probably too presumptuous. Make this confugurable at the app-level?

{
  page_number:  1,
  page_size:    20,
  sort_by:      'id',
  sort_dir:     'ASC'
}
VERSION =
"0.0.3"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(host_class) ⇒ Object

Define and initialise the class-level _defaults Hash and _valid_query_keys Set.

Extend host class with macros when we’re included.



45
46
47
48
49
50
51
52
53
# File 'lib/clean_query_params.rb', line 45

def self.included(host_class)
  host_class.class_attribute :_defaults
  host_class._defaults = Hash.new

  host_class.class_attribute :_valid_query_keys
  host_class._valid_query_keys = Set.new

  host_class.extend(Dsl)
end

Instance Method Details

#initialize(params = {}) ⇒ Object



15
16
17
18
# File 'lib/clean_query_params.rb', line 15

def initialize(params = {})
  params.keep_if { |k,v| v.present? }
  @params = defaults.merge(params)
end

#page_numberObject



32
33
34
# File 'lib/clean_query_params.rb', line 32

def page_number
  params[:page_number]
end

#page_sizeObject



36
37
38
# File 'lib/clean_query_params.rb', line 36

def page_size
  params[:page_size]
end

#queryObject



20
21
22
# File 'lib/clean_query_params.rb', line 20

def query
  params.slice(*valid_query_keys)
end

#sort_byObject



24
25
26
# File 'lib/clean_query_params.rb', line 24

def sort_by
  params[:sort_by]
end

#sort_dirObject



28
29
30
# File 'lib/clean_query_params.rb', line 28

def sort_dir
  params[:sort_dir].upcase
end