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
-
.included(host_class) ⇒ Object
Define and initialise the class-level _defaults Hash and _valid_query_keys Set.
Instance Method Summary collapse
- #initialize(params = {}) ⇒ Object
- #page_number ⇒ Object
- #page_size ⇒ Object
- #query ⇒ Object
- #sort_by ⇒ Object
- #sort_dir ⇒ Object
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_number ⇒ Object
32 33 34 |
# File 'lib/clean_query_params.rb', line 32 def page_number params[:page_number] end |
#page_size ⇒ Object
36 37 38 |
# File 'lib/clean_query_params.rb', line 36 def page_size params[:page_size] end |
#query ⇒ Object
20 21 22 |
# File 'lib/clean_query_params.rb', line 20 def query params.slice(*valid_query_keys) end |
#sort_by ⇒ Object
24 25 26 |
# File 'lib/clean_query_params.rb', line 24 def sort_by params[:sort_by] end |
#sort_dir ⇒ Object
28 29 30 |
# File 'lib/clean_query_params.rb', line 28 def sort_dir params[:sort_dir].upcase end |