Class: Shamu::Entities::ListScope
- Inherits:
-
Object
- Object
- Shamu::Entities::ListScope
- Includes:
- Attributes, Attributes::Assignment, Attributes::Validation
- Defined in:
- lib/shamu/entities/list_scope.rb,
lib/shamu/entities/list_scope/dates.rb,
lib/shamu/entities/list_scope/paging.rb,
lib/shamu/entities/list_scope/sorting.rb,
lib/shamu/entities/list_scope/scoped_paging.rb,
lib/shamu/entities/list_scope/window_paging.rb
Overview
The desired scope of entities offered Services::Service to prepare a list of entities.
Standard scopes
Direct Known Subclasses
Defined Under Namespace
Modules: Dates, Paging, ScopedPaging, Sorting, WindowPaging
Class Method Summary collapse
-
.coerce(params) ⇒ ListScope
Coerces a hash or params object to a proper ListScope object.
-
.coerce!(params) ⇒ ListScope
Coerces the given params object and raises an ArgumentError if any of the parameters are invalid.
-
.for(entity_class) ⇒ ListScope
Finds the natural ListScope class for the given entity class.
Instance Method Summary collapse
-
#except(*param_names) ⇒ ListScope
Clone the params but exclude the given parameters.
-
#params ⇒ Hash
The hash of attributes that can be used to generate a url.
Methods included from Attributes::Validation
attribute, #valid?, #validated?
Methods included from Attributes::Assignment
#[]=, #assigned?, #assigned_attributes, attribute, #unassigned_attributes
Methods included from Attributes
#[], #as_json, #assign_attributes, association, associations, attribute, #attribute?, attributes, #initialize, #pretty_print, #set?, #slice, #to_attributes, #to_json
Class Method Details
.coerce(params) ⇒ ListScope
Coerces a hash or params object to a proper ListScope object.
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/shamu/entities/list_scope.rb', line 56 def coerce( params ) if params.is_a?( self ) params elsif params.respond_to?( :to_h ) || params.respond_to?( :to_attributes ) new( params ) elsif params.nil? new else raise ArgumentError end end |
.coerce!(params) ⇒ ListScope
Coerces the given params object and raises an ArgumentError if any of the parameters are invalid.
72 73 74 75 76 |
# File 'lib/shamu/entities/list_scope.rb', line 72 def coerce!( params ) coerced = coerce( params ) raise ArgumentError unless coerced.valid? coerced end |
.for(entity_class) ⇒ ListScope
Finds the natural Shamu::Entities::ListScope class for the given entity class.
Users::UserEntity -> Users::UserListScope or Users::ListScope
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/shamu/entities/list_scope.rb', line 85 def for( entity_class ) base_name = entity_class.name || "Entity" name = base_name.sub /(Entity)?$/, "ListScope" begin return name.constantize # Rescuing instead of using const_defined? so that active record can # do it's auto-loading magic. rescue NameError # rubocop:disable Lint/HandleExceptions end name = base_name.sub /::[A-Za-z0-9]+(Entity)?$/, "::ListScope" begin return name.constantize rescue NameError # rubocop:disable Lint/HandleExceptions end self end |
Instance Method Details
#except(*param_names) ⇒ ListScope
Clone the params but exclude the given parameters.
39 40 41 |
# File 'lib/shamu/entities/list_scope.rb', line 39 def except( *param_names ) self.class.new( to_attributes( except: param_names ) ) end |
#params ⇒ Hash
Returns the hash of attributes that can be used to generate a url.
44 45 46 47 48 49 50 |
# File 'lib/shamu/entities/list_scope.rb', line 44 def params params = to_attributes params.each do |key, value| params[key] = value.params if value.respond_to?( :params ) end params end |